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);
?>

转载于:https://www.cnblogs.com/frankliiu-java/archive/2010/04/08/1707344.html

### PHP `mail()` 函数详解 `mail()` 是 PHP 中用于发送电子邮件的一个内置函数。其基本语法如下: ```php bool mail ( string $to , string $subject , string $message [, mixed $additional_headers [, string $additional_parameters ]] ) ``` #### 参数说明 - **$to**: 接收邮件的地址,可以是一个或多个邮箱地址,用逗号分隔。 - **$subject**: 邮件的主题,注意主题中不能包含换行符 `\n` 或者回车符 `\r`[^2]。 - **$message**: 要发送的内容,即邮件正文部分。 - **$additional_headers**(可选): 可以指定额外的头部信息,比如发件人、抄送、密送等。 - **$additional_parameters**(可选): 提供给底层邮件程序使用的参数。 #### 示例代码 下面提供几个常见的使用场景及其对应的实现代码。 ##### 发送简单文本邮件 这是最基础的形式,仅需指定接收方、主题以及消息内容即可完成操作。 ```php <?php $to = "recipient@example.com"; $subject = "测试邮件"; $message = "这是一封来自PHP脚本发出的测试邮件."; $headers = "From: sender@example.com"; if(mail($to, $subject, $message, $headers)){ echo "邮件已成功发送!"; }else{ echo "邮件发送失败!"; } ?> ``` ##### 添加HTML格式支持 为了使邮件更加美观或者功能更强大,可以通过设置 `$headers` 来允许 HTML 格式的邮件体。 ```php <?php $to = "recipient@example.com"; $subject = "带有HTML样式的邮件"; $message = " <html> <head> <title>HTML Email</title> </head> <body> <p><strong>Hello!</strong></p> <table border='1'> <tr> <th>Name</th><th>Email</th> </tr> <tr> <td>John Doe</td><td>johndoe@example.com</td> </tr> </table> </body> </html> "; // 设置头文件为text/html类型 $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type:text/html;charset=UTF-8\r\n"; $headers .= 'From: Example Sender <sender@example.com>' . "\r\n"; if(mail($to,$subject,$message,$headers)){ echo "HTML格式邮件已成功发送!"; } else { echo "HTML格式邮件发送失败!"; } ?> ``` ##### 多个收件人与附件处理 当需要向多位用户群发并附带文件时,可通过附加 MIME 边界来构建复杂的邮件结构。 ```php <?php $to = "personA@example.com,personB@example.com"; $subject = "含附件的邮件示例"; $text = "这是一个简单的纯文字描述。\n请查看附件中的更多信息。"; $fileatt = "/path/to/file.txt"; // 文件路径 $fileatt_type = "application/octet-stream"; // MIME 类型 $fileatt_name = basename($fileatt); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // 定义头部信息 $headers = "From: example_sender@example.com\r\n". "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // 组合邮件主体 $email_message = "--{$mime_boundary}\r\n" . "Content-Type: text/plain; charset=\"utf-8\"\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n" . chunk_split(base64_encode($text)) . "--{$mime_boundary}\r\n" . "Content-Type: {$fileatt_type};\r\n" . " name=\"{$fileatt_name}\"\r\n" . "Content-Disposition: attachment;\r\n" . " filename=\"{$fileatt_name}\"\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n" . chunk_split(base64_encode(file_get_contents($fileatt))) . "--{$mime_boundary}--\r\n"; if(mail($to,$subject,"",$headers)){ echo "含附件的邮件已成功发送!"; } else { echo "含附件的邮件发送失败!"; } ?> ``` 以上展示了不同复杂度下的 `mail()` 应用实例,实际开发过程中可能还需要考虑服务器端 SMTP 的配置等问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值