For educational purposes, I need to send an email through an SMTP server, using SMTP's fundamental and simple rules.
I was able to do that using smtp4dev. I telnet localhost 25
and and commands are:
I want to do the same thing, using Gmail SMTP server. However, it requires authentication and TLS. I can't figure out how to do that for Gmail. Here's a screenshot of telnet smtp.gmail.com 587
:
I searched and found many links including Wikipedia's article about STARTTLS
command. But I'm not able to use TLS and authenticate to Gmail's SMTP server using command line (or sending commands myself in programming languages). Can anyone help?
Based on the existing answers, here's a step-by-step guide to sending automated e-mails over SMTP, using a GMail account, from the command line, without disclosing the password.
Requirements
First, install the following software packages:
These instructions assume a Linux operating system, but should be reasonably easy to port to Windows (via Cygwin or native equivalents), or other operating system.
Authentication
Save the following shell script as
authentication.sh
:Make it executable and run it as follows:
When prompted, provide your e-mail address and password. This will look something like:
Copy the last line (
AGJ...==
), as this will be used for authentication.Notification
Save the following expect script as
notify.sh
(note the first line refers to the expect program):Make the following changes:
YOUR_AUTHENTICATION_CODE
with the authentication code generated by the authentication script.YOUR_EMAIL_ADDRESS
with the e-mail address used to generate the authentication code.For example (note the angle brackets are retained for the e-mail address):
Lastly, make the notify script executable as follows:
Send E-mail
Send an e-mail from the command line as follows:
As no one has mentioned - I would suggest to use great tool for such purpose - swaks
It has a lot of options and can do almost everything you want.
GMAIL: STARTTLS, SSLv3 (and yes, in 2016 gmail still support sslv3)
YAHOO: TLS aka SMTPS, tlsv1.2
I have been using swaks to send email notifications from nagios via gmail for last 5 years without any problem.
to send over gmail, you need to use an encrypted connection. this is not possible with telnet alone, but you can use tools like openssl
either connect using the starttls option in openssl to convert the plain connection to encrypted...
openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf -ign_eof
or connect to a ssl sockect directly...
openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof
EHLO localhost
after that, authenticate to the server using the base64 encoded username/password
AUTH PLAIN AG15ZW1haWxAZ21haWwuY29tAG15cGFzc3dvcmQ=
to get this from the commandline:
then continue with "mail from:" like in your example
example session:
Unfortunately as I am forced to use a windows server I have been unable to get openssl working in the way the above answer suggests.
However I was able to get a similar program called stunnel (which can be downloaded from here) to work. I got the idea from www.tech-and-dev.com but I had to change the instructions slightly. Here is what I did:
stunnel.conf
config file, which in my case I installed toC:\Program Files (x86)\stunnel
Then, you need to open this file in a text viewer such as notepad. Look for
[gmail-smtp]
and remove the semicolon on the client line below (in the stunnel.conf file, every line that starts with a semicolon is a comment). You should end up with something like:Once you have done this save the
stunnel.conf
file and reload the config (to do this use the stunnel GUI program, and click on configuration=>Reload).Now you should be ready to send email in the windows telnet client!
Go to Start=>run=>cmd.
Once cmd is open type in the following and press Enter:
You should then see something similar to the following:
You will then need to reply by typing the following and pressing enter:
This should give you the following response:
If you get this you then need to type the following and press enter:
This should then give you the following response:
Now you should be ready to authenticate with your Gmail details. To do this type the following and press enter:
This should then give you the following response:
This means that we are ready to authenticate by using our gmail address and password.
However since this is an encrypted session, we're going to have to send the email and password encoded in base64. To encode your email and password, you can use a converter program or an online website to encode it (for example base64 or search on google for ’base64 online encoding’). I reccomend you do not touch the cmd/telnet session again until you have done this.
For example test@gmail.com would become dGVzdEBnbWFpbC5jb20= and password would become cGFzc3dvcmQ=
Once you have done this copy and paste your converted base64 username into the cmd/telnet session and press enter. This should give you following response:
Now copy and paste your converted base64 password into the cmd/telnet session and press enter. This should give you following response if both login credentials are correct:
You should now enter the sender email (should be the same as the username) in the following format and press enter:
This should give you the following response:
You can now enter the recipient email address in a similar format and press enter:
This should give you the following response:
Now you will need to type the following and press enter:
Which should give you the following response:
Now we can start to compose the message! To do this enter your message in the following format (Tip: do this in notepad and copy the entire message into the cmd/telnet session):
When you have finished the email enter a dot:
This should give you the following response:
And now you need to end your session by typing the following and pressing enter:
This should give you the following response:
And your email should now be in the recipient’s mailbox!