Any modern website needs a few dialogues were users can either subscribe to a newsletter or they get a contact form to a send a request directly from that page. This can be done with PHPmail() function.
There are thousands of free scripts available which are all using the PHP mail() function.
PHP mail() uses the program indicated in the ‘sendmail_path’ in php.ini. On Debian Jessie you can find php.ini under
Now using your own linux server to send e-mail is not necessarily a wise idea and there are many constraints. You need to set up DKIM keys and you have to patch your server quite often to ensure the e-mails send by “your” server will not be flagged as spam my most professional e-mail providers.
It’s far better to use an external SMTP server. Below I will show how to set this up on A Debian Linux and with fastmail.com as the provider:
First we should update our apt-repository and we should ensure that everything is updated to the latest and created. Next we will download and install MSMTP as the program to communicate from our Linux distribution to an external SMTP provider.
apt-get update apt-get upgrade apt-get install msmtp
Configuration of MSMTP
Every user has an own configuration file under ~/.msmtprc .
Edit your own configuration with your editor of choice. I’m using vi(m):
The file should like the following (you have of course to edit your username and credentials – the account “name” can be anything you want, we are using fastmail in this example):
tls on tls_starttls off tls_certcheck off auth on host mail.messagingengine.com user email@example.com from firstname.lastname@example.org password yourpassword
MSMTP doesn’t work if this file has rights higher than **600**. This is a security measure, as the file contains the password and as such it has to be readable strictly by the owner only.
chmod 600 ~/.msmtprc
Now you have to create an email test file, as we want to check if things are working well. Create in any folder of your choice a file called **'email_test.txt'**. It should contain something like the following.
From: Tim Cook To: Jeannot Subject: Testing our external SMTP Server Access This email was sent using MSMTP via Fastmail SMTP server.
In the next step we will launch msmtp in debug-mode. “fastmail” stands for whatever name you gave your account above. If you used a different account name, this has to get changed in the below line as well. The email address ‘email@example.com’ needs to get replaced by one of your email addresses to check if things are running correctly.
cat email_test.txt | msmtp --debug -a fastmail firstname.lastname@example.org
Due to the debugging flag, you will get a rather long report, which might help you, in case things are not running properly. If you the test e-Mail, all is working just fine.
ignoring system configuration file /etc/msmtprc: No such file or directory loaded user configuration file /root/.msmtprc using account fastmail from /root/.msmtprc host = mail.messagingengine.com port = 465 timeout = off protocol = smtp domain = localhost auth = choose user = email@example.com 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 = firstname.lastname@example.org 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 TLS certificate information: Owner: Common Name: *.messagingengine.com Organization: FastMail Pty Ltd Locality: Melbourne State or Province: Victoria Country: AU Issuer: Common Name: DigiCert SHA2 High Assurance Server CA Organization: DigiCert Inc Organizational unit: www.digicert.com Country: US Validity: Activation time: Tue Apr 8 00:00:00 2014 Expiration time: Fri Dec 30 12:00:00 2016 Fingerprints: SHA1: 95:8B:16:01:56:3A:EF:92:60:7A:41:EB:5B:AD:22:E3:CA:CE:84:31 MD5: D8:F5:7E:43:A8:DA:29:22:6B:7E:90:A6:31:86:C8:CD EHLO localhost AUTH PLAIN AGplYW5ub3QubXVsbGVyQHJhbWdhZC5jb20AOG5EZGNDRHYzWkFiaG9RZlVha0VGaUVk MAIL FROM: --&amp;gt; RCPT TO: --&amp;gt; DATA -- 354 End data with . --&amp;gt; From: Tim Cook --&amp;gt; To: Jeannot --&amp;gt; Subject: Testing our external SMTP Server Access --&amp;gt; This email was sent using MSMTP via Fastmail SMTP server. --&amp;gt; . QUIT
Configuration of PHP
Now that our test e-Mail was successfully sent, we have to tell PHP what to do, in case PHP mail() is getting called by any script.
First we have to copy “our” working configuration to the system-wide folder:
cp -p ~/.msmtprc /etc/.msmtp_php
Next we have to change the ownership to the one used by Apache2.
chown www-data:www-data /etc/.msmtp_php
Finally we have to edit our **php.ini** file:
Search for the **sendmail_path = line**. Ensure that any leading ' will be deleted and change the path to:
sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp_php --logfile /var/log/msmtp.log -a fastmail -t"
(here again, 'fastmail' stands for the name of the account you defined above. If you used a different name, you have to change it here as well).
Last but not least, we have to create the appropriate empty log files on our own:
touch /var/log/msmtp.log chown www-data:www-data /var/log/msmtp.log
Now all we have to do, is restarting our Apache2 Server and we are good to go:
/etc/init.d apache2 restart