How to fix PHP not sending mail in Debian 6 and EXIM4

It took a while to figure this out. As it turned out, there were 2 problems affecting the sending of emails: first there was the configuration of EXIM4 for proper sending of emails from the server and the PHP had to be configured properly. I will tell you next the steps I took to fix both.

Initial configuration of EXIM4

There are 2 ways of doing this: Running the start Exim configuration script and answer the questions presented. There are 2 excellent resources you can use if you want to go this way: Linode’s EXIM4 config post and the official package description for Debian. There is a faster way I will show you if you are comfortable modifying configuration text files.

We will edit the configuration file for EXIM4 with the following command:

# nano /etc/exim4/update-exim4.conf.conf

Once in there, make the necessary changes to match as follows:

 dc_readhost='[<em>enter here the name of your host</em>]'

Close the file with Ctrl+X, then confirm saving the file by answering ‘Y’ and then enter to confirm the name of the file. Next, you need to restart EXIM4 as follows:

# /etc/init.d/exim4 restart

That will do the trick for EXIM4, you can test at this point if the system is properly configured by sending you a test email as follows:

# echo "This is a test." | mail -s Testing [<em>youremail</em>]@[<em></em>]

For troubleshooting, you can look into the logfile EXIM4 keeps of its operations as follows:

# cat /var/log/exim4/mainlog

 PHP configuration of SMTP email

Now, we need to make sure PHP is configured to talk nice to EXIM4. Debian 6 uses EXIM4 as its message transfer agent (MTA) and this is different from other Linux builds where SENDMAIL is normally used. Because of this, the PHP.ini file might be missing the proper configuration to work, as PHP manual page mentions: ‘an honest attempt of locating the sendmail program for you is done and set a default, but if it fails, you can set it manually‘. Let’s open PHP.ini for edition with the following command:

# nano /etc/php5/apache2/php.ini

And search for the section configuring SMTP parameters with Ctrl+W, then type ‘sendmail_path‘ and enter.  Once you  are there, edit the line to the following:

sendmail_path = /usr/sbin/sendmail -t -i

In the same way, order closing the file with Ctrl+X, indicate saving changes with Y and then confirm the name of the file with enter.

Now, restart Apache for the changes to take effect with the following commands:

# /etc/init.d/apache2 stop
# /etc/init.d/apache2 start

You can test PHP with the following simple script, create a new file on your server, name it ‘testmail.php’ and paste the following code:

$email_to = "[enter your email here]";
$email_subject = 'Testing EXIM4';
$email_message = 'This is a Test';

    // create email headers
    $headers = 'From:'."\r\n".
               'Reply-To:'."\r\n" .
               'X-Mailer: PHP/' . phpversion();
     $result = mail($email_to, $email_subject, $email_message, $headers); 

     if ($result) echo 'Mail accepted for delivery ';
     if (!$result) echo 'Test unsuccessful... ';

This should fix the problem and email should be sending nicely. Did this fix your issue?… Let me know!

And remember, if you need quality hosting at reasonable prices, give a chance. Visit today!

This entry was posted in Cloud, Debian and tagged , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.


  1. Posted February 25, 2012 at 8:49 pm | Permalink

    thanks for share!

  2. Mirna Limic
    Posted May 10, 2012 at 10:46 am | Permalink

    Thank you so very much ! I didn’t set the flags -t -i in the call to exim, and sending from php was constantly failing. Thanks!

  3. Ben Byrne
    Posted August 19, 2012 at 11:13 pm | Permalink

    Thanks for this. Didn’t work for me, though. When I run the test ($ echo “This is a test”… etc), I get an error:

    send-mail: Cannot open mail:25

    Any ideas? This is a fresh install of Debian – might it be that I don’t have an MTA such as sendmail or postfix around to actually handle things (i’m fuzzy on what exim4 is).

  4. Ben Byrne
    Posted August 19, 2012 at 11:17 pm | Permalink

    Scratch that — got it working. At one point I’d installed ssmtp which was taking the sendmail call, rather than exim4. Removed sstmp and updated exim4 and it appears to be working.