《PHP編程:匯總PHPmailer群發Gmail的常見問題》要點:
本文介紹了PHP編程:匯總PHPmailer群發Gmail的常見問題,希望對您有用。如果有疑問,可以聯系我們。
PHP實例大家在PHPmailer群發Gmail時會遇到許多常見問題,下面為大家總結了一些常見問題,希望對大家的學習有所贊助.
PHP實例1.Could not authenticate
PHP實例首先,如果你沒有使用循環的話,基本上就是賬號或者暗碼錯了;
PHP實例如果使用循環來群發,send()方法結束之跋文得調用Smtpclose(),發一次關一次,否則就會出現只能發一封郵件,第二次就崩潰的情況.
PHP實例2.Gmail
PHP實例首先,開啟php的ssl權限
PHP實例php開啟openssl的辦法,大多數情況下openssl是沒有開啟的,要想啟用需要進行下簡單的設置:
PHP實例windows下開啟辦法:
PHP實例1: 首先檢查php.ini中;extension=php_openssl.dll是否存在, 如果存在的話去掉前面的注釋符‘;', 如果不存在這行,那么添加extension=php_openssl.dll.
PHP實例2: 講php文件夾下的: php_openssl.dll, ssleay32.dll, libeay32.dll 3個文件拷貝到 WINDOWS\system32\? 文件夾下.
PHP實例3: 重啟apache或者iis
PHP實例至此,openssl功能就開啟了.
PHP實例Linux下開啟辦法:
PHP實例我使用的是錦尚數據的云主機,PHP版本:5.2.14
PHP實例下面方案就以我的主機為例講解為PHP添加openssl模塊支持.
PHP實例網上一些答案說要重新編譯PHP,添加configure參數,增加openssl的支持.這里講一個不需要重新編譯的辦法.
PHP實例如果服務器上存在PHP安裝包文件最好,如果已經刪除,去下載和phpinfo頁面顯示版本一樣的PHP安裝文件,我這里是 php-5.2.14.tar.gz
PHP實例推薦去搜狐鏡像下載,網易鏡像沒有找到.地址為: http://mirrors.sohu.com/php/
PHP實例用ssh工具連接到主機.
PHP實例
# 下載到/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
# 進入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-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
PHP實例好了,現在就成功添加openssl支持.?
PHP實例但是,Gmail麻煩的地方可不止這樣,Gmail現在的smtp和pop3都是ssl加密的
PHP實例Step1. php openssl module(extension) support
Step2. download phpmailer library
Step3. change code 'class.phpmailer.php' and 'class.smtp.php'
PHP實例1.phpmailer和smtp里加property Is_SSL
PHP實例
public $Is_SSL = false;
PHP實例2.phpmailer里的SmtpConnect辦法里傳遞給smtp對象
PHP實例
$this->smtp-> Is_SSL = $this-> Is_SSL ;
PHP實例3.smtp里的Connect辦法在fsockopen調用前加上
PHP實例
if($this->is_ssl){ $host = 'ssl://'.$host; }
?PHP實例最后是使用辦法,記得調用phpmailer類哦,代碼里沒有.
PHP實例
$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);
PHP實例代碼部分就這些,還有不要忘記在gmail中做好相應的設置哦.
PHP實例以上三步完成,就可以自由的用phpmailer來發送gmail郵件了.
PHP實例再為大家分享一個phpmailer發送gmail郵件實例:
PHP實例
<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>
PHP實例以上就是本文的全部內容,希望對大家的學習有所贊助.
歡迎參與《PHP編程:匯總PHPmailer群發Gmail的常見問題》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7512.html