This is a sendmail emulator system which delivers email from a local computer to an SMTP server. This method avoids the usage of cloud-based transactional email subscriptions. This is useful if you already using a paid tier of Outlook Gmail or Zoho for your domain. In other words, you’ll get domain-branded, whitelisted email from your server to your mailbox. Normally we need server cron and WordPress to send us emails.
One of our earlier guides on Ubuntu 22.04 Server was on how to set up a Passwordless SSH Login. It is mandatory to follow that guide and create a non-root user and follow this guide. Unless you secure your server, hackers will get access to your email credentials.
There are several ways to configure a Linux server to send email. msmtp is an SMTP client which is free software, published under the GPLv3. MTA stands for Mail Transport Agent. It is a component which forwards 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).
---
If you are using servers from VPSDime, then you must not abuse the system for mass emailing. To follow this guide, you need:
- An Ubuntu 22.04 server running Apache2, PHP (version 7.2 to 8.4), MySQL
- 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 chance of getting the email blacklisted or hacked. You can use an email address such as
webserver.yourdomain@gmail.com. Any email account from Outlook/Hotmail, Yahoo!, Gmail or similar service will work fine. - Of course, you must be able to SSH into the server with root privilege.
Step 1: Install the Required Packages to Operate msmtp
msmtp is an SMTP client which transmits a mail to an SMTP server for further delivery. This is an alternative to /usr/sbin/sendmail. This is a lightweight free software published under a GNU GPL license. This is the official site:
1 | https://marlam.de/msmtp/ |
Now, SSH to your server.
msmtp-mta creates a symbolic link from /usr/bin/sendmail to /usr/bin/msmtp. So we will need to install three packages:
1 2 | sudo apt update -y && apt upgrade -y sudo apt-get install msmtp msmtp-mta mailutils |
It will ask whether the wizard will enable App Armour support. Answer NO.
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 permit Apache:
1 2 3 4 5 6 7 | 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 # press Q to exit |
If you use How to Install Apache MPM Event and PHP-FPM on Ubuntu Server, 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_pathand sendmail_from fields (use ctrl + W in Nano), it will look like this:
1 2 3 4 5 6 7 8 9 10 11 | ; http://php.net/sendmail-from ;sendmail_from = me@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path ;sendmail_path = ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(). ;mail.force_extra_parameters = |
uncomment it and make it look like the below:
1 | sendmail_path = /usr/bin/msmtp -C /etc/msmtprc |
Save the file (use ctrl + O in Nano to save, ctrl + X to exit). 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. Now, test the thing with this text file, edit it correctly, and save it as test.txt (run the command nano test.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 |