PHP funkcja mail ()
Definicja i Wykorzystanie
Funkcja mail () pozwala na wysyłanie wiadomości e-mail bezpośrednio z poziomu skryptu.
Jeśli e-mail został pomyślnie przyjęty dostawę, zwraca TRUE, w przeciwnym wypadku zwraca FALSE.
gramatyka
mail(to,subject,message,headers,parameters)
参数 | 描述 |
---|---|
to | 必需。规定电子邮件的接收者。 |
subject | 必需。规定电子邮件的主题。注释:该参数不能包含任何换行字符。 |
message | 必需。定义要发送的消息。用 LF(\n)将每一行分开。行不应超过70个字符。
Windows 注释:当 PHP 直接连接到 SMTP 服务器时,如果在消息的某行开头发现一个句号,则会被删掉。要解决这个问题,请将单个句号替换成两个句号: |
headers | 可选。规定额外的报头,比如 From、Cc 和 Bcc。附加标头应该用 CRLF(\r\n)分开。
注释:发送电子邮件时,它必须包含一个 From 标头。可通过该参数进行设置或在 php.ini 文件中进行设置。 |
parameters | 可选。规定 sendmail 程序的额外参数(在 sendmail_path 配置设置中定义)。例如:当 sendmail 和 -f sendmail 的选项一起使用时,sendmail 可用于设置发件人地址。 |
Porady i wskazówki
Uwaga: Należy pamiętać, że dostawa e-mail zostanie zaakceptowana, nie oznacza, że e-mail dotrze do programu docelowego.
Przykład 1
Wyślij prosty e-mail:
<?php
$txt = "First line of textnSecond line of text";
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($txt,70);
// Send email
mail("[email protected]","My subject",$txt);
?>
$txt = "First line of textnSecond line of text";
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($txt,70);
// Send email
mail("[email protected]","My subject",$txt);
?>
Przykład 2
Wyślij e-maila z dodatkowym nagłówkiem:
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "rn" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "rn" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
Przykład 3
Wyślij e-mail HTML:
<?php
$to = "[email protected], [email protected]";
$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>
";
// Always set content-type when sending HTML email
$headers = "MIME-版本: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
// More headers
$headers .= 'From: <[email protected]>' . "rn";
$headers .= 'Cc: [email protected]' . "rn";
mail($to,$subject,$message,$headers);
?>
$to = "[email protected], [email protected]";
$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>
";
// Always set content-type when sending HTML email
$headers = "MIME-版本: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
// More headers
$headers .= 'From: <[email protected]>' . "rn";
$headers .= 'Cc: [email protected]' . "rn";
mail($to,$subject,$message,$headers);
?>
Kompletna instrukcja PHP Poczta Reference