A simple and lightweight solution for your VPS
I host this blog on a Debian 12 VPS, and believe me, sending emails gave me more than a few headaches at first. If you’ve ever used PHP’s mail() function in WordPress, you’re likely familiar with its shortcomings: emails landing in spam, messages that never arrive, or the lack of proper SMTP authentication. And if, like me, you have a VPS with modest resources, loading up WordPress with plugins just to handle that is like asking an exhausted horse to carry an extra suitcase: more CPU and memory consumed when you don’t always have plenty to spare.
This is where msmtp comes in: a lightweight tool that replaces Sendmail and gives you total control over how your emails are sent. No plugins, no unnecessary bloat—just emails that reach their destination, reduced server load, and the freedom to customize everything to your liking.
What Is msmtp and Why Should You Try It?
msmtp is a simple, straightforward SMTP client that sends emails from your server using secure protocols. No frills, just the essentials. Compared to plugins like WP Mail SMTP or Easy WP SMTP, it offers clear advantages:
- Low resource usage: It only consumes resources when sending an email, without leaving heavy processes running continuously in the background.
- Total flexibility: You can set up multiple SMTP accounts or even advanced rules based on the sender or recipient.
- Independence: It does not rely on WordPress or its plugin update cycle, freeing you from compatibility concerns.
It is ideal for small VPS instances like mine, headless servers, or projects where you need custom SMTP delivery without unnecessary complications.
Prerequisites
I wrote this guide based on my own VPS setup, so here is what you’ll need:
- A VPS running Debian 12 (or an Ubuntu-based distribution).
- Server access with root privileges or
sudoaccess. - An email account with SMTP access (such as Gmail, Outlook, or any provider offering standard SMTP).
- Basic familiarity with the terminal (being comfortable using nano or vim to edit files is enough).
In my setup, I use Nginx as the web server and PHP-FPM to process PHP. This stack is very common on modern VPS environments due to its performance and efficiency. If you are using Apache, don’t worry; the overall logic behind configuring msmtp remains the same, though specific configuration file paths and service commands may vary slightly.
If you run into differences due to your environment, you may need to check the equivalent paths for your setup. However, most steps apply directly.
If you’d like a more detailed guide on setting up msmtp with Apache and PHP, including advanced optimizations, let me know in the comments! If there’s enough interest, we’ll cover that in an upcoming article.
Step 1: Install and Configure msmtp
Ready? Let’s get straight to it.
Installation
First, update your package lists and install msmtp. Open your terminal and run:
sudo apt update
sudo apt install msmtp
Quick and easy!
Configure SMTP
Next, you need to create a configuration file. You can configure it globally at /etc/msmtprc (recommended for this setup) or per user at ~/.msmtprc. We’ll go with the global option:
sudo nano /etc/msmtprc
Paste this example configuration (configured for Gmail):
# Configuración básica
account default
host smtp.gmail.com
port 587
auth on
user [email protected]
password tu_contraseña
tls on
tls_starttls on
logfile ~/.msmtp.log
Replace [email protected] and tu_contraseña with your actual account credentials. Note: If you use Gmail with two-factor authentication, you must generate an App Password from your Google Account security settings. Save the file with Ctrl+O, press Enter, and exit with Ctrl+X.
To prevent unauthorized users from viewing your credentials, lock down the file permissions:
sudo chmod 600 /etc/msmtprc
Test the Configuration
Send a test email to verify that msmtp is working properly:
echo "Correo de prueba desde msmtp" | msmtp --debug --from=default -t [email protected]
Replace [email protected] with an email address you can access. If something fails, inspect the log file at ~/.msmtp.log to troubleshoot.
Step 2: Connect msmtp to PHP
To make PHP bypass its default mail() mechanism and route through msmtp instead, you only need to tweak a couple of settings.
Edit php.ini
Find your active php.ini file. If you are unsure where it is located, this command will reveal it:
php --ini
Alternatively, look in directories such as /etc/php/8.x/fpm/php.ini (which is typically where it lives when running Nginx with PHP-FPM). If you are using Apache, the path will likely resemble /etc/php/8.x/apache2/php.ini. Open it with:
sudo nano /etc/php/8.x/apache2/php.ini
Locate the sendmail_path directive (use Ctrl+W in nano to search for it quickly) and update it to:
sendmail_path = "/usr/bin/msmtp -t"
Save your changes with Ctrl+O, Enter, and exit with Ctrl+X.
Restart PHP-FPM
Apply the changes by restarting the PHP-FPM service:
sudo systemctl restart php8.x-fpm
(Note: Replace 8.x with the PHP version installed on your system, such as 8.1 or 8.2. If you are unsure, run php -v to check).
Test with a PHP Script
Create a test.php file in your site’s document root (or wherever you prefer):
<?php
mail('[email protected]', 'Asunto de prueba', 'Este es un correo enviado con msmtp y PHP.');
echo "Correo enviado. Revisa tu bandeja (y el spam, por si acaso).";
?>
Update [email protected] with your email address, then load the file in your browser (e.g., yourdomain.com/test.php) or execute php test.php directly from the terminal. Check your inbox to confirm the email was delivered.
Step 3: Test It in WordPress
With msmtp and PHP set up, it’s time to verify how it behaves in WordPress:
- Password Reset: Go to the login page, request a password reset link, and make sure the email arrives.
- Notifications: Post a comment (if comment notifications are enabled) and check for the alert.
- Forms: If you use a contact form plugin, send a test submission.
Troubleshooting
- Permissions: Ensure
/etc/msmtprchaschmod 600permissions. - Deliverability: Use tools like Mail Tester to evaluate your email deliverability score.
- Protect Credentials: Instead of storing plain credentials in the configuration file, consider using environment variables or secret management tools like
pass. - Rate Limiting: If your application sends a high volume of emails, set up limits to avoid triggering spam filters.
Optimization and Security Tips
- SPF, DKIM, and DMARC: Configure these DNS records for your domain so receiving mail servers verify and trust your messages.
- Deliverability Testing: Run tests through services like Mail Tester to keep your sender reputation high.
- Credential Security: Restrict access to the configuration file and monitor logs periodically.
- Volume Control: Keep transactional volume reasonable to prevent provider rate limits.
Why msmtp Over a Plugin?
- Performance: Zero plugins running means no extra PHP processes on your WordPress installation.
- Control: You define the exact routing and configuration directly at the OS level.
- Stability: Less risk of broken email flows triggered by WordPress or plugin updates.
- Security: Handling SMTP on the server layer reduces the attack surface inside WordPress.
Wrapping Up
Setting up msmtp with PHP is a clean, practical way to send emails from WordPress without plugins—ideal for resource-constrained VPS setups. With just a few commands, you get a reliable, secure system that doesn’t depend on third-party plugin stacks. Ready to give it a try and make your WordPress site leaner?
If you have any questions or get stuck along the way, leave a comment below and I’ll be glad to help!