php PHPMailer,SMTP 连接()Gmail 失败错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25924651/
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
PHPMailer, SMTP connect() failed error with Gmail
提问by Devilquest
I'm trying to make a contact form and I'm using PHPMailer. I tried that on localhost with xampp and it works perfect. But when i upload to my host i get the error SMTP connect() failed.
我正在尝试制作联系表格,并且正在使用 PHPMailer。我用 xampp 在 localhost 上尝试过,它工作得很好。但是当我上传到我的主机时,我收到错误 SMTP connect() failed。
Here is my code:
这是我的代码:
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = "smtp.gmail.com";
$m->Username = "[email protected]";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";
$m->isHTML();
$m->Subject = "Hello world";
$m->Body = "Some content";
$m->FromName = "Contact";
$m->addAddress('[email protected]', 'Test');
I've tried to change the port to 587 and the SMTPsecure to tls (and all the combinations). But doesn't work. Any advice to solve this?
我尝试将端口更改为 587,将 SMTPsecure 更改为 tls(以及所有组合)。但不起作用。有什么建议可以解决这个问题吗?
Thanks
谢谢
采纳答案by CH3M
You may need to specify the address from which the message is going to be sent, like this:
您可能需要指定要发送消息的地址,如下所示:
$mail->From = '[email protected]';
I would also give isHTML a parameter, either true or false:
我还会给 isHTML 一个参数,true 或 false:
$m->isHTML(true);
Another option is trying to drop the port specification all together. There are several other parameters that you may find useful. The following example is code I've tested, see if you can adapt it for your uses:
另一种选择是尝试一起删除端口规范。您可能会发现其他几个参数很有用。下面的例子是我测试过的代码,看看你是否可以适应你的用途:
$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'mail.domain.com';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $username;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
/*$mail->SMTPSecure = 'tls';*//*Enable encryption, 'ssl' also accepted*/
$mail->From = '[email protected]';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('[email protected]');*/
/*$mail->addBCC('[email protected]');*/
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(false);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header("Location: ../docs/confirmSubmit.html");
}
Hope this helps!
希望这可以帮助!
回答by Jhonattan
This answer work form me: https://stackoverflow.com/a/47205296/2171764
这个答案对我有用:https: //stackoverflow.com/a/47205296/2171764
I use:
我用:
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);