问题描述
我正在尝试制作联系表格,并且正在使用 PHPMailer.我在 localhost 上用 xampp 试过了,效果很好.但是当我上传到我的主机时,我收到错误 SMTP connect() failed.
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.
这是我的代码:
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = "smtp.gmail.com";
$m->Username = "mymail@gmail.com";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";
$m->isHTML();
$m->Subject = "Hello world";
$m->Body = "Some content";
$m->FromName = "Contact";
$m->addAddress('mymail@gmail.com', 'Test');
我尝试将端口更改为 587,将 SMTPsecure 更改为 tls(以及所有组合).但不起作用.有什么建议可以解决这个问题?
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?
谢谢
推荐答案
您可能需要指定发送消息的地址,如下所示:
You may need to specify the address from which the message is going to be sent, like this:
$mail->From = 'user@domain.com';
我也会给 isHTML 一个参数,无论是真还是假:
I would also give isHTML a parameter, either true or 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 = 'user@domain.com';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('cc@example.com');*/
/*$mail->addBCC('bcc@example.com');*/
$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");
}
希望这会有所帮助!
这篇关于PHPMailer,SMTP 连接()失败错误与 Gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!