利用php mail()进行邮件发送实现方法
利用php mail()进行邮件发送实现方法,mail() 函数允许您从脚本中直接发送电子邮件。
如果邮件的投递被成功地接收,则返回 true,否则返回 false。
语法:mail(to,subject,message,headers,parameters)
参数描述
to必需。规定邮件的接收者。
subject必需。规定邮件的主题。该参数不能包含任何换行字符。
message必需。规定要发送的消息。
headers必需。规定额外的报头,比如 From, Cc 以及 Bcc。
parameters必需。规定 sendmail 程序的额外参数。
简单的发送html内容:
<html> <head> <title>Simple Send Mail Form</title> </head> <body> <h1>Mail Form</h1> <form name="form1" method="post" action="SimpleEmail.php"> <table> <tr><td><b>To</b></td><td><input type="text" name="mailto" size="35"></td></tr> <tr><td><b>Subject</b></td> <td><input type="text" name="mailsubject" size="35"></td></tr> <tr><td><b>Message</b></td> <td><textarea name="mailbody" cols="50" rows="7"></textarea></td> </tr> <tr><td colspan="2"> <input type="submit" name="Submit" value="Send"> </td> </tr> </table> </form> </body> </html>
SimpleEmail.php
<?php if (emptyempty ($mailto) ) { die ( "Recipient is blank! ") ; } if (emptyempty ($mailsubject) ){ $mailsubject=" " ; } if (emptyempty ($mailbody) ) { $mailbody=" " ; } $result = mail ($mailto, $mailsubject, $mailbody) ; if ($result) { echo "Email sent successfully!" ; }else{ echo "Email could not be sent." ; } ?>
带上抄送与密送功能
<HTML> <HEAD> <TITLE>Send email with CC and BCC</TITLE> </HEAD> <BODY> <FORM action="sendemailWithCC_BCC.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>
sendemailWithCC_BCC.php
<html> <head> <title>Mail Sent</title> </head> <body> <?php $message= " " ; if (emptyempty ( $mailtoname) || emptyempty ( $mailtomail) ) { die ( "Recipient is blank! ") ; }else{ $to = $mailtoname . " <" . $mailtomail . ">" ; } if ( emptyempty ( $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 (!emptyempty($mailcc)) { $mailheader.= "Cc: " . $mailcc ."n"; } if (!emptyempty($mailbcc)) { $mailheader.= "Bcc: " . $mailbcc ."n"; } if (emptyempty($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>
在 message 参数规定的消息中,行之间必须以一个 LF(n)分隔,每行不能超过 70 个字符,Windows 下,当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉,要避免此问题,将单个句号替换成两个句号.
教程网址:http://www.phprm.com/develop/fs1501.html
欢迎收藏∩_∩但请保留本文链接。