SMTP Connect()失败.邮件未发送.Mailer错误:SMTP Connect()失败


SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed

我试图将邮件发送到gmail地址,但它一直收到此错误"SMTP->错误:无法连接到服务器:连接超时(110)SMTP connect()失败。邮件未发送。Mailer错误:SMTP connect(?

        require 'class.phpmailer.php'; // path to the PHPMailer class
        require 'class.smtp.php';
            $mail = new PHPMailer();

            $mail->IsSMTP();  // telling the class to use SMTP
            $mail->SMTPDebug = 2;
            $mail->Mailer = "smtp";
            $mail->Host = "ssl://smtp.gmail.com";
            $mail->Port = 587;
            $mail->SMTPAuth = true; // turn on SMTP authentication
            $mail->Username = "myemail@gmail.com"; // SMTP username
            $mail->Password = "mypasswword"; // SMTP password 
            $Mail->Priority = 1;
            $mail->AddAddress("myemail@gmail.com","Name");
            $mail->SetFrom($visitor_email, $name);
            $mail->AddReplyTo($visitor_email,$name);
            $mail->Subject  = "Message from  Contact form";
            $mail->Body     = $user_message;
            $mail->WordWrap = 50;  
            if(!$mail->Send()) {
            echo 'Message was not sent.';
            echo 'Mailer error: ' . $mail->ErrorInfo;
            } else {
            echo 'Message has been sent.';
            }

删除或注释行-

$mail->IsSMTP();

它会为你工作。

我已经从不同的网站检查并试验了许多答案,但除了上面的解决方案之外,没有任何解决方案。

您必须已经安装了php_openssl.dll,如果您使用wampserver,则很容易搜索并应用php扩展。

在示例中更改此:

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl
    $mail->Port = 465;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

然后你收到了来自gmail的一封电子邮件,谈到要启用"不太安全的访问应用程序"选项在这里https://www.google.com/settings/security/lesssecureapps

我建议您更改密码并不断加密

您没有SMTPSecure设置来定义所使用的身份验证类型,并且您正在使用不必要的"ssl://"运行主机设置(PS-ssl通过端口465,如果您需要通过ssl运行它,请在此处查看接受的答案。以下是要添加/更改的行:

+ $mail->SMTPSecure = 'tls';
- $mail->Host = "ssl://smtp.gmail.com";
+ $mail->Host = "smtp.gmail.com";

您在Localhost上运行吗?你编辑php.ini了吗?

如果还没有,请尝试以下操作:
1.打开xamp->php->php.ini
2.搜索extension=php_openssl.dll
3.首字母看起来像这个;extension=php_openssl.dll
4.删除";"它将看起来像这个extension=php_openssl.dll
5.如果找不到extension=php_openssl.dll,请添加此行extension=php_openssl.dll
6.然后重新启动Xampp。

好运;)

我知道这个问题已经有一段时间了,但我发现了确切的问题,并通过在csf.conf上禁用SMTP_BLOCK来解决它(我们使用csf作为防火墙)。

要禁用,只需编辑csf.conf并禁用SMTP_BLOCK,如下所示:

###############################################################################
# SECTION:SMTP Settings
###############################################################################
# Block outgoing SMTP except for root, exim and mailman (forces scripts/users
# to use the exim/sendmail binary instead of sockets access). This replaces the
# protection as WHM > Tweak Settings > SMTP Tweaks
#
# This option uses the iptables ipt_owner/xt_owner module and must be loaded
# for it to work. It may not be available on some VPS platforms
#
# Note: Run /etc/csf/csftest.pl to check whether this option will function on
# this server
# SMTP_BLOCK = "1" --> this will cause phpmailer Connection timed out (110)
SMTP_BLOCK = "0"

我在告诉我收到谷歌的一封电子邮件时遇到了这个问题,告诉我有人试图登录你的帐户,是你吗?我回答是的,然后它开始工作,所以如果你是这样的话,请查看你的电子邮件并允许服务器

在myaccount.google.com/security登录你的谷歌帐户,转到"登录",然后转到"安全",滚动到底部,然后启用"允许不太安全的应用程序"选项。

以下是处理PHPMailer时应该查看的列表:

  1. 通过在PHP.ini中取消对extension=php_openssl.dll的注释来启用openSSL
  2. 使用$mail->SMTPSecure = 'tls';$mail->Port = 587;
  3. 如果你在其他地方出错,比如用户名和密码不正确等,请启用调试

你做得很好。你只需要检查不同的SMTP端口,如465和其他在你的系统上工作的端口。另一件需要记住的事情是,允许谷歌帐户访问不太安全的应用程序,否则会出现同样的错误
我已经经历了一整天,我唯一做错的就是端口号,我只是更改了端口号,它起作用了。

Mailjet

SMTP设置

端口:25或587(一些提供商阻止端口25)

我的工作方式是在将应用程序部署到服务器后更改端口。

  • 在调试中,它对我有效:$mail->Port = 25;
  • 在Release中,它对我有效:$mail->Port = 587;

GL

为了让它正常工作,我必须转到myaccount.google.com->"连接的应用程序和站点",并将"允许不太安全的应用程序"转到"打开"(靠近页面底部)。

如果它在本地主机上工作,但在web主机上不工作:

某些托管站点会阻止某些出站SMTP端口。在接受的答案中注释$mail->IsSMTP();行可能会使其工作,但这只是禁用SMTP配置,并使用托管站点的电子邮件配置。

如果您使用GoDaddy,则无法使用其他SMTP发送邮件。我在使用SiteGround,发现他们只允许从端口25和465进行SMTP访问,并且使用SSL加密类型,所以我会查找您主机的文档,然后从那里开始。

        <?php
    require 'PHPMailer/PHPMailerAutoload.php';
    
    $mail = new PHPMailer();
    
    $mail->SMTPDebug = 0;                               // Enable verbose debug output
    
    
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->IsSMTP();
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'mail@gmail.com';                 // SMTP username
    $mail->Password = 'your pass';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    $mail->setFrom('mail@gmail.com');
    $mail->addAddress('mail@gmail.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';
    }
    
  ?>

当你想测试的时候,这对我有用不要使用IDE进行测试,因为如果您在LOCALHOST中使用LOCALHOST/yourfile.php

解决方案是配置gmail首选项,访问不安全的应用程序

最近谷歌推出了一款名为"应用程序密码"的软件。通过为我的mailer实例创建应用程序密码,为我解决了问题。

https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor&visit_id=1-636228492770322608-2743677043&rd=1