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

✔ Recommended Answer

Found the solution there:

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

Method #2

To run the msmtp mail command in PHP's system() function, you can use the following syntax:

system('echo "This is the message body" | msmtp --port=587 --auth=on --user=yourusername --password=yourpassword');

Here's how this command works:

  • echo "This is the message body" sends the message body to the msmtp command through the pipe (|) operator.
  • msmtp is the name of the command-line utility that sends mail via SMTP.
  • sets the From address for the email message to
  • specifies the SMTP server to use for sending the message. Replace with the hostname of your SMTP server.
  • --port=587 specifies the SMTP port to use. This is typically either 587 or 465, depending on your SMTP server's configuration.
  • --auth=on enables SMTP authentication.
  • --user=yourusername specifies the SMTP username to use for authentication.
  • --password=yourpassword specifies the SMTP password to use for authentication.
  • is the email address of the recipient.

Note that this is just an example command, and you'll need to replace the values for,, yourusername, yourpassword, and with the appropriate values for your own setup.

Also note that sending email from a PHP script using system() is not recommended, as it can be insecure and prone to abuse. It's better to use a dedicated email library, such as PHPMailer or SwiftMailer, which provide a safer and more robust way to send email from PHP.


Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755