PHPMailer used Gmail to send mail connection to smtp server error

Source: Internet
Author: User
Tags gmail password
The error message "smtperrorcouldnotconnecttosmtphost" is returned when PHPMailer is used to send emails to the smtp server! This is because ex

When PHPMailer is used to send emails to the smtp server, the following error occurs: smtp error cocould not connect to smtp host! This is because extension = php_openssl.dll is not enabled.

PHPMailer version: 5.2.1. The following is the code of test_gamil_basic.php in the example folder of PHPMailer. the code is as follows:

  1. $ Mail = new PHPMailer ();
  2. $ Body = file_get_contents('contents.html '); // $ body = $ _ POST ['body'];
  3. $ Body = eregi_replace ("[]", '', $ body );
  4. $ Mail-> IsSMTP (); // telling the class to use SMTP
  5. $ Mail-> SMTPDebug = 2; // enables SMTP debug information (for testing)
  6. // 1 = errors and messages
  7. // 2 = messages only
  8. $ Mail-> SMTPAuth = true; // enable SMTP authentication
  9. $ Mail-> SMTPSecure = "ssl"; // sets the prefix to the servier
  10. $ Mail-> Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server or ssl: // smtp.gmail.com
  11. $ Mail-> Port = 465; // set the SMTP port for the GMAIL server
  12. $ Mail-> Username = "yourusername@gmail.com"; // GMAIL username
  13. $ Mail-> Password = "yourpassword"; // GMAIL password
  14. $ Mail-> SetFrom ('name @ yourdomain.com ', 'First LA ');
  15. $ Mail-> AddReplyTo ("name@yourdomain.com", "First Last ");
  16. $ Mail-> Subject = "PHPMailer Test Subject via smtp (Gmail), basic ";
  17. $ Mail-> AltBody = "To view the message, please use an HTML compatible email viewer! "; // Optional, comment out and test
  18. $ Mail-> MsgHTML ($ body );
  19. $ Address = "whoto@otherdomain.com ";
  20. $ Mail-> AddAddress ($ address, "John Doe ");
  21. $ Mail-> AddAttachment ("images/phpmailer.gif"); // attachment
  22. $ Mail-> AddAttachment ("images/phpmailer_mini.gif"); // attachment
  23. If (! $ Mail-> Send ()){
  24. Echo "Mailer Error:". $ mail-> ErrorInfo;
  25. } Else {
  26. Echo "Message sent! ";
  27. }

According to the code shown in this example, I encountered the following error:

Smtp error cocould not connect to smtp host! Error message: google, found that the openssl extension of PHP needs to be enabled:

Extension = php_openssl.dll // remove the front semicolon and restart the apache or nginx server.

HoHo ~ Sent successfully.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.