Home > Backend Development > PHP Tutorial > Summary of common problems with PHPmailer mass sending to Gmail, phpmailer mass sending to Gmail_PHP tutorial

Summary of common problems with PHPmailer mass sending to Gmail, phpmailer mass sending to Gmail_PHP tutorial

WBOY
Release: 2016-07-12 08:58:12
Original
1032 people have browsed it

Summarize the common problems of mass sending Gmail with PHPmailer, mass sending gmail with phpmailer

Everyone will encounter many common problems when sending mass Gmail with PHPmailer. Here is a summary of some common problems for you. I hope it will help Everyone’s learning helps.

1.Could not authenticate

First of all, if you don’t use loops, it’s basically because your account or password is wrong;

If you use a loop to send mass messages, remember to call Smtpclose() after the send() method is completed, and send and close once. Otherwise, you will only be able to send one email and it will crash the second time.

2.Gmail

First, enable php’s SSL permissions

How to enable openssl in php. In most cases, openssl is not enabled. To enable it, you need to make the following simple settings:

How to open it under windows:

1: First check whether extension=php_openssl.dll exists in php.ini. If it exists, remove the previous comment character ';'. If this line does not exist, add extension=php_openssl.dll.

2: Copy php_openssl.dll, ssleay32.dll, libeay32.dll 3 files in the php folder to the WINDOWSsystem32 folder.

3: Restart apache or iis

At this point, the openssl function is enabled.

How to enable it under Linux:

I am using the cloud host of Jinshang Data, PHP version: 5.2.14

The following plan uses my host as an example to explain adding openssl module support to PHP.

Some answers on the Internet say that you need to recompile PHP, add configure parameters, and add openssl support. Here is a method that does not require recompiling.

It is best if the PHP installation package file exists on the server. If it has been deleted, download the PHP installation file with the same version as shown on the phpinfo page. Here is php-5.2.14.tar.gz

It is recommended to download the Sohu mirror, but the NetEase mirror was not found. The address is: http://mirrors.sohu.com/php/

Connect to the host using ssh tool.

# 下载到/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

Copy after login

Okay, openssl support is now successfully added.

However, Gmail’s troubles don’t end there. Gmail’s current SMTP and POP3 are both SSL encrypted

Step1.php openssl module(extension) support
Step2. download phpmailer library
Step3. change code 'class.phpmailer.php' and 'class.smtp.php'

1.phpmailer and smtp Riga property Is_SSL

public $Is_SSL = false;
Copy after login

2. Pass to the smtp object in the SmtpConnect method in phpmailer

$this->smtp-> Is_SSL = $this-> Is_SSL ;
Copy after login

3. The Connect method in smtp adds

before the fsockopen call.
if($this->is_ssl){ $host = 'ssl://'.$host; }
Copy after login

The last step is the usage method. Remember to call the phpmailer class, which is not in the code.

$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);
Copy after login

That’s it for the code part. Don’t forget to make the corresponding settings in gmail.

After completing the above three steps, you can freely use phpmailer to send gmail emails.

Let me share with you an example of sending gmail emails using phpmailer:

<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<&#63;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!";
}
&#63;>
</body>
</html> 

Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Articles you may be interested in:

  • PHP to obtain 163, gmail, 126 and other email contact addresses [tested 2009.10.10]
  • php simulates GMAIL, HOTMAIL ( MSN), YAHOO, 163, 126 mailbox login details
  • Source code for PHP to implement mass mailing
  • Detailed explanation of examples of phpmailer sending gmail emails
  • java, php, C#, asp to implement SMS Methods of group sending function
  • php realizes unlimited group sending of WeChat public accounts
  • PHP swoole realizes simple multi-person online chat group sending
  • PHP mail mass sending machine implementation code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1104341.htmlTechArticle Summarizes the common problems of PHPmailer mass sending Gmail, phpmailer mass sending gmail Everyone will encounter many common problems when PHPmailer mass sends Gmail, Here is a summary of some common questions for you, I hope it will be useful to you...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template