企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# PHP mail() 函数 ## 定义和用法 mail() 函数允许您从脚本中直接发送电子邮件。 如果邮件的投递被成功地接收,则返回 true,否则返回 false。 ### 语法 ``` mail(to,subject,message,headers,parameters) ``` | 参数 | 描述 | | --- | --- | | to | 必需。规定邮件的接收者。 | | subject | 必需。规定邮件的主题。该参数不能包含任何换行字符。 | | message | 必需。规定要发送的消息。 | | headers | 必需。规定额外的报头,比如 From, Cc 以及 Bcc。 | | parameters | 必需。规定 sendmail 程序的额外参数。 | ### 说明 在 _message_ 参数规定的消息中,行之间必须以一个 LF(\n)分隔。每行不能超过 70 个字符。 (Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。 ``` <?php $text = str_replace("\n.", "\n..", $text); ?> ``` ## 提示和注释 注释:您需要紧记,邮件投递被接受,并不意味着邮件到达了计划的目的地。 ## 例子 ### 例子 1 发送一封简单的邮件: ``` <?php $txt = "First line of text\nSecond line of text"; // 如果一行大于 70 个字符,请使用 wordwrap()。 $txt = wordwrap($txt,70); // 发送邮件 mail("somebody@example.com","My subject",$txt); ?> ``` ### 例子 2 发送带有额外报头的 email: ``` <?php $to = "somebody@example.com"; $subject = "My subject"; $txt = "Hello world!"; $headers = "From: webmaster@example.com" . "\r\n" . "CC: somebodyelse@example.com"; mail($to,$subject,$txt,$headers); ?> ``` ### 例子 3 发送一封 HTML email: ``` <?php $to = "somebody@example.com, somebodyelse@example.com"; $subject = "HTML email"; $message = " <html> <head> <title>HTML email</title> </head> <body> <p>This email contains HTML Tags!</p> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>John</td> <td>Doe</td> </tr> </table> </body> </html> "; // 当发送 HTML 电子邮件时,请始终设置 content-type $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // 更多报头 $headers .= 'From: <webmaster@example.com>' . "\r\n"; $headers .= 'Cc: myboss@example.com' . "\r\n"; mail($to,$subject,$message,$headers); ?> ```