PHP Sending Email


PHP allows you to send emails directly from a script.


PHP mail() Function

The PHP mail() function is used to send emails from a script.

Syntax

mail(to,subject,message,headers,parameters)

Parameter Description
to Required. Specifies the email recipient.
subject Required. Specifies the subject of the email.Note:This parameter cannot contain any newline characters.
message Required. Defines the message to be sent. Use LF (\n) to separate lines. Each line should be limited to 70 characters.
headers Optional. Specifies additional headers, such as From, Cc, and Bcc. Additional headers should be separated with CRLF (\r\n).
parameters Optional. Specifies additional parameters for the mail sending program.

Note:PHP requires an installed and working mail system (such as sendmail, postfix, qmail, etc.) to run the mail functions. The program used is defined by configuration settings in the php.ini file. Please visit ourPHP Mail Reference Manualto read more.


PHP Simple E-Mail

The simplest way to send an email with PHP is to send a plain text email.

In the example below, we first declare variables ($to, $subject, $message, $from, $headers), and then we use these variables in the mail() function to send an E-mail:

<?php
$to = "[email protected]";         // 邮件接收者
$subject = "参数邮件";                // 邮件标题
$message = "Hello! 这是邮件的内容。";  // 邮件正文
$from = "[email protected]";   // 邮件发送者
$headers = "From:" . $from;         // 头部信息设置
mail($to,$subject,$message,$headers);
echo "邮件已发送";
?>


PHP Mail Form

With PHP, you can create a feedback form on your own site. The example below sends a text message to a specified e-mail address:

<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
</head>
<body>

<?php
if (isset($_REQUEST['email'])) { // 如果接收到邮箱参数则发送邮件
    // 发送邮件
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("[email protected]", $subject,
    $message, "From:" . $email);
    echo "邮件发送成功";
} else { // 如果没有邮箱参数则显示表单
    echo "<form method='post' action='mailform.php'>
    Email: <input name='email' type='text'><br>
    Subject: <input name='subject' type='text'><br>
    Message:<br>
    <textarea name='message' rows='15' cols='40'>
    </textarea><br>
    <input type='submit'>
    </form>";
}
?>

</body>
</html>

Example explanation:
  • First, check whether the email input field has been filled in
  • If it has not been filled in (for example, when the page is accessed for the first time), output an HTML form
  • If it has been filled in (after the form has been filled out), send an email from the form
  • After filling in the form and clicking the submit button, the page reloads, and you can see that the email input has been reset and a message showing the email was sent successfully

Note:This simple e-mail sending is not secure. In the next chapter of this tutorial, you will read more about security issues in email scripts, and we will explain how to validate user input to make it more secure.


PHP Mail Reference Manual

To view more information about the PHP mail() function, please visit ourPHP Mail Reference Manual。

Other Extensions