DEV Community

imrinzzzz
imrinzzzz

Posted on

Sending email using SMTP via Gmail with OpenSSL

Hi~ It's been a long time. So many things have changed over the years, but recently, I was tasked to send an email with an HTML body and an attachment. Sounds simple enough until you have no idea how to test your "raw email" and you are tired of making changes, opening a PR, merging to master, deploying it to staging before you can test if your little raw email works. I mean AWS SES isn't very friendly for local test after all.

TDLR: how to send raw email via Gmail using OpenSSL

Some technical background

Introducing OpenSSL

OpenSSL is an open source cryptographic toolkit that facilitates secure communications between endpoints on a network. The toolkit includes three core components: the libcrypto library, the libssl library and a command-line utility for performing cryptographic tasks.[1]

It's helpful with a lot of things, but we're going to use the s_client command. It implements a generic SSL/TLS client which connects to a remote host using SSL/TLS.[2]

Installing OpenSSL

(P.S. this is kinda oversimplifying the process and it might not always work for you)

Linux (with apt, but generally installed by default)

sudo apt install openssl
Enter fullscreen mode Exit fullscreen mode

Mac (with brew)

brew install openssl
Enter fullscreen mode Exit fullscreen mode

Tutorial fr

ok, so here's the part you came for.

  • connect to gmail openssl socket
openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof
Enter fullscreen mode Exit fullscreen mode
  • now say hi to the server
HELO smtp.gmail.com 
Enter fullscreen mode Exit fullscreen mode
  • then authenticate yourself
auth login
Enter fullscreen mode Exit fullscreen mode

You'll get 334 VXNlcm5hbWU6 which basically is "Username: " in base64. Now you'll type in your username, aka gmail address, but encoded in base64.

Then it'll return 334 UGFzc3dvcmQ6 which is "Password: " in base64, so just type your account password. But if you have 2fa on (which you should btw), you can use a password from app password.

An app password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. App passwords can only be used with accounts that have 2-Step Verification turned on.

  • now to the sending email part, type
MAIL FROM: <email@address.com>
Enter fullscreen mode Exit fullscreen mode

press enter. (If the sender isn't permitted to send an email, you'll get an error.)

rcpt to: <also_email@address.com>
Enter fullscreen mode Exit fullscreen mode

then enter. (If the recipient isn't valid, you guessed it, you get an error.)

DATA
Enter fullscreen mode Exit fullscreen mode

And voilà! we can start sending email now! To show you an example of what I kinda drafted, this is what I sent.

Subject: test raw email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d

--25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d
Content-Type: multipart/alternative; boundary=sub-25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d

--sub-25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>Hello!</h1>
<p>Please see the attached file for a list of customers to contact.</p>
</body>
</html>


--sub-25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d--

--25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d
Content-Type: application/pdf; name="some_pdf.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="some_pdf.pdf"

this-should-be-some-pdf-encoded-in-base64

--25b40e96420e3083793e9792f0ef83d50ba5a07f5d413e3eab5002dfdb3d--
Enter fullscreen mode Exit fullscreen mode

**Mind you, the newline and -- thingy are sooo important. I spent several days scratching my head as to why it just wouldn't work because of them.

  • then if you're content and want to send your email, simply enter, type . and it's done!

  • you can repeat from MAIL FROM again and again until you're done with your life testing. To exit the openssl connection, you can just simply wait until it kicks you out or you can type

quit
Enter fullscreen mode Exit fullscreen mode

Closing and sources

Of course, I didn't just know this. I looked up several places and these are my saviours: an answer from stackexchange, an answer from stackoverflow, like who'd have guessed, right? (shout out to the comments under this answer too!), and yet another answer from stackoverflow.

Also, thank www.base64decode.org for providing a noble service of base64 encoding/decoding.

Annnnd, that is all for this post. Hope this helps! (I know it will help me again in the future lol)

Top comments (0)