How to send mail in php using sendmail

To send mail using PHP and the sendmail command, you can use the mail() function in PHP. Here's an example of how to do it:

Step 1: Configure your PHP settings

Make sure that the sendmail_path directive is set in your PHP configuration file (php.ini). This directive specifies the path to the sendmail command.

For example, if you have sendmail installed in the default location (/usr/sbin/sendmail), you can set the sendmail_path directive as follows:

sendmail_path = /usr/sbin/sendmail -t

Step 2: Use the mail() function

Use the mail() function to send an email. The mail() function takes four arguments:

  1. to: The recipient's email address.
  2. subject: The subject of the email.
  3. message: The body of the email.
  4. headers: An optional array of headers to include in the email.

Here's an example of how to use the mail() function:

<?php
$to = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email sent using PHP and sendmail.';

$headers = array(
    'From' => '[email protected]',
    'Reply-To' => '[email protected]',
    'Content-Type' => 'text/plain; charset=UTF-8'
);

if (mail($to, $subject, $message, implode("\r\n", $headers))) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email.';
}
?>

Step 3: Run the script

Save the script to a file (e.g., sendmail.php) and run it using your web server or a command-line PHP interpreter.

When you run the script, it will send an email to the specified recipient using the sendmail command.

Tips and considerations