溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHPmailer群發(fā)Gmail的常見問題有哪些

發(fā)布時(shí)間:2021-08-31 11:24:59 來源:億速云 閱讀:123 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)PHPmailer群發(fā)Gmail的常見問題有哪些,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

1.Could not authenticate

首先,如果你沒有使用循環(huán)的話,基本上就是賬號或者密碼錯(cuò)了;

如果使用循環(huán)來群發(fā),send()方法結(jié)束之后記得調(diào)用Smtpclose(),發(fā)一次關(guān)一次,否則就會(huì)出現(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個(gè)文件拷貝到 WINDOWS\system32\  文件夾下。

3: 重啟apache或者iis

至此,openssl功能就開啟了。

Linux下開啟方法:

我使用的是錦尚數(shù)據(jù)的云主機(jī),PHP版本:5.2.14

下面方案就以我的主機(jī)為例講解為PHP添加openssl模塊支持。

網(wǎng)上一些答案說要重新編譯PHP,添加configure參數(shù),增加openssl的支持。這里講一個(gè)不需要重新編譯的方法。

如果服務(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)錯(cuò)誤 無法找到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

# 安裝完成后,會(huì)返回一個(gè).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郵件了。

再為大家分享一個(gè)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>

關(guān)于“PHPmailer群發(fā)Gmail的常見問題有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI