Sitemap / Advertise

Information



Tags



Share

How to use the PHP mail() function to send emails from the server

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

If you are creating the roots of your new business on the web, having an email list, or widely named as a customer list, is a requisite. When you acquire your customer list, you can inform them about the things going on with your business in a time frame. In this tutorial, I will show you how you can achieve that in PHP, using the mail() function.

Syntax:

mail ( string $to , string $subject , string $message [, mixed $additional_headers [, string $additional_parameters ]] ) : bool

Code(*)

Define 'to' parameter:

Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822.

e.g. user@example.com

Define 'subject' parameter:

Subject of the email to be sent.

Subject must satisfy » RFC 2047.

Define 'message' parameter:

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

Define optional '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 outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.

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

Define optional 'additional_parameters':

The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.

//Define required and optional parameters.

$to = "user@example.com";

$subject = "test";

$message = "This is a test message from the server.";

$additional_headers = "From: info@theamplituhedron.com\r\n";
$additional_headers .= "Reply-To: info@theamplituhedron.com\r\n";
$additional_headers .= "MIME-Version: 1.0\r\n";
$additional_headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$additional_parameters = "-finfo@theamplituhedron.com";

// Send the message.

mail($to, $subject, $message, $additional_headers, $additional_parameters);

References

(*) https://www.php.net/manual/en/function.mail.php