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:
- $ 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 = "smtp.gmail.com"; // sets GMAIL as the SMTP server or ssl: // smtp.gmail.com
- $ Mail-> Port = 465; // set the SMTP port for the GMAIL server
- $ Mail-> Username = "yourusername@gmail.com"; // GMAIL username
- $ Mail-> Password = "yourpassword"; // GMAIL password
- $ Mail-> SetFrom ('name @ yourdomain.com ', 'First LA ');
- $ Mail-> AddReplyTo ("name@yourdomain.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 = "whoto@otherdomain.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! ";
- }
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.