利用smtp.163.com 发送邮件(本地无需服务器)

第一步:需要下载PHPMailer文件包phpmailer-1.73.tar.gz 来自开源社区:

http://phpmailer.sourceforge.net/

第二步:确认你的服务器系统已经支持socket 如下图,通过phpinfo();查看是否支持sockets


  如果没有这一项就请注意: socket 是属于PHP扩展部分,编译时必须给定一个用于./configure --enable-sockets 的配置选项。
第三步:把文件解压到你的web服务器目录下,调用类就可以了,说明:首先包含 class.phpmailer.php,然后创建对象,设置参数,调用成员函数。具体请见下面的示例代码:

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


php

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


/*******************************

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


*

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


*******************************/

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


set_time_limit(120);//

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


require("class.phpmailer.php");

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


function smtp_mail ( $sendto_email, $subject, $body, $extra_hdrs, $user_name) {

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail = new PHPMailer();

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->IsSMTP(); // send via SMTP

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->Host = "smtp.163.com"; // SMTP servers 注意:好像听说是只有2006年以前申请的163邮箱具有此功能

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->SMTPAuth = true; // turn on SMTP authentication

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->Username = "fuyongjie.100"; // SMTP username 注意:普通邮件认证不需要加 @域名

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->Password = "************"; // SMTP password(我把我的密码给隐了)

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->From = "[email protected]"; // 发件人邮箱

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->FromName = "fuyongjie"; // 发件人

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->CharSet = "utf-8"; // 这里指定字符集!

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->Encoding = "base64";

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->AddAddress($sendto_email,"yangli"); // 收件人邮箱和姓名

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->AddReplyTo("[email protected]","163.com");

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


//$mail->WordWrap = 50; // set word wrap

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment('stat.rar','更改后的名字.rar');

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->IsHTML(true); // send as HTML

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


// 邮件主题

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->Subject = $subject;

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


// 邮件内容

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->Body = '

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


欢迎来到http://prdownloads.sourceforge.net/phpmailer/phpmailer-1.73.zip?download

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


下载phpmailer!

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


';

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


$mail->AltBody ="text/html";

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


if(!$mail->Send())

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


{

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


echo "邮件发送有误 ";

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


echo "邮件错误信息: " . $mail->ErrorInfo;

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


exit;

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


}

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


else {

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


echo "$user_name 邮件发送成功!163to qq";

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


}

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


}

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


// 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名)

http://www.phpweblog.net/Images/OutliningIndicators/None.gif

 

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


smtp_mail('[email protected]', '从163发来的!', 'NULL', '163.com', 'fuyongjie');

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


?>

http://www.phpweblog.net/Images/OutliningIndicators/None.gif


要注意的内容:

  1. 邮件的字符集设置, $mail->CharSet = "GB2312"; // 这里指定字符集!在这里我只指定为GB2312因为这样Outlook能正常显示邮件主题,我尝试过设为utf-8但在Outlook下显示乱码。
  2. 如果是发送html格式的邮件,那么记得也指定
  3. 如果你想用它来群发邮件的话,记得修改包含文件函数,如:
  require("phpmailer/class.phpmailer.php");
  改为
  require_once("phpmailer/class.phpmailer.php");
  否则的话会产生类的重定义。

你可能感兴趣的:(smtp)