【问题标题】:Error : "501 5.5.4 Invalid domain name" when I attempt to send an email using PHP + Swiftmailer当我尝试使用 PHP + Swiftmailer 发送电子邮件时出现错误:“501 5.5.4 Invalid domain name”
【发布时间】:2015-03-20 14:40:48
【问题描述】:

我正在编写一个非常简单的联系表格,允许蓝领工人通过我们的内部网提交安全问题通知。联系表格以 HTML 格式显示,并要求提供姓名、发件人电子邮件和要发送的消息。它总是发送到我们的安全电子邮件。

服务器和端口正确。这是一台 Exchange 2010 服务器,我们使用的是 TSL。服务器配置为能够“匿名”接收电子邮件。我可以通过 telnet 命令连接,但是当我尝试通过评论框发送邮件时收到“501 5.5.4 Invalid domain name”错误。

define("EMAIL_SUBJECT", "Safety Concerns");     
    define("EMAIL_TO", "email");        

    // SMTP Configuration
    define("SMTP_SERVER", 'server');                
    define("SMTP_PORT", 25);                                

    // define("UPLOAD_DIR", '/var/www/tmp/');           // Default php upload dir

    // main method. It's the first method called
    function main($contactForm) {

        // Checks if something was sent to the contact form, if not, do nothing
        if (!$contactForm->isDataSent()) {
            return;
        }

        // validates the contact form and initialize the errors
        $contactForm->validate();

        $errors = array();

        // If the contact form is not valid:
        if (!$contactForm->isValid()) {
            // gets the error in the array $errors
            $errors = $contactForm->getErrors();

        } else {
            // If the contact form is valid:
            try {               
                // send the email created with the contact form
                $result = sendEmail($contactForm);              

                // after the email is sent, redirect and "die".
                // We redirect to prevent refreshing the page which would resend the form
                header("Location: ./success.php");
                die();
            } catch (Exception $e) {
                // an error occured while sending the email. 
                // Log the error and add an error message to display to the user.
                error_log('An error happened while sending email contact form: ' . $e->getMessage());
                $errors['oops'] = 'Ooops! an error occured while sending your email! Please try again later!';
            }

        }

        return $errors;
    }

    // Sends the email based on the information contained in the contact form
    function sendEmail($contactForm) {
        // Email part will create the email information needed to send an email based on 
        // what was inserted inside the contact form
        $emailParts = new EmailParts($contactForm);

        // This is the part where we initialize Swiftmailer with 
        // all the info initialized by the EmailParts class
        $emailMessage = Swift_Message::newInstance()
        ->setSubject($emailParts->getSubject())
        ->setFrom($emailParts->getFrom())
        ->setTo($emailParts->getTo())
        ->setBody($emailParts->getBodyMessage());

        // If an attachment was included, add it to the email
        // if ($contactForm->hasAttachment()) {
        //  $attachmentPath = $contactForm->getAttachmentPath();
        //  $emailMessage->attach(Swift_Attachment::fromPath($attachmentPath));
        //}

        // Another Swiftmailer configuration..
        $transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, SMTP_PORT, 'tls');
        $mailer = Swift_Mailer::newInstance($transport);
        $result = $mailer->send($emailMessage);
        return $result;
    }

    // Initialize the ContactForm with the information of the form and the possible uploaded file.
    $contactForm = new ContactForm($_POST, $_FILES);

    // Call the "main" method. It will return a list of errors. 
    $errors = main($contactForm);

    require_once("./views/contactForm.php");

【问题讨论】:

  • 你确定 TSL 的端口是 25 吗?
  • 稍后我将与系统管理员再次检查端口,但我只是使用端口 587 再次测试它,我得到了相同的结果。之前有人告诉我,它肯定是 25 端口。
  • 代码是否托管在可以访问 Intranet 的某个地方?如果你说服务器和端口是正确的,那是我唯一能想到的
  • 我目前正在使用 XAMPP 在 Apache 服务器上本地运行它。 Exchange 和 Intranet 服务器实际位于我的办公室并位于本地网络上。

标签: php email swiftmailer


【解决方案1】:

答案很简单,如果邮件主机字段定义了 FQDN(xx.yy.com)而不是 IP 地址,则服务器应该能够解析 FQDN。否则会触发一个名为 Invalid domain name 的错误。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    只是添加到Dilraj Rajan答案。

    在定义 FQDN(完全限定域名)时,请勿在 URL 中添加 http://https:// 前缀。

    那是

    不要这样定义FQDN http://example.com'

    而是像这样定义 FQDNexample.com

    您也可以选择使用应用程序服务器的Public IP Address

    就是这样。

    我希望这会有所帮助

    【讨论】:

      【解决方案3】:

      基本上,对于 SMTP“问候”,Swiftmailer 正在做:

      $response = $this->executeCommand(
                      sprintf("EHLO %s\r\n", $this->domain), [250]
                      );
      

      这个“域”值来自$_SERVER['SERVER_NAME']

      所以你只需要更改 nginx 中的 server_name 值或 Apache 中的 ServerName (在 Apache 中也可以使用 UseCanonicalName on),并确保你没有像 XXXX:8888 这样的端口号( Exchange 不喜欢它)。

      我读到它也可能是由\r\n 引起的(如果第一个解决方案不起作用,请尝试仅使用“\n”)。

      为了确保您发送的内容,只需将$command$responseexecuteCommand(...)Swift_Transport_AbstractSmtpTransport.phpSwift_Transport_EsmtpTransport.php)打印到日志文件来调试 SMTP。

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-15
        相关资源
        最近更新 更多