5

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( 'noreply.njmedia@gmail.com', 'Test mail from localhost', 'Working Fine.'))
{
    echo 'Mail sent';
}
else
{
    echo 'Error. Please check error log.';
}
?>
user1402171
  • 113
  • 2
  • 7
  • Check the log for your mail server (usually /var/log/mail.log or something similar). – Emil Vikström Aug 26 '12 at 21:50
  • I looked in the error log, like you suggested, and it says the connection refused by 127.0.0.1. I know this is the internal IP address for my server's localhost. Do you have a suggestion to fix this error and make php mail work? Thanks :) – user1402171 Aug 26 '12 at 23:09

2 Answers2

5

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?).

Wireblue
  • 1,329
  • 1
  • 14
  • 24
  • Man, perfect, thank you. I've searched and asked for 3 days with no success. I was using docker-compose, msmtp, postfix and wordpress and was not able to send emails. But the issue was just php and msmt (adding the info so it's searchable), it was working from command line because container was running as root. One upvote is not enough. – jcubic Feb 14 '20 at 15:36
1

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.

drjd
  • 399
  • 1
  • 2
  • Thank you for your reply :) Just a quick clarification, are you saying to try to run sendmail from the terminal, and if that doesn't work to install sendmail? – user1402171 Aug 26 '12 at 23:12
  • Yes and no ;) If you can configure sendmail then yes. But sendmail itself is very hard to configure. I recommend to use the implementation of the sendmail command from postfix or other mail servers, because of easy configuration. – drjd Aug 26 '12 at 23:29
  • I did try sendmail before asking my question on this post. I agree with you, it is pretty hard to configure, espacially on linux :) Do you know of any websites or videos that do a good job of explaining sendmail implementations from postfix? – user1402171 Aug 27 '12 at 00:21
  • Most times, it is enough to use select at installation, that postfix has to be configured as "internet-site". For the ubuntu docs you can see this website: https://help.ubuntu.com/community/Postfix – drjd Aug 27 '12 at 18:09
  • If I install postfix as the link says to do, do I uninstall msmtp or use them together? – user1402171 Aug 27 '12 at 23:41
  • You should uninstall msmtp, but you can use them together, if you need msmtp. – drjd Aug 28 '12 at 17:38
  • Ok, Thanks. I'll have a go at postfix and come back to post the results. – user1402171 Aug 28 '12 at 17:45
  • I am about half way through the instructions on the link you provided. It says I need to configure my mail delivery agent to use the proper path. Is this needed if this server will only be sending mail and not recieving it? – user1402171 Aug 28 '12 at 17:59
  • Do you want a complete Mail server or only a local relay server? If so, you must not configure anything. Just install postfix, and select internet-site while installation process. – drjd Aug 28 '12 at 18:55
  • `sudo apt-get install postfix` -> `Internet-Site` – drjd Aug 28 '12 at 18:56
  • If you have already installed postfix, then execute this before: `sudo apt-get remove postfix --purge` – drjd Aug 28 '12 at 18:57
  • I only need to be able to send email from php. This server will not be recieving mail. The email that php sends out also must be able to reach anyone in the world, not just on the local network. Also I plan to use gmail as an smtp server, so really all I need is a way for php to send mail to gmail for delivery. – user1402171 Aug 28 '12 at 22:22
  • I did exactly as the tutorial said, but I never recieve any mail in my inbox. In an edit above I will post the php code I am using. – user1402171 Aug 29 '12 at 19:49
  • It says "Delivery temporarily suspended: connect to smtp.gmail.com Network is unreachable" – user1402171 Aug 29 '12 at 20:54
  • I think this should be enough information to solve the problem. Please don't let other people think for you. There are many ways to get information and solve easy problems by yourself. I really want to help you, but I'm not your brain. – drjd Aug 29 '12 at 22:14