php5.3 发送邮件phpemail的使用 (适用php5.3)

1、下载phpemail资源包

github上的PHPMailer,需要php版本>=5.5;用在php5.3上回有各种问题,所以找了个适用于php5.3的phpemail  下载地址(如何改下载 所需积分,还请留言告知,上传之后默认要5积分!!无奈)

2、php5.3的实例(写法大致一样)

展示

事先准备邮件服务器

实例使用163的邮箱,开启授权码。

代码

//引用工具类

include_once("../util/phpmailer/class.phpmailer.php"); 
include_once("../util/phpmailer/class.smtp.php");
...
...
//类中实例化
	public function __construct($get)
        {
		$this->email = new PHPMailer(true);
        }
	//测试
	public function test(){
		$this->send_mail('amxl@xxx.com','amxl','这里提示邮件' .time(),'<h1>测试文本</h1>' . date('Y-m-d H:i:s'));
	}
	//一键导入时发送邮件通知
	public function send_mail($to,$fromname,$title,$content){
		try { 
			$this->email->IsSMTP(); 
			$this->email->CharSet   = 'UTF-8'; //设置邮件的字符编码,这很重要,不然中文乱码 
			$this->email->SMTPAuth	= true; //开启认证 
			$this->email->Port	= 25; //端口请保持默认
			$this->email->Host	= "smtp.163.com"; //使用163邮箱发送
			$this->email->Username	= "xx@163.com"; //这个可以替换成自己的邮箱
			$this->email->Password	= "xxxx"; //注意 这里是写smtp的授权码 写的不是163密码,此授权码不可用
			//$mail->IsSendmail(); //如果没有sendmail组件就注释掉,否则出现“Could not execute: /var/qmail/bin/sendmail ”的错误提示 
			$this->email->AddReplyTo("xxx@163.com","Amxl");//回复地址 
			$this->email->From	= "xx@163.com"; //发件用户
			$this->email->FromName	= $fromname; //发件用户名

			$this->email->AddAddress($to); //收件用户
			$this->email->Subject	= $title; 
			$this->email->Body	= $content;
			$this->email->AltBody	= "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示,可以省略 
			$this->email->WordWrap	= 80; // 设置每行字符串的长度 
			//$mail->AddAttachment("f:/test.png"); //可以添加附件 
			$this->email->IsHTML(true); 
			$this->email->Send();
			//echo "邮件已发送";
		} catch (phpmailerException $e) { 
			echo "邮件发送失败:".$e->errorMessage(); 
		} 
	}

3、遇到的问题

SMTP Error: Could not authenticate 

首先,在php.ini中去掉下面的两个分号

;extension=php_sockets.dll
;extension=php_openssl.dll

然后重启一下;

方法一:检查邮件服务器账号密码(我就犯了这个蠢事,两个邮箱 交叉用 用户名和授权码,恐怕是脑子进水了!)

方法二:修改class.phpmailer.php中的smtp大小写,(针对新版PHPMailer)

function IsSMTP() {
    //$this->Mailer = 'smtp';
    $this->Mailer = 'SMTP';
}

方法三:将fsockopen函数替换成pfsockopen函数(或者使用stream_socket_client函数)(针对新版PHPMailer)

$this->smtp_conn = stream_socket_client("tcp://".$host.":".$port, $errno,  $errstr,  $tval);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值