How to setup Sendmail as a smarthost on Ubuntu LTS
Posted in "News"Ubuntu mail sendmail
Some Internet service providers (ISP) do not allow you to have a working mail server due to anti-spam policies, they block all outgoing traffic on port 25.
However, you can use the mail server of your ISP to send your mails. In this article I will describe how to set up sendmail as a smarthost.
1. Install sendmail.
First we need to install sendmail:
sudo apt install sendmail
Just answer the default prompts to finish your installation.
2. Configuring sendmail as a smarthost.
Open your sendmail config using nano:
sudo nano /etc/mail/sendmail.mc
Find the line that starts with MAILER_DEFINITIONS and add following lines below:
As a smarthost you need to use the outgoing mailserver of your ISP, see the example below:
To use the configuration changes you have made, you need to regenerate the sendmail.cf:
sudo bash -c "cd /etc/mail/ && m4 sendmail.mc > sendmail.cf"
Now restart Sendmail:
sudo systemctl restart sendmail.service
Send a mail from terminal to check if your configuration works, replace email@domain with your email address and hit CTRL + D to send the mail:
mail -s "Subject" firstname.lastname@example.org
You can check your mail log for errors:
sudo tail -400 /var/log/mail.log
3. Send mails from PHP.
If you have a webserver running on your Ubuntu installation and you want to send mails from PHP, you need to make some changes to the php.ini file to make it work! Open the php configuration (php.ini *):
sudo nano /etc/php5/apache2/php.ini
* Your php.ini location depends on your PHP version!
Now find the line that contains the sendmail arguments (search for "sendmail" using CTRL + W in nano) and change sendmail path = to this:
sendmail_path = /usr/sbin/sendmail -t -i -f root@<yourdomain.ext>
sendmail_path = /usr/sbin/sendmail -t -i -f email@example.com
Save the changes (CTRL + X) and restart apache:
sudo systemctl restart apache2.service
You can check if mails are delivered to the configured "smarthost" by viewing the mail log:
sudo tail -f /var/log/mail.log