SendGrid is a cloud mail delivery service for mail delivery.
Reasons to use SendGrid
That's why we use SendGrid. SendGrid is a cloud service for mail delivery, and if you exceed 12,000 emails a month, you will be charged a monthly fee.
If you want to deliver mail for free, you can use postfix to set up an SMTP server and send it using sendmail.
However, you will run into problems such as not being able to send emails via SMTP on port 25 on the cloud server, increasing the number of emails delivered, and not wanting to be blacklisted.
I can't send mail via SMTP on port 25 on the cloud server
As a public cloud server configuration, if you are dynamically showing your IP address to the outside world, your email will not arrive if the selected IP address is blacklisted.
Due to such an unexpected problem, it may be prohibited to send mail from port 25 by default.
In such cases, you can connect to SengGrid using SendGrid and the Web API of mail, or the relay function of postfix.
When migrating from an existing system, it seems that the relay function can be used to migrate without modifying the application.
The number of emails increases and it takes time to process
If the number of emails is increasing and it takes a long time to process, SendGrid seems to finish the process faster.
"We were able to significantly reduce the time it took to send an e-mail. It took about 8 hours to send e-mail newsletters to about 160,000 recipients, but it took 3 to 4 minutes after we started using SendGrid. I came to do it. "
The experience story is written.
I don't want to be blacklisted
If the IP address of the sender of the email is blacklisted, the email will not be delivered.
With SendGrid, even if such a thing happens inside SendGrid, it seems that SendGrid will solve it and achieve a high arrival rate.
The price is as follows.
From the top page, enter your email address to apply.
After the review, you will receive an email with this approval.
SendGrid information required to send Web API and relay mail
The SendGrid information required to send Web API and relay emails is the API key. (Before 2021, the user name and password were necessary information, but from the viewpoint of preventing junk mail, it seems that the API key is required.)
Get the API key
Get your API key. Log in to the SendGrid management screen. Click "Settings" in the left sidebar. Click "API keys".
Click "Create API Key" in the upper right.
Enter any name in "API Key name". (Example) perlgenki.
Change "API Key Permissions" to "Full Access".
The created API key is displayed. It will only be displayed once, so copy and paste it and save it somewhere. If you turn off the screen, delete it and recreate it.
Link SendGrid and Postfix
SendGrid has a Web API, but you can also work with Postfix to connect sending mail from the sendmail command to SendGrid.
The advantage of this method is that you choose not to use SendGrid and you don't have to make any changes to the application that sends the email. You can abstract your application.
The disadvantage is that you need to install and configure Postfix.
(Since the GUI screen appears for the installation of Postfix on Ubuntu, I still don't know if the installation can be automated.)
SendGrid official documentation
The SendGrid documentation is detailed for detailed instructions.
Basic documentation. If you want to start anew, follow the steps in "SendGrid Documents --Postfix".
Below, you will find useful information for mail delivery.
Procedure for linking SendGrid and Postfix
Edit the Postfix configuration file.
sudo vi /etc/postfix/main.cf
Add the following settings. Using the SMTP-Auth protocol, Postfix asks SendGrid to deliver the mail.
I'll write the steps here, quoting from the official documentation above. (Quoted below, as of June 2, 2020)
Open the Postfix configuration file. Usually it is /etc/postfix/main.cf. Then rewrite it as follows. You can delete the other contents.
smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash: / etc / postfix / sasl_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
Now create the password file described in the settings above.
sudo vi / etc / postfix / sasl_passwd
Specify the API key obtained by SendGrid. Replace the API key part with the obtained API key.
[smtp.sendgrid.net]: 587 apikey: API key
Then make sure that this file is restricted to read and write only by the root account, and use the postmap command to update the hashtable for Postfix to use this new file:
sudo chmod 600 / etc / postfix / sasl_passwd sudo postmap / etc / postfix / sasl_passwd
Finally restart Postfix with systemctl:
sudo systemctl restart postfix
Send an email with the sendmail command
Let's send an email with the sendmail command. If there is no sender, the email has been rejected, so "-f" is used to specify the sender's email address.
echo Hello | sendmail -f email@example.com firstname.lastname@example.org
If you can send an email, the settings are correct.
If you haven't been able to send it, use the mailq command to first look at the mail queue.
If the queue says send failed, it has failed.
To see the Postfix systemd logs, use the journalctl command. Specify the unit "postfix" with the "-u" option. Please note that you will not be able to see the Postfix logs unless you run it with sudo. If you want to see only the last part of the log, combine the "-r" option.
sudo journalctl -r -u postfix
This is a sample Postfix log.
--Logs begin at Mon 2020-06-01 15:51:16 JST, end at Thu 2020-07-23 14:44:11 JST. - Jul 22 13:24:35 shinshina-development-app-00000001 systemd : Started PostfixMail Transport Agent. Jul 22 13:24:35 shinshina-development-app-00000001 systemd : Starting Postfix Mail Transport Agent ... --Reboot - Jul 22 13:13:47 shinshina-development-app-00000001 systemd : Stopped Postfix Mail Transport Agent.