user1402171
user1402171

Reputation: 113

Sending mail through terminal using msmtp works fine, but doesn't work with php mail()

Recently I installed msmtp on my ubuntu 12.04 server that is running the kubuntu desktop. The install of msmtp worked fine, and I can even send mail through the terminal using gmail's smtp server.

My problem is that although sending mail works through the terminal, it still doesn't work with php mail. I have tried using the php mail() function, but I never receive any email.

I have checked the apache error logs (/var/log/apache2/error.log), but they are empty.
Does anyone know how to fix this? If any further information is needed, just say so :)

Also when executing the php script, no errors appear.

The code below echos Mail Sent, but I never recieve an email:

<?
if(mail( '[email protected]', 'Test mail from localhost', 'Working Fine.'))
{
    echo 'Mail sent';
}
else
{
    echo 'Error. Please check error log.';
}
?>

Upvotes: 5

Views: 4744

Answers (2)

Wireblue
Wireblue

Reputation: 1509

For me the cause was incorrect file ownership & permissions on the msmtp config file.

When I tried to run the PHP mail-sending script from the CLI as root, it worked. However switching user to www-data and trying to run the script again (also on CLI) failed with the following messages:

msmtp: /etc/msmtprc: contains secrets and therefore must be owned by you
msmtp: /etc/msmtprc: contains secrets and therefore must have no more than user read/write permissions

Triggering the PHP script by a HTTP request to Apache (or Nginx etc) would have the same results (since msmtp would be invoked by the www-data user).

Assuming your msmtp config file is at /etc/msmtprc, these commands would fix those issues:

chown www-data:www-data /etc/msmtprc
chmod 600 /etc/msmtprc

Before making these changes please consider if these new permissions are appropriate for your circumstances (eg. are there security implications?).

Upvotes: 6

drjd
drjd

Reputation: 399

The php mail module calls a system command called sendmail for sending emails. sendmail is implemented by various mail servers, e.g. postfix or exim. As I see, msmtp doesn't provide a sendmail binary. Please check, if you can execute sendmail via command line. If not, try to install postfix.

update: I see, that msmtp-mta does provide an sendmail binary too. You can try this, if you don't want postfix.

Upvotes: 1

Related Questions