本文实例讲述了php带抄送和密件抄送的邮件发送方法。分享给大家供大家参考。具体分析如下:
程序中用到了php的mail函数,该函数定义如下:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
如果邮件发送成功返回true,否则返回false
<html>
<head>
<title>send email with cc and bcc</title>
</head>
<body>
<form action="sendemail.php" method=post name=form1>
<table>
<tbody>
<tr>
<td>
<div align=right><b>to</b></div></td>
<td>
<p>name <input name=mailtoname size=35><br />e-mail
<input name=mailtomail size=35></p></td></tr>
<tr>
<td>
<div align=right><b>cc</b></div></td>
<td><input name=mailcc size=35> </td></tr>
<tr>
<td>
<div align=right><b>bcc</b></div></td>
<td><input name=mailbcc size=35> </td></tr>
<tr>
<td>
<div align=right><b>priority</b></div></td>
<td><select name=mailpriority>
<option value=1>highest</option>
<option value=2>high</option>
<option selected value=3>normal</option>
<option value=4>low</option>
<option value=5>lowest</option>
</select>
</td></tr>
<tr>
<td><div align=right><b>subject</b></div></td>
<td><input name=mailsubject size=35></td></tr>
<tr>
<td>
<div align=right><b>message</b> </div></td>
<td><textarea cols=50 name=mailbody rows=7></textarea></td></tr>
<tr>
<td colspan=2>
<div align=center>
<input name=submit type=submit value=submit></div>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
后端php代码,保存为sendmail.php
<html>
<head>
<title>send mail script</title>
</head>
<body>
<?php
$message= " " ;
if (empty ( $mailtoname) || empty ( $mailtomail) ) {
die ( "recipient is blank! ") ;
}else{
$to = $mailtoname . " <" . $mailtomail . ">" ;
}
if ( empty ( $mailsubject) ) {
$mailsubject=" ";
}
if (($mailpriority>0) && ($mailpriority<6)) {
$mailheader = "x-priority: ". $mailpriority ."n";
}
$mailheader.= "from: " . "sales team <sales@yourdomain.com>n";
$mailheader.= "x-sender: " . "support@yourdomain.comn";
$mailheader.= "return-path: " . "support@yourdomain.comn";
if (!empty($mailcc)) {
$mailheader.= "cc: " . $mailcc ."n";
}
if (!empty($mailbcc)) {
$mailheader.= "bcc: " . $mailbcc ."n";
}
if (empty($mailbody)) {
$mailbody=" ";
}
$result = mail ($to, $mailsubject, $mailbody, $mailheader);
echo "<center><b>mail sent to ". "$to". "<br />";
echo $mailsubject. "<br />";
echo $mailbody. "<br />";
echo $mailheader. "<br />";
if ($result) {
echo "<p><b>email sent successfully!</b></p>";
}else{
echo "<p><b>email could not be sent. </b></p>";
}
?>
<div align="center">
<table><tr><td width="66"><div align="right"><b>to</b></div></td>
<td width="308"><b>
<?php echo $mailtoname . " [". $mailtomail . " ]";?>
</b></td></tr>
<tr><td width="66"><div align="right"><b>cc</b></div></td>
<td width="308"><b><?php echo $mailcc;?></b></td></tr>
<tr><td width="66"><div align="right"><b>bcc</b></div></td>
<td width="308"><b><?php echo $mailbcc; ?></b></td></tr>
<tr><td width="66"><div align="right"><b>priority</b></div></td>
<td width="308"><b><?php echo $mailpriority;?></b></td></tr>
<tr><td width="66"><div align="right"><b>subject </b></div></td>
<td width="308"><b><?php echo $mailsubject;?></b></td></tr>
<tr><td width="66"><div align="right"><b>message</b></div></td>
<td width="308"><b><?php echo $mailbody;?></b></td></tr>
</table>
</div>
</body>
</html>
希望本文所述对大家的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!