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