An Essential Guide to PHP mail() Function By Examples (original) (raw)

Summary: in this tutorial, you’ll learn how to send email using the PHP mail() function.

Introduction to the PHP mail() function #

To send mail, you use the mail() function.

On Linux or Unix systems, you can configure the mail() function to use the sednmail or Qmail program to send messages.

On Windows, you can install the sendmail and set the sendmail_path in php.ini file to point at the executable file.

However, it’s more convenient to set the SMTP server with a port and sendmail_from in the php.ini file on Windows like this:

[mail function] SMTP=smtp.phptutorial.net smtp_port=25 sendmail_from=contact@phptutorial.netCode language: PHP (php)

If the SMTP server requires authentication, you can add the following lines for the account to authenticate:

auth_username=smtp_user auth_password=smpt_passwordCode language: PHP (php)

Once the configuration is ready, you need to restart the webserver.

The following illustrates the syntax of the mail() function:

mail( string $to, string $subject, string $message, array|string $additional_headers = [], string $additional_params = "" ): boolCode language: PHP (php)

The mail() function has the following parameters:

The mail() function returns true if the mail was accepted for delivery. It doesn’t mean that the mail is successfully reached the intended receiver. If an error occurred, the mail() function return false.

Let’s take some examples of using the PHP mail() function.

1) Using the PHP mail() function to send a plain text email example #

The following example uses the mail() function to send a simple email:

`<?php

$subject = 'This is a test email';

$message = <<<MSG Hi, This is a simple email. It's sent from PHP. MSG;

wordwrap($message, 70, "\r\n");

mail('[email protected]', subject,subject, subject,message);`Code language: PHP (php)

In this example, we use the wordwrap() function to ensure that the lines of the message won’t exceed 70 characters.

The following example uses the mail() function to send a mail with additional headers like From, Reply-To, and X-Mailer:

`<?php

$to = '[email protected]'; $subject = 'This is a test email'; $message = 'Hi there';

$headers[] = 'From: [email protected]'; $headers[] = 'Reply-To: [email protected]'; $headers[] = 'X-Mailer: PHP/' . phpversion();

mail($to, subject,subject, subject,message, implode('\r\n', $headers));`Code language: PHP (php)

If you use PHP 7.2 or later, you can pass the headers as an array like this:

`<?php

$to = '[email protected]'; $subject = 'This is a test email'; $message = 'Hi there';

$headers = [ 'From' => '[email protected]', 'Reply-To' => '[email protected]', 'X-Mailer' => 'PHP/' . phpversion() ];

mail($to, subject,subject, subject,message, $headers);`Code language: PHP (php)

3) Using the PHP mail() function to send HTML email example #

To send HTML mail, you need to set the Content-type for the header like this:

`<?php

$to = '[email protected]'; $subject = 'This is a test email'; $message = '

Email
<h1>This is HTML mail</h1>
';

$headers = [ 'MIME-Version' => '1.0', 'Content-type' => 'text/html; charset=utf8', 'From' => '[email protected]', 'Reply-To' => '[email protected]', 'X-Mailer' => 'PHP/' . phpversion() ];

if (mail($to, subject,subject, subject,message, $headers)) { echo 'email was sent.'; } else { echo 'An error occurred.'; }`Code language: PHP (php)

Summary #

Did you find this tutorial useful?