php中发送邮件函数解析

作者: dreamfly 分类: linux,php 发布时间: 2016-11-28 00:00

无论是平时工作还是日常开发,我们都需要发送邮件,而在php中,对于邮件的发送,我们有非常好用的函数可以直接使用,下面就让我们看看php是如何发送邮件的。

<?php
// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
 <title>Birthday Reminders for August</title>
</head>
<body>
 <p>Here are the birthdays upcoming in August!</p>
 <table>
 <tr>
 <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
 </tr>
 <tr>
 <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
 </tr>
 <tr>
 <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
 </tr>
 </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\\r\\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\\r\\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\\r\\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\\r\\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\\r\\n";

// Mail it
mail($to, $subject, $message, $headers);

下面主要介绍下mail函数的参数说明。

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

additional_headers (optional)

这是一个可以插入到邮件头部的参数,这里的字符串会被插入到邮件头。

常用的头部比如 From, Cc, Bcc 等。

需要注意的是,如果有多个头部,需要使用CRLF(\r\n)进行分隔。

当发送邮件的时候, 邮件必须包含一个From头部,这个可以通过additional_headers 来指定。否则可能在windows上会出现发送失败的错误。

需要注意的是,发送的邮件的内容最好只使用LF分隔,这样不会出现无法接收的问题。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!