By Jesin A
The lines that the user needs to enter or customize will be in red in this tutorial!
The rest should mostly be copy-and-pastable.
The PHP mail() function uses the program in sendmail_path configuration directive to send emails. This is set up as sendmail by default.
While most Linux installations have sendmail preinstalled, there is always a hassle of setting up SPF/PTR records, generating DKIM keys and a lot more to ensure that the email sent by your PHP script is not flagged as spam. A SMTP client called MSMTP can be used to send emails using third-party SMTP servers, this can also be used by PHP's mail() in the place of sendmail.
To install MSMTP on Fedora Linux use yum:
yum install msmtp
CentOS repository doesn't have a RPM package for MSMTP so we need to install it from source:
yum install make gcc pkgconfig wget http://sourceforge.net/projects/msmtp/files/msmtp/1. tar -xvf msmtp-1.4.31.tar.bz2 cd msmtp-1.4.31 ./configure make make install4.31/msmtp-1.4.31.tar.bz2/ download
The latest version is 1.4.31 at the time of this writing but it may change in future so to get the latest version, visit this sourceforge page.
On Ubuntu/Debian distribution use apt-get:
apt-get install msmtp
Arch Linux users:
sudo pacman -S msmtp
The configuration file of MSMTP is stored in ~/.msmtprc for each user and /etc/msmtprc is the system wide configuration file. Open the configuration file in your directory.
vi ~/.msmtprc
Add the following lines for a Yahoo account:
account yahoo tls on tls_starttls off tls_certcheck off auth on host smtp.mail.yahoo.com user user1 from user1@yahoo.com password yourYahooPa5sw0rd
For Gmail, use the following settings:
account gmail tls on tls_certcheck off auth on host smtp.gmail.com port 587 user user1@gmail.com from user1@gmail.com password yourgmailPassw0rd
This file can also have more than one account, just ensure that the "account" value is unique for each section. Save the file and use chmod to make this file readable only by the owner since it contains passwords. This step is mandatory because msmtp won't run if the permissions are more than 600.
chmod 600 ~/.msmtprc
Before implementing this in PHP, check from the command-line to ensure it works properly. To do this, create a plain text file containing a simple email:
echo -e "From: alice@example.com \n\ To: bob@domain.com \n\ Subject: Hello World \n\ \n\ This email was sent using MSMTP via Gmail/Yahoo." >> sample_email.txt
Now send this email:
cat sample_email.txt | msmtp --debug -a gmail bob@domain.com
Replace the word "gmail" with "yahoo" or whatever you entered for the "account" option. You'll see a lot of messages because of the "--debug" parameter. This is to make troubleshooting easy if things don't work as expected. If bob@domain.com receives this email, everything is setup correctly so copy this file to the /etc directory:
cp -p ~/.msmtprc /etc/.msmtp_php
Change the ownership to the username under which the web server is running. This can be "apache", "www-data", or "nobody" depending on the Linux distribution on your VPS and web server installed:
chown www-data:www-data /etc/.msmtp_php
Open the php.ini file, its location varies according to the OS and PHP type installed (PHP CGI, mod_php, PHP-FPM etc):
vi /etc/php5/php.ini
Find the following line:
sendmail_path =
Modify it by adding the path to the msmtp command:
sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp_php --logfile /var/log/msmtp.log -a gmail -t"
Manually create a log file and change its ownership to the username your web server is running as:
touch /var/log/msmtp.log chown www-data:www-data /var/log/msmtp.log
Restart your web server to apply the changes:
service httpd restart
In Arch Linux, this is done using the systemctl command:
systemctl restart httpd
Depending on your OS and web server, replace "httpd" with the appropriate name. If PHP is running as a separate process (like PHP-FPM), restart it instead:
service php5-fpm restart
Create a PHP script with a simple mail() to test this setup:
<?php if(mail("receipient@domain.com","A Subject Here","Hi there,\nThis email was sent using PHP's mail function.")) print "Email successfully sent"; else print "An error occured"; ?>
Access this file from the web browser.
http://www.example.com/file.php
If this email wasn't sent you can check the msmtp log file for errors.
tail /var/log/msmtp.log
If the email was not sent when using the PHP script, troubleshoot as follows:
php /var/www/html/file.php
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Independent Technical writer, WordPress developer and Linux administrator.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi,
Thanks for the tutorial, it’s very clear and easy to follow. I have a problem though, and I’ve exhausted every method I can think of to solve it.
When using the sample PHP script above to send the message (through gmail), I get the following error:
errormsg=‘authentication failed (method PLAIN)’ exitcode=EX_NOPERM
I have followed the config settings to the letter, checked the php.ini path, etc. but to no avail. Any suggestions on where to start looking to solve this?
By the way, I can tell than an attempt to send the message through gmail was made because I received a message from Google about an unauthorized attempt to send email through my account. I double checked the password, file settings, file location, but all seems correct.
@wizardware: Google prevented the login so php was unable to send the message. Try to see if you can whitelist your app from your gmail account’s settings.
Bingo! That did the trick, problem solved. Thank you Kamal, I doubt I would have ever thought of this.
It looks like msmtp expects to see the system wide config file in /etc/msmtprc in spite of what you put in the sendmail path. You also need to change the SMTP port to 587 to work with gmail - at least for my paid Google Apps account.
Ignore my comment - i’d missed out the chown. After some sleuthing, I found the reason!
CentOS 6.4 x32 does not send e-mail in error: “msmtp: support for TLS is not compilled in”. How to fix the problem?
This method is not safe to use on shared environments, since the gmail/yahoo password is exposed and readable by any apache user.
Hi there, great guide! It’s helped a lot, but I seem to be having issues getting the actual PHP to work.
Using command line msmtp command seems to work correctly, however using the PHP file sends back the failiure message. Just running “php mail.php” sends a “Authentication Failed (xxx Too Many Login Attempts)” which really confuses me. How could the command line email pass authentication but not the php file?
Thanks!
@em.apparition That error is displayed by the SMTP server (usually Gmail’s SMTP) if you’re using Google Apps without a DKIM record.
As to why it succeeds with the msmtp command I’m guessing that you’re using different email accounts in the ~/.msmtprc file and the one in /etc
something missing that make me crazy
yum install openssl yum install openssl-devel
and check send mail path
msmtp --version
I couldn’t send it from file.php but it worked on php /var/www/html/file.php What did I do wrong?
I made it work. But not on 465 port. Actually I’m on 587… but would be more secure to use 465? I tried to exchange from 587 to 465 but with no success. I tried add ssl:// on smtp.gmail.com and no email wass ent
I cant get it working… Tryed postfix, sendmail, exim and now this, none work. Unnistalled everything before testing and followed the many tutorials. Using unbutu 12.4
On this one im having problem on step cat sample_email.txt | msmtp --debug -a gmail bob@domain.com
msmtp: cannot connect to smtp.gmail.com, port 587: Network is unreachable msmtp: could not send mail (account gmail from /home/username/.msmtprc)
This is the first time I’m doing stuff like this, so I almost don’t have any idea. My msmtprc file looks like this:
account killaribyte host mail.killaribyte.com port 587 from contacto@killaribyte.com user contacto password **********
When I run the command: dig MX killaribyte.com +short @ns1.digitalocean.com I got: 50 mail.killaribyte.com.
When I run: host mail.killaribyte.com ns1.digitalocean.com I got: Using domain server: Name: ns1.digitalocean.com Address: 198.199.120.125#53 Aliases:
mail.killaribyte.com has address ... (my IP)
And finally when I want to test it with: cat sample_email.txt | msmtp --debug -a killaribyte contacto@killaribyte.com I got: msmtp: cannot connect to mail.killaribyte.com, port 587: Connection refused msmtp: could not send mail (account killaribyte from /etc/msmtprc)
@lfna23 Try changing the port to 25
@luanpersini I think iptables is configured incorrectly to block outgoing connections or to prevent TCP connections. Try clearing the rules
<pre>iptables -X iptables -F</pre>
The command line of sending e mail works for me but when it comes to using the file.php, I encountered the error msg in the browser "Failed to connect to ssl://foeapp.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]”
Any idea? I thought i have changed it to 587 on the msmtp file.
Thank you for this tutorial! It was very helpful and did exactly what I needed!
Hi, followed and successfully configured.
However, its rather slow even for the test mail. Takes 3-5 mins, is it normal?
@erwin: What’s the latency between your server and the SMTP server you’re connecting to?
ping -c 4 smtp.mail.yahoo.com
or
ping -c 4 smtp.gmail.com
Ok … managed to nail the problem, droplet is in Singapore 1 with IPv6 enabled. It takes 3-5 minutes with IPv6 and after disabling IPv6 within OS (nothing else changed), it takes 2-4 seconds with IPv4.
I do not know if it is Google or DigitalOcean but I really hope that DigitalOcean will look into and check if there is a IPv6 routing or speed issue.
msmtp: TLS handshake failed: An unexpected TLS packet was received. msmtp: could not send mail (account gmail from /home/pi/.msmtprc)
I have no idea what this means, could someone help? I would greatly appreciate it!
When I run the test php command it throws this error.
sh: 1: /usr/sbin/sendmail: not found
Why is it even setting it to sendmail path? how can I change it to make it use the msmtp? Also it’s not logging this error. I had to run the php in putty to get this error msg =\
Please help
I’m having the same issue as dmmd running CentOS… Everything works up until I try to send the test email.
[root@first msmtp-1.4.32]# cat sample_email.txt | msmtp --debug -a yahoo bob@domain.com ignoring system configuration file /usr/local/etc/msmtprc: No such file or directory loaded user configuration file /root/.msmtprc using account yahoo from /root/.msmtprc host = smtp.mail.yahoo.com port = 465 timeout = off protocol = smtp domain = localhost auth = choose user = myUserName password = * passwordeval = (not set) ntlmdomain = (not set) tls = on tls_starttls = off tls_trust_file = (not set) tls_crl_file = (not set) tls_fingerprint = (not set) tls_key_file = (not set) tls_cert_file = (not set) tls_certcheck = off tls_force_sslv3 = off tls_min_dh_prime_bits = (not set) tls_priorities = (not set) auto_from = off maildomain = (not set) from = myUserName@yahoo.com dsn_notify = (not set) dsn_return = (not set) keepbcc = off logfile = (not set) syslog = (not set) aliases = (not set) reading recipients from the command line msmtp: support for TLS is not compiled in
Any suggestions? I already had the tls_certcheck set to off.
Thanks.
Thank you for this. Skipped sendmail for this. However I would suggest not disabling TLS cert check.
Run this command to find cert issuer of your SMTP host.
Then simply locate appropriate cert to be used by msmtp.
tls_trust_file /usr/share/ca-certificates/mozilla/Thawte_Premium_Server_CA.crt
domain yourdomain.com
port 587
keepbcc on
Now I just need to figure out how to use passwordeval
+ gpg
with this so password is not plaintext.
It works for me exactly as described. I’m using an Archlinux and msmtp
in version 1.4.32.
Thanks, it’s very useful.
I get to the part to enter my data into vi ~/.msmtprc however I cannot figure out how to save it and move to the next screen. Im a total noob with the terminal. Please help someone :)
i am getting “The program ‘php’ is currently not installed. You can install it by typing: apt-get install php5-cli”
I have the current LEMP stack installed
This is a great article. I’ve used replacement modules for PHP mail before, and I’ve configured SMTP servers before… but I like this no-nonsense approach.
Email sent this way is more reliable/deliverable because other mail servers will see the traffic coming from a well-known SMTP service like Google (instead of directly off my webhosting IP, which some sites will spam score or at least treat with skepticism).
Hmm, odd problem occurring. I have only created an /etc/msmtprc file for config testing, not created a ~/.msmtprc. In this I have
account gmailsam
tls on
tls_certcheck off
auth on
host smtp.gmail.com
port 587
from sam.richardson@mydomain.com
user sam.richardson@mydomain.com
password foobar
Then I send this text message:
From: sam.richardson@mydomain.com
To: ronny@otherdomain.co.uk
Subject: Test from our server
This email from the our server, sam account
When I test with debug, it authorises great thru google apps, but I get this:
<-- 250 SMTPUTF8
--> AUTH PLAIN AHNhbS5yaWNoYXJkc29uQHZvb2Rvb3Ntcy5jb20AYnJpZ2h0MG46
<-- 235 2.7.0 Accepted
--> MAIL FROM:<sam.richardson@mydomain.com>
--> RCPT TO:<sam@mydomain.com>
--> DATA
<-- 250 2.1.0 OK ud4sm18672618wib.0 - gsmtp
<-- 250 2.1.5 OK ud4sm18672618wib.0 - gsmtp
<-- 354 Go ahead ud4sm18672618wib.0 - gsmtp
--> From: sam.richardson@mydomain.com
--> To: ronny@otherdomain.co.uk
--> Subject: Test from our server
-->
--> This email from the our server, sam account
-->
--> .
<-- 250 2.0.0 OK 1422374255 ud4sm18672618wib.0 - gsmtp
--> QUIT
Question: Where is it getting **RCPT TO: ** value from - there is no sam@ anywhere in this config.
David
Thanks for this tutorial! Works great!!!
Took a fair amount of time and hair pulling between figuring out exactly what parameters I needed to change, and remembering to uncomment the darn sendmail path >.<, but got everything working eventually!
Everyone getting blocked by google may try this solution
if you don’t have w3m text browser - install it with this command: sudo apt-get install w3m
w3m https://www.google.com/accounts/DisplayUnlockCaptcha
authorize with the e-mail you are using on the server confirm
press q to quit w3m
Great tutorial! But I’m running on a small problem that I don’t quite understand.
I’ve installed msmtp, and configured everything of my Google account (Google apps account) above, (email and password are correct).
Created the small email test php script. restarted apache2 and php5-fpm services (even my droplet after several attempts). But my email has never arrived.
Strange this is. When I execute the test script via command line I get the my success message and when I go to the same test script via the browser (script is located in www directory), I get the error message, msmtp.log file is for some reason empty, maybe permissions problem?
Oh yes: cat sample_email.txt | msmtp --debug -a gmail “myemail@myemail.com” works…
Any help is welcome :)
Hi,
thank you for your great tutorial. I followed your tutorial and manage to send email when executing the php command line but not when I access the file from the web browser, what does this mean?
I don’t understand why I cannot send email from Wordpress (ie. fail to send a password from the interface Wordpress). How about Apache config ‘ServerAdmin’ ?
Thank you in advance for your help
Hi, Thanks for the tutorial. Recently I have taken a gmail domain account (like support@domain.com). I want to send a mail from this account whenever the user register in my website. For this, I have followed the above steps and configured things as mentioned. When I run the given file sample_email.txt the mail is delivered to the user with the correct details like (from: namesupport@domain.com mailed-by : domain.com ). But when I run the php script it is giving different values (like, from : www-data “www-data@user@gmail.com via ******(some domain)” mailed-by: ******(that same name as mentioned after via) ). Please help me to correct these columns.
Hello. how are you? I wanted to install smtp but I wanted to ask you what directory should I do the configuration? in fact I use centos 7 and wanted to make ntopng alerts by email with php. I really need your help. I am an amateur in linux I wanted to clarify it. thank you in advance!!!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.