Phpmailer using Gmail to send a mail connection SMTP server error

Source: Internet
Author: User
Tags nginx server

Phpmailer version used: 5.2.1

The following are some of the code given in the example folder of Phpmailer: test_gamil_basic.php.

The code is as follows
$mail = new Phpmailer ();

$body = file_get_contents (' contents.html '); $body = $_post[' body '];

$body = Eregi_replace ("[]", ", $body);

$mail->issmtp (); Telling the class to use SMTP

$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 = ""; Sets GMAIL as the SMTP server or ssl://

$mail->port = 465; Set the SMTP port for the GMAIL server

$mail->username = ""; GMAIL username

$mail->password = "YourPassword"; GMAIL Password

$mail->setfrom (' ', ' the ' last ');

$mail->addreplyto ("", "the Last");

$mail->subject = "Phpmailer Test Subject via SMTP (Gmail), basic";

$mail->altbody = "To view", please use an HTML compatible email viewer! "; Optional, comment out and test

$mail->msghtml ($body);

$address = "";

$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!";


According to the code given in this example, I encountered the following error: prompts you for SMTP error could not connect to SMTP host!

The error prompted Google to find that it is necessary to open the PHP OpenSSL extension:

Extension=php_openssl.dll//Remove the front semicolon and restart the Apache or Nginx server.

Hoho~ successfully sent.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.