Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

mb_send_mail

Description

The mb_send_mail of Multibyte String for PHP send encoded mail.

Syntax

mb_send_mail(
    string $to,
    string $subject,
    string $message,
    array|string $additional_headers = [],
    ?string $additional_params = null
): bool

Parameters

to

The mail addresses being sent to. Multiple recipients may be specified by putting a comma between each address in to. This parameter is not automatically encoded.

subject

The subject of the mail.

message

The message of the mail.

additional_headers

String or array to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

If an array is passed, its keys are the header names and its values are the respective header values.

NOTE: When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

NOTE: If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used).

additional_params

additional_params is a MTA command line parameter. It is useful when setting the correct Return-Path header when using sendmail.

This parameter is escaped by escapeshellcmd() internally to prevent command execution. escapeshellcmd() prevents command execution, but allows to add additional parameters. For security reason, this parameter should be validated.

Since escapeshellcmd() is applied automatically, some characters that are allowed as email addresses by internet RFCs cannot be used. Programs that are required to use these characters mail() cannot be used.

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.

Return

Returns true on success or false on failure.

Examples

1 · to · single subject message

<?

$to = 'myto@mydomain.com';
$subject = '🐘subject';
$message = '🐘message';

$return = mb_send_mail($to, $subject, $message);

var_export($return);

?>
true

2 · to · multiple subject message

<?

$to = 'myto1@mydomain.com, myto2@mydomain.com, myto3@mydomain.com';
$subject = '🐘subject';
$message = '🐘message';

$return = mb_send_mail($to, $subject, $message);

var_export($return);

?>
true

3 · additional_headers · array

<?

$to = 'myto@mydomain.com';
$subject = '🐘subject';
$message = '🐘message';
$additional_headers = ['from' => 'myfrom@mydomain.com', 'reply-to' => 'myreply-to@mydomain.com', 'cc' => 'mycc@mydomain.com', 'bcc' => 'mybcc@mydomain.com'];

$return = mb_send_mail($to, $subject, $message, $additional_headers);

var_export($return);

?>
true

4 · additional_headers · string

<?

$to = 'myto@mydomain.com';
$subject = '🐘subject';
$message = '🐘message';
$additional_headers = "from: myfrom@mydomain.com\r\nreply-to: myreply-to@mydomain.com\r\ncc: mycc@mydomain.com\r\nbcc: mybcc@mydomain.com";

$return = mb_send_mail($to, $subject, $message, $additional_headers);

var_export($return);

?>
true

5 · additional_params

<?

$to = 'myto@mydomain.com';
$subject = '🐘subject';
$message = '🐘message';
$additional_headers = ['from' => 'myfrom@mydomain.com', 'reply-to' => 'myreply-to@mydomain.com', 'cc' => 'mycc@mydomain.com', 'bcc' => 'mybcc@mydomain.com'];
$additional_params = '-fwebmaster@mydomain.com';

$return = mb_send_mail($to, $subject, $message, $additional_headers, $additional_params);

var_export($return);

?>
true
HomeMenu