萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHPMailer使用Gmail來發送郵件的連接smtp服務器錯誤

PHPMailer使用Gmail來發送郵件的連接smtp服務器錯誤

   使用的PHPMailer版本:5.2.1

  以下是PHPMailer的example文件夾裡給出的:test_gamil_basic.php的部分代碼。

 代碼如下     $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 = "[email protected]"; // GMAIL username
    $mail->Password = "yourpassword"; // GMAIL password
    $mail->SetFrom('[email protected]', 'First Last');
    $mail->AddReplyTo("[email protected]","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 = "[email protected]";
    $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!";
    }

  按照這個例子給出的代碼操作,我遇到了以下錯誤:

  www.111cn.net提示您 smtp error could not connect to smtp host !

  的錯誤提示,google了下,發現是需要開啟PHP的openssl擴展:

  extension=php_openssl.dll //去掉最前面的分號,重啟apache或nginx服務器。

  HoHo~成功發送。

copyright © 萬盛學電腦網 all rights reserved