How to send email using simple SMTP commands through Gmail?

For educational purposes, I need to send an email through an SMTP server using basic and simple SMTP rules.

I was able to do this using smtp4dev . I have telnet localhost 25 and and the command:

enter image description here

I want to do the same using the Gmail SMTP server. However, this requires authentication and TLS. I can’t figure out how to do this for Gmail. Here is a screenshot of telnet smtp.gmail.com 587 :

enter image description here

I searched and found many links, including a Wikipedia article on the STARTTLS team. But I cannot use TLS and authenticate on the Gmail SMTP server using the command line (or send commands myself in programming languages). Can anyone help?

+58
email smtpclient smtp gmail smtp-auth
Jun 15 '12 at 7:10
source share
4 answers

to send via gmail you need to use an encrypted connection. this is not possible with telnet, but you can use tools like openssl

either connect using the starttls parameter in openssl to convert a regular connection to an encrypted one ...

openssl s_client -starttls smtp -connect smtp.gmail.com/1087 -crlf -ign_eof

or directly connect to ssl sockect ...

openssl s_client -connect smtp.gmail.com-00-0065 -crlf -ign_eof

Ehlo localhost

after that, authenticate to the server using a base64 encoded username / password

AUTH PLAIN AG15ZW1haWxAZ21haWwuY29tAG15cGFzc3dvcmQ =

to get this from the command line:

 perl -MMIME::Base64 -e 'print encode_base64("\000myemail\@gmail.com\000mypassword")' AG15ZW1haWxAZ21haWwuY29tAG15cGFzc3dvcmQ= 

then go to "mail from:", as in your example

Session Example:

 openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof [... lots of openssl output ...] 220 mx.google.com ESMTP m46sm11546481eeh.9 EHLO localhost 250-mx.google.com at your service, [1.2.3.4] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH 250 ENHANCEDSTATUSCODES AUTH PLAIN AG5pY2UudHJ5QGdtYWlsLmNvbQBub2l0c25vdG15cGFzc3dvcmQ= 235 2.7.0 Accepted MAIL FROM: <gryphius-demo@gmail.com> 250 2.1.0 OK m46sm11546481eeh.9 rcpt to: <somepoorguy@example.com> 250 2.1.5 OK m46sm11546481eeh.9 DATA 354 Go ahead m46sm11546481eeh.9 Subject: it works yay! . 250 2.0.0 OK 1339757532 m46sm11546481eeh.9 quit 221 2.0.0 closing connection m46sm11546481eeh.9 read:errno=0 
+99
Jun 15 '12 at 11:05
source share

Unfortunately, since I have to use a Windows server, I was not able to get openssl to work as the answer above suggests.

However, I managed 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 a bit. Here is what I did:

  • Install the telnet client in the window window.
  • Download stunnel. (I downloaded and installed a file called stunnel-4.56-installer.exe).
  • After installation, you need to find the configuration file stunnel.conf , which in my case I installed in C:\Program Files (x86)\stunnel
  • Then you need to open this file in a text editor such as notepad. Locate [gmail-smtp] and delete the semicolon on the client line below (in the stunnel.conf file, each line starting with a semicolon is a comment). You should get something like:

     [gmail-smtp] client = yes accept = 127.0.0.1:25 connect = smtp.gmail.com:465 

    After that, save the stunnel.conf file and reload the configuration (for this, use the stunnel GUI program and click on configuration => Reload).

You should now be ready to send email in the telnet windows client!
Go to Start => run => cmd.

As soon as cmd opens in the following form and press Enter:

 telnet localhost 25 

Then you should see something similar to the following:

 220 mx.google.com ESMTP f14sm1400408wbe.2 

Then you need to answer by typing the following and pressing enter:

 helo google 

This should give you the following answer:

 250 mx.google.com at your service 

If you get this, you need to enter the following and press enter:

 ehlo google 

Then you will get the following answer:

 250-mx.google.com at your service, [212.28.228.49] 250-SIZE 35651584 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH 250 ENHANCEDSTATUSCODES 

You should now be ready to authenticate with your Gmail data. To do this, enter the following and press enter:

 AUTH LOGIN 

Then you will get the following answer:

 334 VXNlcm5hbWU6 

This means that we are ready to authenticate using our gmail address and password.

However, since this is an encrypted session, we will need to send an email and password encoded in base64. To encode your email and password, you can use the converter program or an online website to encode it (for example, base64 or google search for base64 online encoding). I recommend that you do not touch the cmd / telnet session before you do this.

For example, test@gmail.com will become dGVzdEBnbWFpbC5jb20 = and the password will become cGFzc3dvcmQ =

Once you have made this copy and paste the converted base64 username into the cmd / telnet session and press enter. This should give you the following answer:

 334 UGFzc3dvcmQ6 

Now copy and paste the converted base64 password into the cmd / telnet session and press enter. This should give you the following answer if both credentials are correct:

 235 2.7.0 Accepted 

Now you must enter the sender address (it must match the username) in the following format and press enter:

 MAIL FROM:<test@gmail.com> 

This should give you the following answer:

 250 2.1.0 OK x23sm1104292weq.10 

Now you can enter the recipient's email address in the same format and press enter:

 RCPT TO:<recipient@gmail.com> 

This should give you the following answer:

 250 2.1.5 OK x23sm1104292weq.10 

Now you need to enter the following and press enter:

 DATA 

Which should give you the following answer:

 354 Go ahead x23sm1104292weq.10 

Now we can start composing a message! To do this, enter your message in the following format ( Tip : do this in notepad and copy the entire message to the cmd / telnet session):

 From: Test <test@gmail.com> To: Me <recipient@gmail.com> Subject: Testing email from telnet This is the body Adding more lines to the body message. 

When you finish writing, enter a period:

 . 

This should give you the following answer:

 250 2.0.0 OK 1288307376 x23sm1104292weq.10 

And now you need to end the session by typing the following and pressing enter:

 QUIT 

This should give you the following answer:

 221 2.0.0 closing connection x23sm1104292weq.10 Connection to host lost. 

Your email should now be in the recipient's inbox!

+21
Aug 15 '13 at 15:11
source share

According to existing answers, a step-by-step guide on sending automatic e-mail messages via SMTP using a GMail account from the command line without password disclosure.

Requirements

First install the following software packages:

These instructions assume the Linux operating system, but should be easy enough to port to Windows (via Cygwin or native equivalents) or to another operating system.

Authentication

Save the following shell script as authentication.sh :

 #!/bin/bash # Asks for a username and password, then spits out the encoded value for # use with authentication against SMTP servers. echo -n "Email (shown): " read email echo -n "Password (hidden): " read -s password echo TEXT="\0$email\0$password" echo -ne $TEXT | base64 

Make it executable and run it as follows:

 chmod +x authentication.sh ./authentication.sh 

When prompted, enter your email address and password. It will look something like this:

 Email (shown): bob@gmail.com Password (hidden): AGJvYkBnbWFpbC5jb20AYm9iaXN0aGViZXN0cGVyc29uZXZlcg== 

Copy the last line ( AGJ...== ), as it will be used for authentication.

Notification

Save the following expected script as notify.sh (note that the first line refers to the wait program):

 #!/usr/bin/expect set address "[lindex $argv 0]" set subject "[lindex $argv 1]" set ts_date "[lindex $argv 2]" set ts_time "[lindex $argv 3]" set timeout 10 spawn openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof expect "220" { send "EHLO localhost\n" expect "250" { send "AUTH PLAIN YOUR_AUTHENTICATION_CODE\n" expect "235" { send "MAIL FROM: <YOUR_EMAIL_ADDRESS>\n" expect "250" { send "RCPT TO: <$address>\n" expect "250" { send "DATA\n" expect "354" { send "Subject: $subject\n\n" send "Email sent on $ts_date at $ts_time.\n" send "\n.\n" expect "250" { send "quit\n" } } } } } } } 

Make the following changes:

  • Insert through the YOUR_AUTHENTICATION_CODE authentication code generated by the authentication script.
  • Change YOUR_EMAIL_ADDRESS to the email address used to generate the authentication code.
  • Save the file.

For example (note that angle brackets are stored for the email address):

 send "AUTH PLAIN AGJvYkBnbWFpbC5jb20AYm9iaXN0aGViZXN0cGVyc29uZXZlcg==\n" send "MAIL FROM: <bob@gmail.com>\n" 

Finally, make the script notification executable as follows:

 chmod +x notify.sh 

Send Email

Send the email from the command line as follows:

 ./notify.sh recipient@domain.com "Command Line" "March 14" "15:52" 
+2
Mar 29 '16 at 23:21
source share

Like no one mentioned - I would suggest using a great tool for this purpose - swaks

 # yum info swaks Installed Packages Name : swaks Arch : noarch Version : 20130209.0 Release : 3.el6 Size : 287 k Repo : installed From repo : epel Summary : Command-line SMTP transaction tester URL : http://www.jetmore.org/john/code/swaks License : GPLv2+ Description : Swiss Army Knife SMTP: A command line SMTP tester. Swaks can test : various aspects of your SMTP server, including TLS and AUTH. 

It has many options and can do almost anything you want.

GMAIL: STARTTLS, SSLv3 (and yes, in 2016 gmail still supports sslv3)

 $ echo "Hello world" | swaks -4 --server smtp.gmail.com:587 --from user@gmail.com --to user@example.net -tls --tls-protocol sslv3 --auth PLAIN --auth-user user@gmail.com --auth-password 7654321 --h-Subject "Test message" --body - === Trying smtp.gmail.com:587... === Connected to smtp.gmail.com. <- 220 smtp.gmail.com ESMTP h8sm76342lbd.48 - gsmtp -> EHLO www.example.net <- 250-smtp.gmail.com at your service, [193.243.156.26] <- 250-SIZE 35882577 <- 250-8BITMIME <- 250-STARTTLS <- 250-ENHANCEDSTATUSCODES <- 250-PIPELINING <- 250-CHUNKING <- 250 SMTPUTF8 -> STARTTLS <- 220 2.0.0 Ready to start TLS === TLS started with cipher SSLv3:RC4-SHA:128 === TLS no local certificate set === TLS peer DN="/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com" ~> EHLO www.example.net <~ 250-smtp.gmail.com at your service, [193.243.156.26] <~ 250-SIZE 35882577 <~ 250-8BITMIME <~ 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH <~ 250-ENHANCEDSTATUSCODES <~ 250-PIPELINING <~ 250-CHUNKING <~ 250 SMTPUTF8 ~> AUTH PLAIN AGFhQxsZXguaGhMGdATGV4X2hoYtYWlsLmNvbQBS9TU1MjQ= <~ 235 2.7.0 Accepted ~> MAIL FROM:<user@gmail.com> <~ 250 2.1.0 OK h8sm76342lbd.48 - gsmtp ~> RCPT TO:<user@example.net> <~ 250 2.1.5 OK h8sm76342lbd.48 - gsmtp ~> DATA <~ 354 Go ahead h8sm76342lbd.48 - gsmtp ~> Date: Wed, 17 Feb 2016 09:49:03 +0000 ~> To: user@example.net ~> From: user@gmail.com ~> Subject: Test message ~> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/ ~> ~> Hello world ~> ~> ~> . <~ 250 2.0.0 OK 1455702544 h8sm76342lbd.48 - gsmtp ~> QUIT <~ 221 2.0.0 closing connection h8sm76342lbd.48 - gsmtp === Connection closed with remote host. 

YAHOO: TLS aka SMTPS, tlsv1.2

 $ echo "Hello world" | swaks -4 --server smtp.mail.yahoo.com:465 --from user@yahoo.com --to user@gmail.com --tlsc --tls-protocol tlsv1_2 --auth PLAIN --auth-user user@yahoo.com --auth-password 7654321 --h-Subject "Test message" --body - === Trying smtp.mail.yahoo.com:465... === Connected to smtp.mail.yahoo.com. === TLS started with cipher TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128 === TLS no local certificate set === TLS peer DN="/C=US/ST=California/L=Sunnyvale/O=Yahoo Inc./OU=Information Technology/CN=smtp.mail.yahoo.com" <~ 220 smtp.mail.yahoo.com ESMTP ready ~> EHLO www.example.net <~ 250-smtp.mail.yahoo.com <~ 250-PIPELINING <~ 250-SIZE 41697280 <~ 250-8 BITMIME <~ 250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE ~> AUTH PLAIN AGFhQxsZXguaGhMGdATGV4X2hoYtYWlsLmNvbQBS9TU1MjQ= <~ 235 2.0.0 OK ~> MAIL FROM:<user@yahoo.com> <~ 250 OK , completed ~> RCPT TO:<user@gmail.com> <~ 250 OK , completed ~> DATA <~ 354 Start Mail. End with CRLF.CRLF ~> Date: Wed, 17 Feb 2016 10:08:28 +0000 ~> To: user@gmail.com ~> From: user@yahoo.com ~> Subject: Test message ~> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/ ~> ~> Hello world ~> ~> ~> . <~ 250 OK , completed ~> QUIT <~ 221 Service Closing transmission === Connection closed with remote host. 

I have been using swaks to send email notifications from nagios via gmail for the last 5 years without any problems.

+1
Feb 17 '16 at 10:30
source share



All Articles