php 邮件发送如何进行的呢?
php邮件发送是通过smtp协议进行的。
下面是一个php邮件发送的类的一个函数。
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { $mail_from = $this->get_address($this->strip_comment($from)); $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); $header = "MIME-Version:1.0\r\n"; if($mailtype=="HTML"){ $header .= "Content-Type:text/html\r\n"; } $header .= "To: ".$to."\r\n"; if ($cc != "") { $header .= "Cc: ".$cc."\r\n"; } $header .= "From: 报名邮件.<".$from.">\r\n"; $header .= "Subject: ".$subject."\r\n"; $header .= $additional_headers; $header .= "Date: ".date("r")."\r\n"; $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; $utfheader=iconv("UTF-8","GB2312",$header); list($msec, $sec) = explode(" ", microtime()); $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; $TO = explode(",", $this->strip_comment($to)); if ($cc != "") { $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); } if ($bcc != "") { $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); } $sent = TRUE; foreach ($TO as $rcpt_to) { $rcpt_to = $this->get_address($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) { $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); $sent = FALSE; continue; } if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $utfheader, $body)) { $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); } else { $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); $sent = FALSE; } fclose($this->sock); $this->log_write("Disconnected from remote host\n"); } return $sent; }
我们如何调用这个类呢?
再看示例
include("sendmail.php");//发送邮件类 ####################--发邮件--#################### $smtpserver = "smtp.126.com";//SMTP服务器 $smtpserverport = 25;//SMTP服务器端口 $smtpusermail = "test@126.com";//SMTP服务器的用户邮箱 $smtpuser = "test";//SMTP服务器的用户帐号 $smtppass = "123456";//SMTP服务器的用户密码 $smtpemailto = "dianzhong@126.com";//发送给谁 $mailsubject = $username.'报名!';//邮件主题 $mailtime = date("Y-m-d H:i:s"); $mailbody = $content;//邮件内容 $utfmailbody = iconv("UTF-8","GB2312",$mailbody);//转换邮件编码 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
在这里需要一个smtp服务器。我们可以注册一个126的邮箱。 在上面的代码中,修改成你自己注册的邮箱地址和用户名、密码即可。
这样就可以用php发邮件了。
我做了一个用php发邮件的例子。
下载后记得修改里面的邮箱用户名和密码哦。
有问题可以留言。
原文地址: http://www.gosoa.com.cn/php-email