【问题标题】:PHPMailer Cannot send emails on live serverPHPMailer 无法在实时服务器上发送电子邮件
【发布时间】:2020-05-09 07:16:44
【问题描述】:

我相信以前有人问过这个问题,但我似乎找不到解决问题的方法。

我正在使用 PHPMailer 来处理网站上的电子邮件。在本地主机上一切正常,但在实时服务器上不起作用。这是我的代码:

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->IsSMTP();
$mail->Host='smtp.gmail.com';
$mail->Port=587;
$mail->SMTPAuth=true;
$mail->SMPTSecure='tls';
$mail->SMTPDebug = 3;
$mail->Username='*********';
$mail->Password='*********';

$mail->setFrom('*********', '********* Booking', 0);
$mail->addAddress('*********');

$name = "First Name :" . $_POST['firstname'] . "<br>";
$email = "Email :" . $_POST['email'] . "<br>";
$number = "Contact Number :" . $_POST['number'] . "<br>";
$message = "message :" . $_POST['message'] . "<br>";
$truck = "Truck Selected :" . $_POST['item'] . "<br>";


$mail->isHTML(true);
$mail->Subject='********* Booking';
$mail->Body= "$name $email $number $message $truck";


if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  header('Location: /index.php');
  echo 'Message was sent.';
  exit;
}

使用上述代码在实时服务器上发送电子邮件会产生以下结果:

2020-01-22 07:51:48 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
2020-01-22 07:51:48 Connection failed. Error #2: stream_socket_client() [<a href='function.stream-socket-client'>function.stream-socket-client</a>]: unable to connect to smtp.gmail.com:587 (Connection refused) [/websites/cv/*********/phpmailer/class.smtp.php line 299]
2020-01-22 07:51:48 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

但是,如果我禁用某些人建议的 $mail-&gt;IsSMTP();,则不会显示错误,但不会发送/接收电子邮件。

我尝试过的事情:

  • 使用 ssl,端口 465
  • 在发送电子邮件时打开了不太安全的应用
  • 联系我的主机询问是否有任何端口被阻止,他们 说他们不是
  • 确保 openssl 处于活动状态
  • 确保登录 凭据正确

做了所有这些事情后,它仍然说使用 SMTP 时连接被拒绝。有谁知道是什么原因造成的?

【问题讨论】:

    标签: php email smtp


    【解决方案1】:

    这听起来像是网络/系统管理员问题,而不是 PHPMailer 问题。试试

    telnet smtp.gmail.com 587  // host and port
    

    在您的控制台中,用于检查连通性。如果一切正常,它应该写一条消息,如“ESMTP MAIL Service ready”。否则该服务器会阻止您的。

    【讨论】:

    • 我在检查端口Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:587 (Connection refused) 时在服务器上收到此消息,本地主机给了我一个成功的连接。我猜是主人
    【解决方案2】:

    请在您的文件中添加类 smtp 或使用此代码并相应地更改凭据,它将起作用。

    include_once('class.phpmailer.php');
    
     require_once('class.smtp.php');
    
    $name = strip_tags($_POST['full_name']);
    $email = strip_tags ($_POST['email']);
    $msg = strip_tags ($_POST['description']);
    
    $subject = "Contact Form from DigitDevs Website";
    
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';
    
    $mail->Host       = "mail.example.com"; // SMTP server example
    //$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "info@example.com"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example
    
    $mail->From = $email;
    $mail->FromName = $name;
    
    $mail->AddAddress('info@example.com', 'Information'); 
    $mail->AddReplyTo($email, 'Wale');
    
    $mail->IsHTML(true);
    
    $mail->Subject = $subject;
    
    $mail->Body    =  $msg;
    $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;
    exit;
    }
     echo 'Message has been sent';
    

    【讨论】:

    • 端口 26 似乎提供了持续的加载时间,并且根本不提交表单。我添加了class.smtp.php并再次尝试使用端口587,但出现相同的连接被拒绝错误。
    • 这是您尝试连接时出现的错误“无法连接到服务器连接被拒绝”
    • 是的,您的解决方案会发生同样的错误,即在我的原始帖子中发布的错误。我将在另一台服务器上尝试相同的脚本,看看它是否是导致阻塞的主机
    • 这听起来像是网络/系统管理员问题,而不是 PHPMailer 问题。在您的控制台中尝试“telnet 主机名端口”,以检查连接性。如果一切正常,它应该写一条消息,如“ESMTP MAIL Service ready”。否则该服务器会阻止您的。
    猜你喜欢
    • 2019-01-19
    • 2023-03-20
    • 2014-11-27
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2020-11-25
    • 1970-01-01
    相关资源
    最近更新 更多