1.新建一个发送页面 ceshi.html--无需修改
PHP利用smtp类发送邮件范例收件人:
标 题:
内 容:
2.新建一个接收发送页 sendmail.php----有三个地方要改为个人的信息
/**
* 注:本邮件类都是经过我测试成功了的,如果大家发送邮件的时候遇到了失败的问题,请从以下几点排查:
* 1. 用户名和密码是否正确;
* 2. 检查邮箱设置是否启用了smtp服务;
* 3. 是否是php环境的问题导致;
* 4. 将26行的$smtp->debug = false改为true,可以显示错误信息,然后可以复制报错信息到网上搜一下错误的原因
*/
require_once "email.class.php";
//******************** 配置信息 ********************************
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "xxxxxx@163.com";//SMTP服务器的用户邮箱 ----要改
$smtpemailto = $_POST['toemail'];//发送给谁
$smtpuser = "xxxxx";//SMTP服务器的用户帐号 不包含@后边的文本----要改
$smtppass = "xxxx";//SMTP服务器的用户授权码----要改
$mailtitle = $_POST['title'];//邮件主题
$mailcontent = "
".$_POST['content']."
";//邮件内容$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
//************************ 配置信息 ****************************
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
echo "
if($state==""){
echo "对不起,邮件发送失败!请检查邮箱填写是否有误。";
echo "点此返回";
exit();
}
echo "恭喜!邮件发送成功!!";
echo "点此返回";
echo "
?>
3.新建函数页面 email.class.php--无需修改
class Smtp
{
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
var $sock;
function Smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 3600;
$this->auth = $auth;
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost";
$this->log_file = "";
$this->sock = FALSE;
}
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";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: \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, $header, $body)) {
$this->log_write("E-mail has been sent to \n");
} else {
$this->log_write("Error: Cannot send email to \n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n")
本文标签:php
版权声明:若无特殊注明,本文皆为《
傲世零零 》原创,转载请保留文章出处。
本文链接:PHP版本-通过163邮箱的SMTP服务器端口发送邮件出去 http://www.wangjunjiang.com/?post=130
正文到此结束