匯總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郵件實例:
<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郵箱實現(xiàn)郵件發(fā)送功能
- Linux服務(wù)器下PHPMailer發(fā)送郵件失敗的問題解決
- PHP插件PHPMailer發(fā)送郵件功能
- 使用PHPMailer發(fā)送郵件實例
- PHPMailer發(fā)送郵件
- phpmailer綁定郵箱的實現(xiàn)方法
- php使用phpmailer發(fā)送郵件實例解析
- phpmailer簡單發(fā)送郵件的方法(附phpmailer源碼下載)
- phplist及phpmailer(組合使用)通過gmail發(fā)送郵件的配置方法
- PHP借助phpmailer發(fā)送郵件
- thinkphp使用phpmailer發(fā)送郵件的方法
- 使用PHPMailer實現(xiàn)郵件發(fā)送代碼分享
- PHP郵件發(fā)送類PHPMailer用法實例詳解
- phpmailer發(fā)送郵件之后,返回收件人是否閱讀了郵件的方法
- phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法
- 實例分析PHP中PHPMailer發(fā)郵件
相關(guān)文章
PHP+jquery實時顯示網(wǎng)站在線人數(shù)的方法
這篇文章主要介紹了PHP+jquery實時顯示網(wǎng)站在線人數(shù)的方法,較為詳細(xì)的分析了實時顯示在線人數(shù)的原理與代碼實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01
PHP使用trim函數(shù)去除字符串左右空格及特殊字符實例
這篇文章主要介紹了PHP使用trim函數(shù)去除字符串左右空格及特殊字符的用法,結(jié)合實例簡單分析了trim函數(shù)不帶附加參數(shù)去除空格及使用附加參數(shù)去除指定字符的使用技巧,需要的朋友可以參考下2016-01-01
php數(shù)據(jù)庫中將base64圖片數(shù)據(jù)轉(zhuǎn)化為可訪問src的方法詳解
后端同學(xué)在對一些老項目進(jìn)行二次開發(fā)時候,可能會遇到一些小的圖片以base64的方式存儲在數(shù)據(jù)庫中的情況,這樣做的好處是這些圖片數(shù)據(jù)一般不會因為項目重新部署而丟失,但是這么做也會存在一些壞處,本文給大家介紹了php數(shù)據(jù)庫中base64圖片數(shù)據(jù)轉(zhuǎn)化可訪問的src的方法2023-10-10

