大家在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郵件實例:
<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"); // attachmentif(!$mail->Send()) {echo "Mailer Error: " . $mail->ErrorInfo;} else {echo "Message sent!";}?></body></html>
以上就是本文的全部內容,希望對大家的學習有所幫助。