Mailer Error: SMTP connect() failed in php mailer( https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)

Mailer Error: SMTP connect() failed in php mailer( https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)

这是我在网上参考了很多之后从本地主机发送电子邮件的代码。

html形式:

<form method="post" action="email.php">
  Email: <input name="email" id="email" type="text" /><br />

  Message:<br />
  <textarea name="message" id="message" rows="15" cols="40"></textarea><br />

  <input type="submit" value="Submit" />
</form>

email.php:

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require_once('class.phpmailer.php');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';

$mail = new PHPMailer();
$body='hellooooo';
$mail->IsSMTP();

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server

$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = "xxxx@gmail.com"; // SMTP username
$mail->Password = "zzz"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("xxx", "xx");
$mail->SetFrom('xxx@gmail.com','xxxx');
$mail->WordWrap = 50;

$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

$mail->MsgHTML($body);

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "Message has been sent";
?>

所以当我 运行 我的代码显示这样的错误时,

Message could not be sent.

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

并且我从以下文件中删除了这一行 ;extension=php_openssl.dll 中的分号,然后重新启动 xampp.

c/xampp/apache/bin/php.ini and c/xampp/php/php.ini

仍然是同样的错误..

注意:我是 php 的新手,但我想了解这个问题并解决问题。我在堆栈中提到了类似的问题,但它对我没有帮助,

谁能帮我解决这个问题?

谢谢,

您用于连接身份验证的凭据似乎失败了。我经常从本地发送邮件,我发现使用其他 SMTP 比 gmail 要容易得多,例如 mandrillapp,直到 12,000 封邮件才免费。您的代码中有很多我不理解的地方,所以我将分享我的代码。

<?php 

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your@username.com';                 // SMTP username
$mail->Password = 'mandrilapp_will_give_you_a_password';                           // SMTP password
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'your@email.com';
$mail->FromName = 'Test phpmailer';
$mail->addAddress('who_are_you_sending@to.com');               // Name is optional

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

确保 PHPMailer-master 文件夹(您可以从 here 下载)与 php 文件处于同一级别。这就是我 link php 邮件程序的方式。希望对你有帮助,有什么问题可以问我!