- All updates installed:
sudo apt update
- Install the sendmail package:
sudo apt-get install sendmail
- Configure sendmail: You need to choose Yes to every question or otherwise configure it as you see fit.
sudo sendmailconfig
- 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
- 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
- Make sure the libsasl2-modules package is installed and up to date:
sudo apt-get install libsasl2-modules
- 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.
- 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
- 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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
- 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
- Restart Postfix:
sudo service postfix restart
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 protected]" [email protected]
Or create a file php with mail() function and run.
<?php
mail('[email protected]', 'test', 'you done that');
echo 'ok';
?>
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!