PHP to send mail

On Unix systems, you can configure the mail() function to use sendmail or Qmail to send messages. When running PHP under Windows, you can use sendmail by installing sendmail and setting the sendmail_path in php.ini to point at the executable. It likely is more convenient to simply point the Windows version of PHP to an SMTP server that will accept you as a known mail client:

Syntax of mail function

mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    string $additional_params = “”
): bool

About mail parameters

to

Receiver, or receivers of the mail.

Example :[email protected]

subject

Subject of the email to be sent

message

Message to be sent.

Each line should be separated with a CRLF (\r\n). Lines should not be larger than 70 characters

Example of mail function to send mail in PHP

<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('[email protected]', 'My Subject', $message);
?>

More example to send mail

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = array(
    'From' => '[email protected]',
    'Reply-To' => '[email protected]',
    'X-Mailer' => 'PHP/' . phpversion()
);

mail($to, $subject, $message, $headers);
?>

Leave a Comment