Install and configure Postfix on Ubuntu 16.04 with Sendgrid as smarthost

SendGrid is cloud-based solution sends transactional and marketing emails instead of our email infrastructure.

How to use SendGrid for delivery emails to your customers.

We will use Postfix

1. Install postfix and client for console

Go to console and install postfix:

$sudo apt-get update
$sudo apt-get install mailutils

it will install few necessary packages

When ask you what configuration do you need, just select Num 5 - Local Only.

---------------------
Please select the mail server configuration type that best meets your needs.
No configuration:
Should be chosen to leave the current configuration unchanged.
Internet site:
Mail is sent and received directly using SMTP.
Internet with smarthost:
Mail is received directly using SMTP or by running a utility such
as fetchmail. Outgoing mail is sent using a smarthost.
Satellite system:
All mail is sent to another machine, called a 'smarthost', for delivery.
Local only:
The only delivered mail is the mail for local users. There is no network.
1. No configuration 2. Internet Site 3. Internet with smarthost 4. Satellite system 5. Local only
General type of mail configuration:
---------------------

select 5 , we will modify postfix config file later

2. Getting API Key 

Goto sendgrid.com, login and goto Settings - > API Keys.

We need generate API Key for our host

Push Button "Create API Key"

Select "Restricted Access" - we need just send mails

Email Send - move bar to right

and Create

Api key will show ones and will not show again, just keep key in clipboard or to some safe place.

Click "Done" and go to terminal.

We need create file 

$touch /etc/postfix/sasl_passwd

with content:

[smtp.sendgrid.net]:587 yourSendGridUsername:yourSendGridPassword

- use 'apikey' as your username

- use your password as your API from previos step

[smtp.sendgrid.net]:587 apikey:ssdfsejsoudfs<fake apikey>7f98s7d9f8s6d9fsd

Note, put your correct api key

3. Configure Postfix

Edit main.cf

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
relayhost = [smtp.sendgrid.net]:587

modify permission of this file, need access only for root and update postfix hashtable to use this file 

$ sudo chmod 600 /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd

and restart postfix:

$ sudo systemctl restart postfix

Check send email to real recipient address:

$mail -s 'test message' <your email>@gmail.com
Cc:
test message

Push Ctrl+D to send message.

Check log:

$tailf /var/log/mail.log

Dec 3 11:00:47 web1 postfix/qmgr[21664]: CD5C41AA039F: from=<developer@your server.com>, size=379, nrcpt=1 (queue active)
Dec 3 11:00:47 web1 postfix/smtp[27502]: CD5C41AA039F: to=<anyour [email protected]>, relay=smtp.sendgrid.net[167.89.115.18]:587, delay=0.1, delays=0.01/0.01/0.07/0.01, dsn=2.0.0, status=sent (250 Ok: queued as hus4UVF-QkqTM8e26rf0mw)
Dec 3 11:00:47 web1 postfix/qmgr[21664]: CD5C41AA039F: removed

Alright

Now your server/Web App can send any notifications to your customers through SendGrid

Links:

https://sendgrid.com/docs/for-developers/sending-email/postfix/

Current rating: 5