Before starting this tutorial, you should have:
- Debian 7 installed
- Your fully qualified domain name (FQDN)
- All updates installed :
A valid username and password for the SMTP mail provider, such as Mandrill, or SendGrid
Make sure the libsasl2-modules package is installed and up to date:
apt-get install libsasl2-modules
In this section, you will install Postfix and set the domain and hostname. Install Postfix with the following command:
apt-get install postfix
During the installation, a prompt will appear asking for your General type of mail configuration.
Select Internet Site. Enter the fully qualified name of your domain, fqdn.example.com. Once the installation is finished, open the /etc/postfix/main.cf file with your favorite text editor:
Make sure that the myhostname parameter is configured with your server’s FQDN:
/etc/postfix/main.cf myhostname = fqdn.example.com
Configuring SMTP Usernames and Passwords
Usernames and passwords are generally stored in a file called sasl_passwd in the /etc/postfix/directory. In this section, you’ll add your external mail provider credentials to this file and to Postfix.
Open or create the /etc/postfix/sasl_passwd file, using your favorite text editor:
Add your destination (Gmail SMTP Host), username, and password in the following format including third brackets :):
Create the hash db file for Postfix by running the postmap command:
If all went well, you should have a new file named sasl_passwd.db in the /etc/postfix/ directory.
Securing Your Password and Hash Database Files
The /etc/postfix/sasl_passwd and the /etc/postfix/sasl_passwd.db files created in the previous steps contain your SMTP credentials in plain text.
For security reasons, you should change their permissions so that only the root user can read or write to the file. Run the following commands to change the ownership to root and update the permissions for the two files:
chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
Configuring the Relay Server
In this section, you will configure the /etc/postfix/main.cf file to use the external SMTP server.
Open the /etc/postfix/main.cf file with your favorite text editor:
Update the relayhost parameter to show Gmail SMTP relay host. Important: If you specified a non-default TCP port in the sasl_passwd file, then you must use the same port when configuring the relayhost parameter in /etc/postfix/main.cf
# specify SMTP relay host relayhost = [smtp.gmail.com]:587
At the end of the file, add the following parameters to enable authentication:
# enable SASL authentication smtp_sasl_auth_enable = yes # disallow methods that allow anonymous authentication. smtp_sasl_security_options = noanonymous # where to find sasl_passwd smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd # Enable STARTTLS encryption smtp_use_tls = yes # where to find CA certificates smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Save your changes and restart Postfix:
service postfix restart
Enable “Less Secure Apps” In Gmail
By default, only the most secure sign-ins, such as logging in to Gmail on the web, are allowed for your Gmail account. To permit relay requests, log in to your Gmail account and turn on Allow less secure apps.
For more information, review the Google Support document “Allowing less secure apps to access your account.”
Allow Other Mailserver to use this system as a gateway
To allow postfix accept email relaying from a specific ip adress, we can edit /etc/postfix/main.cf:
mynetworks = 127.0.0.0/8
mynetworks = 127.0.0.0/8, a.b.c.d/e
where a.b.c.d/e is the new group of ip address, or you can simply add one ip address in it. Restart postfix and now postfix will consider a.b.c.d/e as trusted ip and allow email from this ip address relaying email using this postfix mail server.
The fastest way to test your configuration is to send an email to any unrelated email address, using the mail command:
echo "body of your email" | mail -s "This is a Subject" -a "From: email@example.com" firstname.lastname@example.org