Skip to content

Instantly share code, notes, and snippets.

@garyrojas
Last active April 20, 2020 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garyrojas/12ce44ba6e157bd454b80da2cf92d27d to your computer and use it in GitHub Desktop.
Save garyrojas/12ce44ba6e157bd454b80da2cf92d27d to your computer and use it in GitHub Desktop.

Config SMTP Postfix (DEBIAN+SENDGRID)

Enable PHP Mail

  1. All updates installed:
sudo apt update
  1. Install the sendmail package:
sudo apt-get install sendmail
  1. Configure sendmail: You need to choose Yes to every question or otherwise configure it as you see fit.
sudo sendmailconfig
  1. Edit hosts file
sudo vim /etc/hosts
  • NOTE: You would define 127.0.0.1 my_ip localhost myhostname eg:
127.0.0.1   172.18.0.6  localhost   65e8408b426c5
  1. Define your path mail at php.ini
  • Get your php.ini path
php -i | grep "Loaded Configuration File"
  • Edit your php.ini
vi /usr/local/etc/php/php.ini
sendmail_path= /usr/sbin/sendmail -t -i
  1. Make sure the libsasl2-modules package is installed and up to date:
sudo apt-get install libsasl2-modules

Installing Postfix

  1. Install Postfix with the following command:
sudo 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.

  1. Find your Postfix config file, typically /etc/postfix/main.cf, and add the following:
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
  1. Now you need to specify your credentials (optionally, use apikey as username and an API Key as password) in the separate file /etc/postfix/sasl_passwd (you'll likely need to create it):
[smtp.sendgrid.net]:587 yourSendGridUsername:yourSendGridPassword
  • If you will use the API access then you need take this format
[smtp.sendgrid.net]:587 apikey:YOUR_API_KEY_HERE
  • Would look like this
root@honVm:~# cat /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 apikey: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
  1. Next, make sure the file has restricted read and write access only for root, and use the postmap command to update Postfix's hashtables to use this new file:
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
  1. Restart Postfix:
sudo service postfix restart

Testing

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: you@example.com" recipient@elsewhere.com

Or create a file php with mail() function and run.

<?php 
   mail('myWorkingEmail@example.com', 'test', 'you done that');
   echo 'ok';
?>

Note

If you restart the service again with another access then is possible you have this message:

[....] Starting Postfix Mail Transport Agent: postfixpostfix/postfix-script: fatal: the Postfix mail system is already running
 failed!
  • Cause of the problem

Maybe it's postfix. The last time you forced the shutdown, you did not delete the lock itself. Lock exists at startup and cannot be run.

  • Solution

The 1.Postfix master PID file (that is, the lock) also exists under the /var/spool/postfix/pid directory. Enter the directory to delete the file:

sudo ls /var/spool/postfix/pid
sudo rm /var/spool/postfix/pid/master.pid
sudo service postfix restart

Listo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment