PHP mail on CentOS 6

From n0v4 Wiki
Revision as of 17:40, 29 March 2015 by Red (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a quick tutorial to setup PHP/mail on CentOS 6 so that the mail() function works. This is probably not the best configuration, but it works.


This tutorial assumes that you have root access to your server, and have PHP already installed, and your server is setup correctly to access the internet.


CentOS 6 uses Postfix by default. (Or at least my installation did.) The version installed to do this was postfix-2.6.6-6.el6_5.x86_64

The configuration file of interest is located here:


In it is the default configuration file that came with CentOS 6. At the end of the file you can add the lines:

authorized_submit_users = root, otherUser
mydomain =

where otherUser is the name of the Linux user running the intended PHP script, and is the domain name you are sending from. Failing to do this causes the Postfix instance to send mail from host.localdomain, which is configured by default by most mail recipients to deny.This allows the user to submit -f flags to Postfix (from PHP), and configures Postfix to use your domain name you want the server to be associated with.

Postfix Configuration Parameters [1]


Recipient servers will generally be configured to perform SPF and other checks to make sure that your server is an allowed sender for your domain name. There are multiple tools available online for configuring an SPF record, which is really a TXT DNS record with a specially formatted string. Be sure there is an SPF record for the domain name you are sending from. (In the above code this would be If your server is a subdomain on another domain, make sure there is at minimum a subdomain SPF record for your server.

  • Microsoft SPF Online Wizard [2]
  • AJAX SPF Wizard [3]
  • SPF Validation [4]


The "From", "To", and "Message" parameters of the PHP mail()[5] function are generally required. Additionally "additional headers" and "additional parameters" can be passed through mail(). In this case it is good to include additional headers, and the additional parameters are required.

The following code is based on the note on the PHP mail() manual page by bimal at sanjaal dot com [6]


$to      = '';
$subject = 'Test Message';
$message = 'Hello, this is a test email.';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Exampl Source <";
$headers[] = "Bcc: Example BCC User <>";
$headers[] = "Reply-To:  <>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
if(mail($to, $subject, $message, implode("\n", $headers), '-f')){
	echo "Mail success!\r\n";
} else {
	echo "Mail fail!\r\n";


Note the use of the additional parameter '-f'. This is the parameter sent to Postfix from PHP needed to properly configure the from portion of the header. Additionally, I've found that using "\n" as the glue in the implode() function is better than using "\r\n", the suggestion by bimal at sanjaal dot com. Some recipient servers do not seem to handle it well and the headers end up in the body of the message.


Alternatives to configuring your mail sever like this, mainly consist of setting up a mail account at a imap/smtp mail provider, and having the PHP script log into the server and send messages that way. This can easily be done by using the PHPMailer Class[7] which is quite popular.