Msmtp works from centOS command line but not from PHP

I hope someone has run into this before. My php function is;

function msmtp($To, $Subject, $Body, $Headers, $Account) {
	$Email = "To: $To\nSubject: $Subject\n$Headers\n\n$Body\n";
	exec("echo '$Email' | /usr/bin/msmtp -t -a $Account $To");
	}

this is the function call:
msmtp($mailTo, $mailSubject, stripslashes($mailBody), $mailHeader, “information”);

$Email has the correct information. When echo’d into the msmtp program it’s like entering a black hole. I can’t find any logs that tell me what’s going on. from the command line, no problem. it works and updates the log in /var/log. /etc/msmtprc has the right mods, 600.

I wish I could find some way to see what’s going on with this. I add a -v option to msmtp but it doesn’t make it to stdout.

Any help would be appreciated.

Why don’t you use PHPMailer?

standard PHP checking; make sure you’re outputting your errors for a local page so you can figure out whats going wrong. In particular, exec will emit an E_WARNING level message if it can’t run the command (like, for example, if the PHP executable user doesnt have permission to access the msmtp file?)

Ok, fixed it. This one was really obscure and honestly don’t understand the why of it. I used the below code to get and output in the browser window so I could finallly see what was going on. The -v for verbose mode in msmtp. With this I found the log file couldn’t be written to due to a permissions problem. Fixed that. I then got “errormsg=‘the server sent an invalid reply’ exitcode=EX_PROTOCOL” in the log file, which was coming from the destination mail server.

exec(“echo ‘$Email’ | /usr/local/bin/msmtp -v -a ‘$Account’ ‘$mailTo’ 2>&1”, $output, $return_var);

   var_dump($return_var);
   echo "return_var is: $return_var" . "\n";
   var_dump($output);

I finally discovered that the below setting of the variable $mailHeader didn’t work

$mailHeader = "From: " . $email;

which is the $Headers variable in the below function.

function msmtp($To, $Subject, $Body, $Headers, $Account) {
	$Email = "To: $To\nSubject: $Subject\n$Headers\n\n$Body\n";
	exec("echo '$Email' | /usr/bin/msmtp -t -a $Account $To");
	}
The below however, does work:

$mailHeader  = "From: <?=$email?>";

I do not know what the difference is between these two ways of creating the variable $mailHeader but the last one is the one that works.

Weird.




Ok fixed it. Weird problem. I used the below to get an output

exec("echo '$Email' | /usr/local/bin/msmtp -v -a '$Account' '$mailTo' 2>&1", $output, $return_var);
  var_dump($return_var);
  echo "return_var is: $return_var" . "\n";
  var_dump($output);

First I found was the reason the log file wasn’t being updated, didn’t have the right permissions. Fixed that. Next thing, from the log file was:

errormsg='the server sent an invalid reply' exitcode=EX_PROTOCOL

This one took a while to figure out but essentially it was the $Header variable being passed into the function.
The original variable was:

$mailHeader  = "From: " . $emailx;

The one that works is:

$mailHeader  = "From: <?=$emailx?>";

I do not get the difference in the two ways to set this variable, but the second way definitely fixed my problem. All is working now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.