In this post we will setup Postfix to Relay Mail through SendGrid and we will also configure the authentication as SendGrid is not an open relay, but you can obtain credentials by signing up with the for a free account to obtain your username and password which will use to relay mail through them.
Access Control on Postfix
For this demonstration we can make use of the mynetworks configuration to specify the cidr of the source which we want to allow clients to be able to relay from. This is a acceptable way of controlling which source addresses you would like to authorize to relay mail via your smtp relay server.
Sendgrid
Sendgrid offers 100 free outbound emails per day, sign up with them via sendgrid.com/free, create a API Key and save your credentials in a safe place.
You first need to verify your account by sending a mail using their API, but it’s step by step so won’t take more than 2 minutes to complete.
Setup Postifx
I will be using ubuntu to setup postfix and configure postfix to specify sendgrid as the relayhost and also configure the authentication for the destination server in question:
1
|
|
Configure postfix to relay all outbound mail via sendgrid, enable sasl auth, tls, relayhost etc via /etc/postfix/main.cf
. The settings that needs to be set/configured:
1 2 3 4 5 6 7 8 |
|
Create the /etc/postfix/mynetworks
file where the whitelisted source addresses will be specified. In our case the loopback address and the class c subnet 10.0.1.0 :
1 2 |
|
Create the credential file where the credentials for the sendgrid service will be stored, in my case it will be in /etc/postfix/sasl_passwd
:
1
|
|
Apply permissions and update postfix hashtables on the file in question:
1 2 |
|
Enable and Start the Service:
1 2 |
|
Send a Test Mail
From the server you can test your mail delivery by sending a mail:
1
|
|
or using telnet for a remote system:
1 2 3 4 5 6 7 8 9 10 11 |
|
You can monitor /var/log/maillog
to see log messages of your email.