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('[email protected]','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	= "[email protected]"; //這個可以替換成自己的郵箱
			$this->email->Password	= "xxxx"; //注意 這裏是寫smtp的授權碼 寫的不是163密碼,此授權碼不可用
			//$mail->IsSendmail(); //如果沒有sendmail組件就註釋掉,否則出現“Could not execute: /var/qmail/bin/sendmail ”的錯誤提示 
			$this->email->AddReplyTo("[email protected]","Amxl");//回覆地址 
			$this->email->From	= "[email protected]"; //發件用戶
			$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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章