Latest web development tutorials

PHP Mail

PHP allows you to send e-mail directly from a script.

PHP mail () function

PHP mail () function is used to send e-mail from a script.



参数 描述
to 必需。规定 email 接收者。
subject 必需。规定 email 的主题。注释:该参数不能包含任何新行字符。
message 必需。定义要发送的消息。应使用 LF (\n) 来分隔各行。每行应该限制在 70 个字符内。
headers 可选。规定附加的标题,比如 From、Cc 和 Bcc。应当使用 CRLF (\r\n) 分隔附加的标题。
parameters 可选。对邮件发送程序规定额外的参数。

Note: PHP run-mail function requires an installed and running mail system (such as: sendmail, postfix, qmail, etc.).The procedure used is defined by the configuration settings in the php.ini file. Please our PHP Mail Reference Manual read more.

PHP Easy E-Mail

The easiest way to send email via PHP is to send a text email.

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

$to = "";         // 邮件接收者
$subject = "参数邮件";                // 邮件标题
$message = "Hello! 这是邮件的内容。";  // 邮件正文
$from = "";   // 邮件发送者
$headers = "From:" . $from;         // 头部信息设置
echo "邮件已发送";

PHP Mail Form

PHP, for you to make your site a feedback form. The following examples are sent to the specified e-mail address a text message:

<meta charset="utf-8">

if (isset($_REQUEST['email'])) { // 如果接收到邮箱参数则发送邮件
	// 发送邮件
	$email = $_REQUEST['email'] ;
	$subject = $_REQUEST['subject'] ;
	$message = $_REQUEST['message'] ;
	mail("", $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>
	<textarea name='message' rows='15' cols='40'>
	<input type='submit'>


Examples explain:
  • First, check whether the message input box to fill in
  • If you do not fill in (for example, when the page is accessed for the first time), the output HTML form
  • If you have to fill in (after the form is filled in), send e-mail from a form
  • When the form is complete click on the submit button, reload the page, you can see the message input is reset, and the message has been sent successfully message is displayed

Note: This simple send e-mail to be unsafe, the next chapter of this tutorial, you will read more about e-mail security risks in the script, we'll explain how to validate user input to make it more secure.

PHP Mail Reference Manual

To see more () information about the PHP mail functions, visit our PHP Mail Reference Manual .