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);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值