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

✔ Recommended Answer

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

Method #2

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

php
system('echo "This is the message body" | msmtp --from=user@example.com --host=mail.example.com --port=587 --auth=on --user=yourusername --password=yourpassword recipient@example.com');

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.
  • --from=user@example.com sets the From address for the email message to user@example.com.
  • --host=mail.example.com specifies the SMTP server to use for sending the message. Replace mail.example.com 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.
  • recipient@example.com is the email address of the recipient.

Note that this is just an example command, and you'll need to replace the values for user@example.com, mail.example.com, yourusername, yourpassword, and recipient@example.com 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.

Comments

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