How to send an Email with SMTP
Learn how to send an email with SMTP. This tutorial uses Telnet so you can see how an SMTP server requests and responds.
In most other use cases, you would send email with the Twilio SendGrid console or the Twilio SendGrid API.
The Simple Mail Transfer Protocol (SMTP) provides the language that transmits email from one server to another. Twilio SendGrid provides an SMTP service to deliver your email using its servers.
To set custom email handling instructions, use a JSON-encoded list called the X-SMTPAPI header in the Twilio SendGrid SMTP API. To modify your message in the ways you specify, Twilio SendGrid parses the X-SMTPAPI header.
To learn more about SMTP, see the SMTP Service Crash Course in the Twilio blog.
Before you start this tutorial, complete the following prerequisites.
Placeholder variable notation
The following code samples wrap placeholder values in angle brackets (<YOUR_API_KEY>
). Replace the brackets and the text they contain with an actual value.
For example, if your API key is SG.someactualkey
, change echo -n '<YOUR_API_KEY>' | openssl base64
to echo -n 'SG.someactualkey' | openssl base64
.
In some cases, a placeholder gets wrapped in two sets of angle brackets. In this case, Twilio SendGrid requires the outer set of brackets in the actual value and you shouldn't replace them.
For example, if the example displays From: "Example" <<[email protected]>>
, change your value to From: "Example" <[email protected]>
.
-
Install OpenSSL.
- OpenSSL comes installed on macOS and Linux.
- If you use Windows, download and run the official installer.
-
Install
telnet
. See the PuTTY for Windows, macOS Homebrew formula, or Linux instructions. -
Sign up for a SendGrid account
-
Create and store a SendGrid API key with full access "Mail Send" permissions.
-
Open your terminal, command prompt, or command line.
-
Encode your API key using Base64, use the following command.
echo -n '<YOUR_API_KEY>' | openssl base64Never convert your API key using an external webpage, always convert it in your terminal with OpenSSL.
-
Save your encoded key for later.
Check that the API key doesn't include any newline or whitespace characters by accident. This can happen when copying the encoded key from a shell that line wraps output. SMTP uses line breaks to end commands, so Linefeed characters prevent successful authentication. -
Verify your Sender Identity.
To see how the SMTP server requests and responds, use Telnet.
Danger
Telnet doesn't register backspaces. Type your commands correctly or copy and paste them from this tutorial.
-
Type the following in the terminal and start a Telnet session:
telnet smtp.sendgrid.net 587Use port
587
. This should avoid rate limiting or connection blocking from ISPs and hosting providers. -
After connecting to Twilio SendGrid, log in to the mail service using the following SMTP command:
AUTH LOGINThe mail server responds with
334 VXNlcm5hbWU6
, a Base64-encoded request for your username. -
Type
YXBpa2V5
and press Enter on your keyboard.
Twilio SendGrid authenticates using an API key, so it expectsapikey
instead of your account username. When Base64-encoded,apikey
becomesYXBpa2V5
.The mail server responds with
334 UGFzc3dvcmQ6
, a Base64-encoded request for your API Key as a password. -
Enter your Base64-converted API key in the next line as the password and press Enter.
The mail server responds with
235 Authentication successful
. This indicates an open connection tosmtp.sendgrid.net
on port587
and that you have a valid API key. -
Add the email that you're sending from using the SMTP
MAIL FROM
command and press Enter.MAIL FROM: <SENDER_EMAIL>The mail server responds with
250 Sender address accepted
. -
Type the SMTP
RCPT TO
command, then the recipient email address, and press Enter.RCPT TO: <RECIPIENT_ADDRESS>To add more recipient addresses, type
RCPT TO <RECIPIENT_ADDRESS>
and press Enter for each recipient.After each recipient, the mail server should respond with
250 Recipient address accepted
. -
Type
DATA
and press Enter.The mail server responds with
354 Continue
. Unlike theMAIL FROM
andRCPT TO
commands, which define the email envelope and route your message to the recipient, theDATA
command modifies the content of your message. -
You can add a
mail-to
header to add the name and email address of the recipient to the email header and press Enter.
Note: Wrap the name in quotation marks ("
) and the address in angle brackets (<
,>
).To: "<RecipientName>" <<RecipientEmailAddress>> -
Add a
From
header to add the name and email address of the sender to the email header and press Enter.(warning)SMTP requires a From header
If you omit a
From
header, Twilio SendGrid blocks your email as it violates RFC 5322.From: "<SenderName>" <<SenderEmail>> -
Add a
Subject
line and press Enter.Subject: <EMAIL_SUBJECT> -
Press Enter to start the body of your message.
-
Type the body content then press Enter.
"<MESSAGE>"For example:
"This is a test for the SMTP relay." -
Type a period (
.
) as the signal to end the email body, then press Enter. This sends the email.The mail server returns
250 Ok: queued as <examplestring1234>
. The mail server has put this email into the send queue. This queue clears quickly. The mail should deliver to the designated recipients in a short time. -
To exit the Telnet connection, type
quit
and press Enter.
The following example shows the all user inputs and SMTP server responses.
1235 Authentication successful2MAIL FROM:[email protected]3250 Sender address accepted4RCPT TO:[email protected]5250 Recipient address accepted6DATA7354 Continue8910Subject: Test message subject1112"This is the test message body."13.14250 Ok: queued as Yo60h6C5ScGPeP5fUWU3K
Save time and feel confident in your long-term success with Email Implementation. Our experts work as an extension of your team to ensure your email program is correctly set up and delivering value for your business.