PHPMailer,SMTP connect()与Gmail失败 [英] PHPMailer, SMTP connect() failed error with Gmail

查看:160
本文介绍了PHPMailer,SMTP connect()与Gmail失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个联系表单,我正在使用PHPMailer。我尝试在本地主机与xampp,它的工作完美。但是,当我上传到我的主机时,我收到错误SMTP connect()失败。



这是我的代码:

  $ 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(areymail@gmail.com','Test');

我尝试将端口更改为587,将SMTPsecure更改为tls(以及所有组合) 。但不行。任何建议来解决这个问题?



谢谢

解决方案

指定要发送邮件的地址,如下所示:

  $ mail-> From ='user @ domain.com; 

我还会给isHTML一个参数,无论是true还是false:

  $ m-> isHTML(true); 

另一个选项是尝试将端口规范放在一起。您可能会发现有其他几个参数有用。以下示例是我测试的代码,看看是否可以适应您的用途:

  $ mail = new PHPMailer; 
$ mail-> isSMTP(); / *设置邮件程序使用SMTP * /
$ mail-> Host ='mail.domain.com'; / *指定主服务器和备份SMTP服务器* /
$ mail-> Port = 587;
$ mail-> SMTPAuth = true; / *启用SMTP验证* /
$ mail->用户名= $ username; / * SMTP用户名* /
$ mail->密码= $ password; / * SMTP密码* /
/ * $ mail-> SMTPSecure ='tls'; * // *启用加密,'ssl'也被接受* /
$ mail-> From ='user@domain.com';
$ mail-> FromName = $ name;
$ mail-> addAddress($ to,'Recipients Name'); / *添加收件人* /
$ mail-> addReplyTo($ email,$ name);
/*$mail->addCC('cc@example.com');*/
/*$mail->addBCC('bcc@example.com');*/
$ mail-> WordWrap = 70; / * DEFAULT =将字换行设置为50个字符* /
$ mail-> addAttachment('../ tmp /'。$ varfile,$ varfile) *添加附件* /
/*$mail->addAttachment('/tmp/image.jpg','new.jpg'); * /
/ * $ mail-> addAttachment(' /tmp/image.jpg','new.jpg'); * /
$ mail-> isHTML(false); / *将电子邮件格式设置为HTML(default = true)* /
$ mail-> Subject = $ subject;
$ mail-> Body = $ message;
$ mail-> AltBody = $ message;
if(!$ mail-> send()){
echo'无法发送消息。
echo'Mailer Error:'。 $ MAIL-> ERRORINFO;
} else {
header(Location:../docs/confirmSubmit.html);
}

希望这有帮助!


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.

Here is my code:

$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');

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?

Thanks

解决方案

You may need to specify the address from which the message is going to be sent, like this:

$mail->From = 'user@domain.com';

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");
}

Hope this helps!

这篇关于PHPMailer,SMTP connect()与Gmail失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆