即使成功发送邮件,PHPMailer也会返回False

时间:2011-11-25 05:37:59

标签: php phpmailer

我创建了一个PHP脚本,用于将电子邮件从我的网站发送到我的支持电子邮件地址。该脚本使用的是PHPMailer,但是当发送电子邮件时它似乎有一个错误,因为它无法发送电子邮件但发送它没有问题,这是我的PHP代码...

<?php

    header('application/json');
    require_once('phpmailer.php');

    // Retreive all the required fields
    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $subject = $_REQUEST['subject'];
    $message = $_REQUEST['message'];
    $timestamp = date("dmYhms", mktime(date(h), date(m), date(s), date(m), date(d), date(y)));

    // Get users ip address
    if ($_SERVER['HTTP_X_FORWARD_FOR'] != '') {
        $ipaddress = $_SERVER['HTTP_X_FORWARD_FOR'];
    } else {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    }

    // Check a name was specified
    if ($name == ''){
        $output = json_encode(array('status'=>'invalid','message'=>'Please enter your full name.'));
        die($output);
    }

    // Check a email address was specified
    if ($email == ''){
        $output = json_encode(array('status'=>'invalid','message'=>'Please enter your email address.'));
        die($output);
    }

    // Check a subject was specified
    if ($subject == ''){
        $output = json_encode(array('status'=>'invalid','message'=>'Please enter a subject for your message.'));
        die($output);
    }

    // Check a message was specified
    if ($message == ''){
        $output = json_encode(array('status'=>'invalid','message'=>'A message without a message, interesting...'));
        die($output);
    }

    // Double check all the fields are not blank and prepare the message
    if ($name != '' && $email != '' && $subject != '' && $message != ''){

        // Check the email address specified is valid
        if (preg_match("/^(\w+((-\w+)|(\w.\w+))*)\@(\w+((\.|-)\w+)*\.\w+$)/", $email)){

            $mail = new phpmailer;
            $mail->From = $email;
            $mail->FromName = $name;
            $mail->AddAddress("support@idify.me", "IDify Ltd");
            $mail->AddReplyTo("support@idify.me", "IDify Ltd");
            $mail->WordWrap = 500;
            $mail->IsHTML(true);
            $mail->Subject = $subject;
            $mail->Body = "<strong>Ip Address:&nbsp;</strong>".$ipaddress."<br/><strong>Timestamp:&nbsp;</strong>".$timestamp."<br/><strong>Fullname:&nbsp;</strong>".$name."<br/><strong>Email Address:&nbsp;</strong>".$email."<br/><strong>Subject:&nbsp;</strong>".$subject."<br/><p>".$message."</p>";

            if($mail->Send()){
                $output = json_encode(array('status'=>'valid','message'=>'Your message is on its way, we will be in touch with you soon.'));
                die($output);
            } else {
                $output = json_encode(array('status'=>'invalid','message'=>'We were unable to send your message, please try again later.'));
                die($output);
            }

        }else{

            $output = json_encode(array('status'=>'invalid','message'=>'Please enter a valid email address.'));
            die($output);

        }

    }

?>

感谢您提供任何帮助,非常奇怪,但我相信我们可以解决它。

1 个答案:

答案 0 :(得分:2)

在我解决这个问题之前,请注意代码,我不打算用正则表达式来验证电子邮件。虽然有一个RFC标准,因此许多邮件服务器有不同的实现,我发现只是尝试发送电子邮件比弹出可能有效的电子邮件地址更好。

就返回False而言,你的意思是$mail->Send()返回False吗? 我看了PHPMailer source code

545     public function Send() {
SNIP    SNIP
581       } catch (phpmailerException $e) {
582       $this->SetError($e->getMessage());
583       if ($this->exceptions) {
584       throw $e;
585       }
586       echo $e->getMessage()."\n";
587       return false;
588       }
589     }

如果send函数在第572行的switch语句中抛出异常,那么你可能会遇到邮件被发送的情况但是你点击catch块并返回False。

如果是这种情况,那么您的配置可能存在问题,您是否愿意尝试项目中的example script并报告结果?