发送邮件时,php connect()在php中失败错误

时间:2014-04-27 18:13:02

标签: php email smtp gmail

我正在尝试在提交表单时向用户发送邮件。我在我的本地主机和我的服务器上有相同的代码,它们都在ubuntu上运行。在我的本地主机上我得到SMTP连接()失败,而它在我的服务器上工作正常,表明我的本地计算机上的php / smtp设置配置错误但我无法弄清楚是什么。以下是我在本地计算机上收到的错误。

2014-04-27 18:04:40 CLIENT -> SERVER: EHLO localhost 2014-04-27 18:04:41    
SMTP ERROR: EHLO command failed: 220 mx.google.com ESMTP yv5sm29656409pbb.49 - gsmtp 2014-04-27 18:04:41    CLIENT -> SERVER: HELO localhost 2014-04-27 18:04:41    
SMTP ERROR: HELO command failed: 552 Command sent before response recieved. 2014-04-27 18:04:41 CLIENT -> SERVER: STARTTLS 2014-04-27 18:04:41  
SMTP ERROR: STARTTLS command failed: 2014-04-27 18:04:41    
SMTP NOTICE: EOF caught while checking if connected 
SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.

以下是我的HTML代码

<html>

<body>

<section class="stats" id="signup">
<div class="container" >


    <form action="index.php" method="post"  class="subscription wow fadeInLeft animated" data-wow-offset="30" data-wow-duration="1.5s" data-wow-delay="0.15s" role="form">
        <input type="email" placeholder="Enter email" class="form-control input-box" name="email_address">
        <button class="btn btn-primary custom-button red-btn">Sign Up</button>
    </form>


</div> 
</section> 



<?php



if(!isset($_POST['email_address']) || trim($_POST['email_address']) == '')
{

} else {

require '/usr/share/php5/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->SMTPDebug = 1;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'team@compamy.com';                            // SMTP username
$mail->Password = '<password>';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'team@compamy.com';
$mail->FromName = 'Team Scaleqa';
$mail->addAddress($_POST['email_address'], '');  // Add a recipient
$mail->addAddress('team@compamy.com');               // Name is optional
$mail->addReplyTo('team@compamy.com', 'Team Scaleqa');
$mail->addCC('team@compamy.com');
$mail->addBCC('team@compamy.com');



$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Welcome to Company';
$pos = strpos( $_POST['email_address'], "@");

$username = substr($_POST['email_address'], 0, $pos);
$mail->Body = "
    Hi 
    ";
//echo $mail->Body;
//$mail->Body    = clone $message;
$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';
}

?>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

GMail拥有强大的垃圾邮件过滤器。其中一个检查发件人是否是有效的MTA。在您的EHLO问候语中,您使用&#34; localhost&#34;,这不是普通服务器的功能。即使此脚本从服务器运行,GMail也会运行反向DNS查找,以确保您的域对应于您服务器的IP,否则将其归类为垃圾邮件。