匯總PHPmailer群發Gmail的常見問題,phpmailer群發gmail
大家在PHPmailer群發Gmail時會遇到許多常見問題,下面為大家總結了一些常見問題,希望對大家的學習有所協助。
1.Could not authenticate
首先,如果你沒有使用迴圈的話,基本上就是帳號或者密碼錯了;
如果使用迴圈來群發,send()方法結束之後記得調用Smtpclose(),發一次關一次,否則就會出現只能發一封郵件,第二次就崩潰的情況。
2.Gmail
首先,開啟php的ssl許可權
php開啟openssl的方法,大多數情況下openssl是沒有開啟的,要想啟用需要進行下簡單的設定:
windows下開啟方法:
1: 首先檢查php.ini中;extension=php_openssl.dll是否存在, 如果存在的話去掉前面的注釋符‘;', 如果不存在這行,那麼添加extension=php_openssl.dll。
2: 講php檔案夾下的: php_openssl.dll, ssleay32.dll, libeay32.dll 3個檔案拷貝到 WINDOWS\system32\ 檔案夾下。
3: 重啟apache或者iis
至此,openssl功能就開啟了。
Linux下開啟方法:
我使用的是錦尚資料的雲主機,PHP版本:5.2.14
下面方案就以我的主機為例講解為PHP添加openssl模組支援。
網上一些答案說要重新編譯PHP,添加configure參數,增加openssl的支援。這裡講一個不需要重新編譯的方法。
如果伺服器上存在PHP安裝包檔案最好,如果已經刪除,去下載和phpinfo頁面顯示版本一樣的PHP安裝檔案,我這裡是 php-5.2.14.tar.gz
推薦去搜狐鏡像下載,網易鏡像沒有找到。地址為: http://mirrors.sohu.com/php/
用ssh工具串連到主機。
# 下載到/var/www/php5目錄下cd /var/www/php5wget http://mirrors.sohu.com/php/php-5.2.14.tar.gz# 解壓tar zxvf php-5.2.14.tar.gz# 進入PHP的openssl擴充模組目錄cd php-5.2.14/ext/openssl//var/www/php5/bin/phpize # 這裡為你自己的phpize路徑,如果找不到,使用whereis phpize尋找# 執行後,發現錯誤 無法找到config.m4 ,config0.m4就是config.m4。直接重新命名mv config0.m4 config.m4/var/www/php5/bin/phpize./configure --with-openssl --with-php-config=/var/www/php5/bin/php-configmakemake install# 安裝完成後,會返回一個.so檔案(openssl.so)的目錄。在此目錄下把openssl.so 檔案拷貝到你在php.ini 中指定的 extension_dir 下(在php.ini檔案中尋找:extension_dir =),我這裡的目錄是 var/www/php5/lib/php/extensions# 編輯php.ini檔案,在檔案最後添加extension=openssl.so# 重啟Apache即可/usr/local/apache2/bin/apachectl restart
好了,現在就成功添加openssl支援。
但是,Gmail麻煩的地方可不止這樣,Gmail現在的smtp和pop3都是ssl加密的
Step1. php openssl module(extension) support
Step2. download phpmailer library
Step3. change code 'class.phpmailer.php' and 'class.smtp.php'
1.phpmailer和smtp裡加property Is_SSL
public $Is_SSL = false;
2.phpmailer裡的SmtpConnect方法裡傳遞給smtp對象
$this->smtp-> Is_SSL = $this-> Is_SSL ;
3.smtp裡的Connect方法在fsockopen調用前加上
if($this->is_ssl){ $host = 'ssl://'.$host; }
最後是使用方法,記得調用phpmailer類哦,代碼裡沒有。
$mail = new PHPMailer();$mail->IsSMTP();$mail->Host = 'smtp.gmail.com'; // 您的企業郵局網域名稱$mail->SMTPAuth = true; // turn on SMTP authentication$mail->SMTPSecure = "tls";$mail->Username = '***@gmail.com';$mail->Password = '******';$mail->From = '***';$mail->FromName = '***';$mail->CharSet = 'UTF-8';$mail->Encoding = "base64";$mail->IsHTML(true); // send as HTML$mail->Subject = '***'; //郵件標題$mail->Body = '***'; //郵件內容$mail->AltBody = "text/html";$mail->AddAddress('***', "");$mail->Is_SSL = true;$mail->Port = 587;if (!$mail->Send()) { exit($mail->ErrorInfo);}$mail->Smtpclose();unset($mail);
代碼部分就這些,還有不要忘記在gmail中做好相應的設定哦。
以上三步完成,就可以自由的用phpmailer來發送gmail郵件了。
再為大家分享一個phpmailer發送gmail郵件執行個體:
PHPMailer - SMTP (Gmail) basic test<?php//error_reporting(E_ALL);error_reporting(E_STRICT);date_default_timezone_set('America/Toronto');require_once('../class.phpmailer.php');//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded$mail = new PHPMailer();$body = file_get_contents('contents.html');$body = eregi_replace("[\]",'',$body);$mail->IsSMTP(); // telling the class to use SMTP$mail->Host = "mail.gmail.com"; // SMTP server$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)// 1 = errors and messages// 2 = messages only$mail->SMTPAuth = true; // enable SMTP authentication$mail->SMTPSecure = "ssl"; // sets the prefix to the servier$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server$mail->Port = 465; // set the SMTP port for the GMAIL server$mail->Username = "***@gmail.com"; // GMAIL username$mail->Password = "***"; // GMAIL password$mail->SetFrom('****@gmail.com', 'First Last');$mail->AddReplyTo("***@gmail.com","First Last");$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test$mail->MsgHTML($body);$address = "***@gmail.com";$mail->AddAddress($address, "John Doe");$mail->AddAttachment("images/phpmailer.gif"); // attachment$mail->AddAttachment("images/phpmailer_mini.gif"); // attachmentif(!$mail->Send()) {echo "Mailer Error: " . $mail->ErrorInfo;} else {echo "Message sent!";}?>
以上就是本文的全部內容,希望對大家的學習有所協助。
您可能感興趣的文章:
- PHP擷取163、gmail、126等郵箱連絡人地址【已測試2009.10.10】
- php 類比GMAIL,HOTMAIL(MSN),YAHOO,163,126郵箱登入的詳細介紹
- PHP實現郵件群發的源碼
- phpmailer發送gmail郵件執行個體詳解
- java、php、C#、asp實現簡訊群發功能的方法
- php實現公眾號無限群發
- PHP+swoole實現簡單多人線上聊天群發
- PHP郵件群發機實現代碼
http://www.bkjia.com/PHPjc/1104341.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1104341.htmlTechArticle匯總PHPmailer群發Gmail的常見問題,phpmailer群發gmail 大家在PHPmailer群發Gmail時會遇到許多常見問題,下面為大家總結了一些常見問題,希望對大...