PHP mail() 函数

定义和用法

mail() 函数允许您从脚本中直接发送电子邮件。

如果邮件的投递被成功地接收,则返回 true,否则返回 false。

定义和用法

mail(描述语法必需。规定邮件的主题。该参数不能包含任何换行字符。语法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

ສົ່ງອີເມວທີ່ມີຫົວຫຼັກພິມອີກຄັນ:

<?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:

<?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>Bill</td>
<td>Gates</td>
</tr>
</table>
</body>
</html>
";
// Set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>