大家在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ī)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# 下載到/ 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
1
|
public $Is_SSL = false; |
2.phpmailer里的SmtpConnect方法里傳遞給smtp對象
1
|
$this ->smtp-> Is_SSL = $this -> Is_SSL ; |
3.smtp里的Connect方法在fsockopen調(diào)用前加上
1
|
if ( $this ->is_ssl){ $host = 'ssl://' . $host ; } |
最后是使用方法,記得調(diào)用phpmailer類哦,代碼里沒有。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
$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í)例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
< 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í)有所幫助。