匯總PHPmailer群發(fā)Gmail的常見問題
大家在PHPmailer群發(fā)Gmail時會遇到許多常見問題,下面為大家總結(jié)了一些常見問題,希望對大家的學(xué)習(xí)有所幫助。
1.Could not authenticate
首先,如果你沒有使用循環(huán)的話,基本上就是賬號或者密碼錯了;
如果使用循環(huán)來群發(fā),send()方法結(jié)束之后記得調(diào)用Smtpclose(),發(fā)一次關(guān)一次,否則就會出現(xiàn)只能發(fā)一封郵件,第二次就崩潰的情況。
2.Gmail
首先,開啟php的ssl權(quán)限
php開啟openssl的方法,大多數(shù)情況下openssl是沒有開啟的,要想啟用需要進(jìn)行下簡單的設(shè)置:
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下開啟方法:
我使用的是錦尚數(shù)據(jù)的云主機(jī),PHP版本:5.2.14
下面方案就以我的主機(jī)為例講解為PHP添加openssl模塊支持。
網(wǎng)上一些答案說要重新編譯PHP,添加configure參數(shù),增加openssl的支持。這里講一個不需要重新編譯的方法。
如果服務(wù)器上存在PHP安裝包文件最好,如果已經(jīng)刪除,去下載和phpinfo頁面顯示版本一樣的PHP安裝文件,我這里是 php-5.2.14.tar.gz
推薦去搜狐鏡像下載,網(wǎng)易鏡像沒有找到。地址為: http://mirrors.sohu.com/php/
用ssh工具連接到主機(jī)。
# 下載到/var/www/php5目錄下 cd /var/www/php5 wget http://mirrors.sohu.com/php/php-5.2.14.tar.gz # 解壓 tar zxvf php-5.2.14.tar.gz # 進(jìn)入PHP的openssl擴(kuò)展模塊目錄 cd php-5.2.14/ext/openssl/ /var/www/php5/bin/phpize # 這里為你自己的phpize路徑,如果找不到,使用whereis phpize查找 # 執(zhí)行后,發(fā)現(xiàn)錯誤 無法找到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-config make make 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
好了,現(xiàn)在就成功添加openssl支持。
但是,Gmail麻煩的地方可不止這樣,Gmail現(xiàn)在的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調(diào)用前加上
if($this->is_ssl){ $host = 'ssl://'.$host; }
最后是使用方法,記得調(diào)用phpmailer類哦,代碼里沒有。
$mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = 'smtp.gmail.com'; // 您的企業(yè)郵局域名 $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 = '***'; //郵件標(biāo)題 $mail->Body = '***'; //郵件內(nèi)容 $mail->AltBody = "text/html"; $mail->AddAddress('***', ""); $mail->Is_SSL = true; $mail->Port = 587; if (!$mail->Send()) { exit($mail->ErrorInfo); } $mail->Smtpclose(); unset($mail);
代碼部分就這些,還有不要忘記在gmail中做好相應(yīng)的設(shè)置哦。
以上三步完成,就可以自由的用phpmailer來發(fā)送gmail郵件了。
再為大家分享一個phpmailer發(fā)送gmail郵件實(shí)例:
<html> <head> <title>PHPMailer - SMTP (Gmail) basic test</title> </head> <body> <?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"); // attachment if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- ThinkPHP3.2利用QQ郵箱/163郵箱通過PHPMailer發(fā)送郵件的方法
- phpmailer發(fā)送郵件功能
- PHPMailer使用QQ郵箱實(shí)現(xiàn)郵件發(fā)送功能
- Linux服務(wù)器下PHPMailer發(fā)送郵件失敗的問題解決
- PHP插件PHPMailer發(fā)送郵件功能
- 使用PHPMailer發(fā)送郵件實(shí)例
- PHPMailer發(fā)送郵件
- phpmailer綁定郵箱的實(shí)現(xiàn)方法
- php使用phpmailer發(fā)送郵件實(shí)例解析
- phpmailer簡單發(fā)送郵件的方法(附phpmailer源碼下載)
- phplist及phpmailer(組合使用)通過gmail發(fā)送郵件的配置方法
- PHP借助phpmailer發(fā)送郵件
- thinkphp使用phpmailer發(fā)送郵件的方法
- 使用PHPMailer實(shí)現(xiàn)郵件發(fā)送代碼分享
- PHP郵件發(fā)送類PHPMailer用法實(shí)例詳解
- phpmailer發(fā)送郵件之后,返回收件人是否閱讀了郵件的方法
- phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法
- 實(shí)例分析PHP中PHPMailer發(fā)郵件
相關(guān)文章
談?wù)勑率秩绾螌W(xué)習(xí)PHP網(wǎng)絡(luò)編程
最近用到了php,雖然php的好的都不懂,也只是做一些簡單的修改和書寫很少的代碼,但感覺php的功能真的很強(qiáng)2008-05-05PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
這篇文章主要介紹了PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫操作類,其中dbconfig類負(fù)責(zé)配置數(shù)據(jù)庫訪問信息,dbtemplate類集合了對數(shù)據(jù)庫的訪問操作,非常具有實(shí)用價值,需要的朋友可以參考下2014-12-12PHP 預(yù)定義變量、魔術(shù)常量和魔術(shù)方法功能與用法小結(jié)
這篇文章主要介紹了PHP 預(yù)定義變量、魔術(shù)常量和魔術(shù)方法,總結(jié)分析了PHP 預(yù)定義變量、魔術(shù)常量和魔術(shù)方法基本概念、原理、功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04rephactor 優(yōu)秀的PHP的重構(gòu)工具
從PHP5開始,提供了強(qiáng)大的面向?qū)ο蠊δ堋J沟肞HP能夠完全按設(shè)計(jì)模式編程。2011-06-06Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)
這篇文章主要介紹了Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù),本文擴(kuò)展了一個ACE緩存驅(qū)動,以便使用阿里云ACE緩存服務(wù),需要的朋友可以參考下2015-02-02php中將一段數(shù)據(jù)存到一個txt文件中并顯示其內(nèi)容
這篇文章主要介紹了php中將一段數(shù)據(jù)存到一個txt文件中,并獲取其內(nèi)容顯示的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08