phpshellsystemmsmtp

How to run 'msmtp' mail command in PHP system()?


My server don't have 'postfix' to send mails from 'sendmail'.

Instead, I use 'msmtp' and 's-nail' shell commands.

It works well from interactive shell ('bash').

I try to send mail from PHP, but I don't receive any, and have no output nor error:

<?php

$mess = 'foobar';
$to = 'me@mail.org';
$subject = 'test';
//mail($to,$subject,$mess); doesn't work
$out = system("echo $mess | /usr/bin/s-nail -v -s $subject $to");
echo $out;
?>

Any help?

If I run the script like this :

# php script.php

it works.

But when called from web, it doesn't work


Solution

  • Found the solution there: https://wiki.archlinux.org/title/Msmtp#Miscellaneous

    In 'php.ini':

    sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -t"
    

    In /etc/mail.rc:

    set mta=/usr/bin/msmtp
    

    In PHP script, now mail() run properly.


    If you have the package msmtp-mta like Debian/Ubuntu, things are more simple. It enable port 25 to listen on localhost as a MTA