PHP编程:汇总PHPmailer群发Gmail的常见问题

开发技术 作者: 2024-08-19 15:05:01
介绍《PHP编程:汇总PHPmailer群发Gmail的常见问题》开发教程,希望对您有用。

《汇总PHPmailer群发Gmail的常见问题》要点:
本文介绍了汇总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实例以上就是本文的全部内容,希望对大家的学习有所赞助.

欢迎参与《汇总PHPmailer群发Gmail的常见问题》讨论,分享您的想法,编程之家 jb51.cc为您提供专业教程。

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65000.html