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

by darrion.kuhn , in category: PHP General , 9 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 9 months ago

@darrion.kuhn 

To run the 'msmtp' mail command in PHP system(), you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
// Define the email content and recipient
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using msmtp command in PHP system()';

// Build the command to send the email using msmtp
$command = "echo "$message" | msmtp $to -t -s "$subject"";

// Execute the command using PHP's system() function
$result = system($command, $return_var);

// Check if the command was executed successfully
if ($result !== false) {
    echo 'Email sent successfully.';
} else {
    echo 'Failed to send email.';
}
?>


Replace '[email protected]' with the actual email recipient, and customize the email subject and message as needed. This code snippet will send an email using the 'msmtp' command through PHP's system() function.