邮件程序错误:php 邮件程序中的 SMTP 连接()失败(https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30542095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mailer Error: SMTP connect() failed in php mailer( https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)
提问by pcs
Here is code for sending email from localhost after i referred a lot from online.
这是我从网上参考了很多之后从本地主机发送电子邮件的代码。
html form:
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:
<?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 = "[email protected]"; // SMTP username
$mail->Password = "zzz"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("xxx", "xx");
$mail->SetFrom('[email protected]','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";
?>
so when i run my code it shows error like this,
所以当我运行我的代码时,它会显示这样的错误,
Message could not be sent.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
无法发送消息。
邮件程序错误:SMTP connect() 失败。https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
And i removed semicolon in this line ;extension=php_openssl.dll
from the following files, and restart the xampp.
我;extension=php_openssl.dll
从以下文件中删除了这一行中的分号,然后重新启动 xampp。
c/xampp/apache/bin/php.ini and c/xampp/php/php.ini
still stays same error..
仍然保持相同的错误..
Note:I m new to php, but i want to know particular this one and fix the problem. and I referred similar questions in stack, but it didn't help me,
注意:我是 php 新手,但我想特别了解这个并解决问题。我在堆栈中提到了类似的问题,但这对我没有帮助,
Can anybody help me to fix this?
有人可以帮我解决这个问题吗?
Thanks,
谢谢,
采纳答案by Matthieu Boisjoli
It looks like your credentials for connecting to your authentication has failed. I often send mail from my local and I found it that it's a LOT easier to use another SMTP than gmail, like mandrillapp, free until 12,000 mails. There are a lot of things that I don't understand in your code so I will share mine.
您的身份验证凭据似乎已失败。我经常从我的本地发送邮件,我发现使用另一个 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 = '[email protected]'; // SMTP username
$mail->Password = 'mandrilapp_will_give_you_a_password'; // SMTP password
$mail->Port = 587; // TCP port to connect to
$mail->From = '[email protected]';
$mail->FromName = 'Test phpmailer';
$mail->addAddress('[email protected]'); // 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';
}
Be sure to have PHPMailer-master folder (you can download it from here) at the same level as your php file. This is how I link phpmailer. Hope it helps, if you have any questions, ask me!
确保 PHPMailer-master 文件夹(您可以从这里下载)与您的 php 文件处于同一级别。这就是我链接phpmailer的方式。希望能帮到你,有什么问题可以问我!