Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email using simple SMTP commands via Gmail?

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:

enter image description here

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:

enter image description here

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?

like image 791
Saeed Neamati Avatar asked Jun 15 '12 07:06

Saeed Neamati


People also ask

How do I use Gmail SMTP server to send emails?

Set up the app or device with the Gmail SMTP serverOn your device or in the app, enter smtp.gmail.com as the server address. In the Port field, enter one of the following numbers: If you're using SSL, enter 465. If you're using TLS, enter 587.

Is Google SMTP free?

The SMTP server for Gmail is a free SMTP server that anyone across the globe can use. It allows you to manage email transactions from your Gmail account via email clients or web applications. Email clients are user-end mail applications. Some of the most popular ones are Thunderbird, Outlook, and Mac Mail.

Does Gmail allow SMTP relay?

There are different per-user sending limits for sending email with Gmail, rather than SMTP relay. The SMTP relay and Gmail user sending limits are independent and are counted separately from each other. For details about Gmail sending limits, visit Gmail sending limits in Google Workspace.


1 Answers

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:

echo -ne '\[email protected]\00password' | base64 AHVzZXJAZ21haWwuY29tAHBhc3N3b3Jk 

then continue with "mail from:" like in your example

example session:

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: <[email protected]> 250 2.1.0 OK m46sm11546481eeh.9 rcpt to: <[email protected]> 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 
like image 75
Gryphius Avatar answered Sep 22 '22 12:09

Gryphius