In today’s digital world, effective and reliable email communication is essential for businesses and individuals alike. Whether you’re sending transactional emails, marketing campaigns, or personal messages, having a robust and efficient system in place is crucial. That’s where SendGrid comes in.
As one of the leading cloud-based email delivery platforms, SendGrid offers a range of powerful features, including the ability to act as an SMTP relay. In this article, we’ll walk you through the steps required to set up SendGrid as your trusted SMTP relay, allowing you to enhance the deliverability and scalability of your email communications.
Get ready to unlock the full potential of SendGrid and revolutionize your email-sending experience. Let’s dive in!
Table of Contents
Requirements
- A SendGrid account. You can sign up for free if you don’t have one yet.
Step 1: Verify Your Domain in SendGrid
Before you can fully utilize SendGrid as an SMTP relay, it’s important to verify your domain within the platform. This verification process helps ensure that your email campaigns are sent securely and that your messages have a higher chance of reaching the recipients’ inboxes than getting marked as spam.
- Open the SendGrid sign-in page and log in with your credentials. Expand Settings and click Sender Authentication.
- Under the Sender Authentication, you have two options for sender identity:
- Domain Authentication — Verify a whole domain. Once verified, any sender email address from that domain will be a valid sender. This option requires adding records in your public DNS host.
- Single Sender Verification — Verify a single email address. Once verified, only this email address will be valid as the email sender.
In this example, we’ll focus on domain authentication. To do that, click Authenticate Your Domain.
- You will be asked two questions and are not required to answer them. For this example, don’t change anything and click Next.
If you answer the first question (Which Domain Name Server (DNS) host do you use?), it will give you an automatic DNS configuration option.
- On the next step, enter the domain you will be sending messages from and click Next. Ensure that you can prove ownership of this domain.
- You will be given the DNS records you must create in your DNS host.
In this example, I added the DNS records in Cloudflare, the DNS host I am using for my domain.
- Once you added these records, check the “I’ve added these records.” box and click Verify.
- Suppose you’ve correctly configured the DNS records; the sender domain should be verified successfully, similar to the screenshot below. Click the “Return to Sender Authentication” button.
Your domain will now be listed with verified status.
Step 2: Generate the API Key
After successfully verifying your domain in SendGrid, the next step is to generate an API key. This API key will grant you access to SendGrid’s powerful email delivery features and enable you to send emails through the SMTP relay. Follow the steps below to generate your API key:
- From the dropdown menu, click “API Keys.”
- Click the “Create API Key” button on the API Keys page.
- Give your API key a descriptive name that helps you identify its purpose. For example, you can name it “SMTP Relay Key” or anything meaningful.
- Select “Restricted Access,” turn on the “Mail Send” permission, and click the “Create and View” button.
- SendGrid will generate an API key for you. Make sure to copy and securely store this key, as it will be required for authenticating your SMTP relay requests.
You can now view and manage your API keys from the API Keys page in your SendGrid account. Note that for security purposes, SendGrid will only display the API key to you once. If you lose it, you’ll need to generate a new one.
Congratulations! You have successfully generated your API key. This key will bridge your application or system and SendGrid, allowing you to send emails through the SMTP relay. In the next section, we’ll dive into the configuration process to integrate SendGrid as your SMTP relay and start sending emails seamlessly.
Step 3: Use SendGrid as SMTP Relay
Now that you have verified your domain and generated the API key, it’s time to configure SendGrid as your SMTP relay. By integrating SendGrid with your application or system, you can send emails efficiently and reliably.
The table below shows the SendGrid SMTP relay settings:
Server | smtp.sendgrid.net |
---|---|
Username | apikey |
Password | Your SendGrid API key |
Port (TLS) | 25, 2525, or 587 |
Port (SSL) | 465 |
SendGrid SMTP Relay Example: PowerShell (Send-MailMessage)
Related. Send emails using the send MailMessage PowerShell cmdlet.
This code prompts the user for their SendGrid credentials, creates an email message with specific details, and then sends the email using the SendGrid SMTP relay server.
In this example, the email is sent from “mailer@lazyexchangeadmin.cyou” to “june@icuc.ml” with the subject and body specified in the code.
$sendGridCredential = Get-Credential $mailMessage = @{ SmtpServer = 'smtp.sendgrid.net' Port = 587 Credential = $sendGridCredential From = 'mailer@lazyexchangeadmin.cyou' To = 'june@icuc.ml' Subject = 'SendGrid SMTP Relay Example with PowerShell' Body = 'SendGrid SMTP Relay Example with PowerShell' } Send-MailMessage @mailMessage
And the email should be delivered successfully.
SendGrid SMTP Relay Example: IIS SMTP Server
You can also use SendGrid as the SMTP relay or smart host in your IIS SMTP Server. The setting is similar to the screenshots below.
SendGrid SMTP Relay Example: HMailServer
If you have HMailServer, below is the screenshot of how you can configure it to use SendGrid as the smart host.
SendGrid SMTP Relay Example: Postfix
Using SendGrid as SMTP relay in Postfix is possible but involves a more meticulous configuration.
- Create a new file called /etc/postfix/sendgrid_passwd on your Postfix server and open it in your preferred text editor.
sudo nano /etc/postfix/sendgrid_passwd
- Copy the code below and paste it into your editor. Replace API_KEY_VALUE with your actual API key.
[smtp.sendgrid.net]:587 apikey:API_KEY_VALUE
- Save and close the /etc/postfix/sendgrid_passwd file.
- Open the Postfix configuration file in the text editor.
sudo nano /etc/postfix/main.cf
- Delete the existing “relayhost =” line.
- Scroll to the bottom of the file and paste the following lines.
smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sendgrid_passwd smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous smtp_tls_security_level = encrypt header_size_limit = 4096000 relayhost = [smtp.sendgrid.net]:587
Let’s go through each parameter:
- smtp_sasl_auth_enable = yes: This line enables SASL (Simple Authentication and Security Layer) authentication for SMTP (Simple Mail Transfer Protocol). It allows Postfix to authenticate with the mail server before sending outgoing emails.
- smtp_sasl_password_maps = hash:/etc/postfix/sendgrid_passwd: This line specifies the location of the password map file used for SASL authentication. In this case, the file is located at /etc/postfix/sendgrid_passwd and is expected to be in a hashed format.
- smtp_sasl_security_options = noanonymous: This parameter sets the SASL security options. Specifically, it disables the “anonymous” authentication method, ensuring that authentication is required when connecting to the mail server.
- smtp_sasl_tls_security_options = noanonymous: Similar to the previous parameter, this line sets the security options for SASL TLS (Transport Layer Security). It also disables anonymous authentication over TLS connections.
- smtp_tls_security_level = encrypt: This parameter specifies the security level for TLS encryption when communicating with the mail server. In this case, it is set to “encrypt,” meaning all connections with the server should be encrypted using TLS.
- header_size_limit = 4096000: This line specifies the maximum allowed size for email headers in bytes. In this case, the limit is set to 4,096,000 bytes (or 4 MB), the maximum size headers can occupy in an email.
- relayhost = [smtp.sendgrid.net]:587: This parameter specifies the relay host (SMTP server) that Postfix should use to forward outgoing emails. In this case, it is set to [smtp.sendgrid.net]:587, indicating that SendGrid’s SMTP server at smtp.sendgrid.net should be used as the relay host, and communication should take place over port 587.
Your Postfix configuration will look similar to the screenshot below.
- Save and close the text editor.
- Run the following commands to create a lookup table for SASL passwords and restart the Postfix mail server:
sudo postmap /etc/postfix/sasl_passwd sudo systemctl restart postfix
- Now, test the outbound email delivery by running this command. Ensure to replace the sender and recipient addresses accordingly:
echo "SendGrid SMTP Relay Example with Postfix" | \ mail -s "SendGrid SMTP Relay Example with Postfix" -a \ "From: mailer@lazyexchangeadmin.cyou" june@icuc.ml
- Check the mail log to see what happens with the test email.
tail -f /var/log/mail.log
According to the result, the message was relayed to the SendGrid SMTP relay.
- Check if the message reached the intended recipient’s mailbox.
- If you inspect the message header (i.e., Message Header Analyzer, Email Header Analyzer), you’ll see that the email came from your Postfix server and through the SendGrid SMTP relay.
Congratulations! You have successfully configured your application or system to use SendGrid as your SMTP relay.
Conclusion
This post provided a comprehensive guide on configuring SendGrid as an SMTP relay. We covered the initial setup process, including verifying your domain and configuring DNS settings.
We also explored the necessary steps to integrate SendGrid with popular email platforms HMailServer, IIS SMTP server, and Postfix.
Thank you for sticking with us until the end. We hope that what you learned in this tutorial will help you get started in using SendGrid as SMTP relay for your apps, systems, and services.
1 comment
Hi June,
Thanks so much for this tutorial!
It’s amazing.
Just a quick comment. Step 8 only works for me when I run:
sudo postmap /etc/postfix/sendgrid_passwd
I don’t have a sasl_passwd in my postfix directory.