It is very easy to use msmtp
package, configure a dedicated email account for the server and send email alerts from the server or the web application, such as WordPress. However, for making web applications such as WordPress work, you need to perform a few extra steps which we have not described in this article.
To follow this guide, you need a dedicated email account for this purpose. If you are using paid hosted email for a domain, such as we use for our official emails, avoid using them since there is a remote change of getting the email blacklisted or hacked. You can use an email address such as webserver.yourdomain@gmail.com
.
MTA stands for Mail Transport Agent. It is a component which forward and routes the emails using the headers on the email. It communicates with many other components in the SMTP. It can accept emails, forward emails for further routing, and pass the email to the MDA (Mail Delivery Agent).
---
How To SMTP on Ubuntu Server With msmtp
msmtp is an SMTP client which transmits mail to an SMTP server for further delivery. This is an alternative to /usr/sbin/sendmail
. This is a lightweight free software published under GNU GPL license. This is the official site:
1 | https://marlam.de/msmtp/ |
msmtp-mta
creates a symbolic link from /usr/bin/sendmail
to /usr/bin/msmtp
. So we will need to install three packages:
1 | sudo apt-get install msmtp msmtp-mta mailutils |
Normally you will have certificates here:
1 | ls /etc/ssl/certs |
If it is absent or empty then run:
1 | sudo apt-get install ca-certificates |
Create a log directory and give permission to Apache:
1 2 3 4 5 6 | sudo mkdir -p /var/log/msmtp sudo touch /var/log/msmtp/msmtp.log sudo chown www-data:www-data /var/log/msmtp sudo chown www-data:www-data /var/log/msmtp/msmtp.log sudo systemctl restart msmtpd.service sudo systemctl status msmtpd.service |
If you use PHP-FPM with MPM Event, then open this file (change the version of PHP on path):
1 2 3 | locate fpm/php.ini ## get the path nano /etc/php/7.2/fpm/php.ini |
Search for the phrase sendmail_path
, uncomment it and make it look like the below:
1 | sendmail_path = /usr/bin/msmtp -C /etc/msmtprc |
You’ll get example configurations here:
1 2 | sudo updatedb locate msmtprc |
Here are the files:
1 2 | /usr/share/doc/msmtp/examples/msmtprc-system.example /usr/share/doc/msmtp/examples/msmtprc-user.example |
Run cat
on them:
1 2 | cat /usr/share/doc/msmtp/examples/msmtprc-user.example cat /usr/share/doc/msmtp/examples/msmtprc-system.example |
and create your settings file:
1 | sudo nano /etc/msmtprc |
with this kind of setting, make sure that port 587 is open:
Restart, and reload the required services:
1 2 3 | service php7.2-fpm restart apachectl configtest /etc/init.d/apache2 reload |
Avoid suddenly running service apache2 restart
on a production server, you may face Config variable ${APACHE_RUN_DIR} is not defined error.
Usually, the dedicated servers have additional firewall which we can not see. More than allowing ufw, iptables, fail2ban and configuration on the control panel’s firewall, you need not to do anything. If it fails then open a support ticket, show this guide which you were trying. They will open the port for you.
Now, test the thing with this text file, edit it correctly, and save it as text.txt
:
1 2 3 4 5 6 7 8 9 10 11 12 | From: Abhishek Ghosh <username@hotmail.com> To: Recipient <your-another-emaile@hotmail.com> Subject: It Works! Test successful You have successfully set up MSMTP after reading our guide. Consider subscribing to our free newsletter and mobile notification. Regards, Abhishek Ghosh https://thecustomizewindows.com |
Now send it:
1 | cat test.txt | msmtp username@hotmail.com |
You’ll get no output on SSH. This is an example email received on inbox:
Also, you can check the log:
1 | cat /var/log/msmtp/msmtp.log |
How to Use mail Command to Send Email
Edit this:
1 | sudo nano /etc/mail.rc |
Add this line:
1 | set mta=/usr/bin/msmtp |
Create:
1 | nano /etc/aliases |
Add :
1 2 3 | your_username: username@hotmail.com # reference # https://wiki.archlinux.org/title/Cron#Example_with_msmtp |
Now, test it:
1 | echo "Testing msmtp with mail command" | mail -s "Does it Work" your-another-emaile@hotmail.com |
It should work. The next guide is about how to configure WordPress so that it can send you an email with this setup.