Everyone will encounter many common problems when sending group emails to Gmail with PHPmailer. Here is a summary of some common problems for you. I hope it will be helpful to your study.
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
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;
2. Pass to the smtp object in the SmtpConnect method in phpmailer
$this->smtp-> Is_SSL = $this-> Is_SSL ;
3. The Connect method in smtp adds
before the fsockopen call.if($this->is_ssl){ $host = 'ssl://'.$host; }
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);
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.
I will share with you an example of sending gmail emails using phpmailer:
<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>
The above is the entire content of this article, I hope it will be helpful to everyone’s study.