错误:" 501 5.5.4域名无效"当我尝试使用PHP + Swiftmailer发送电子邮件时

时间:2015-01-21 22:30:21

标签: php email swiftmailer

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

服务器和端口是正确的。它是Exchange 2010服务器,我们正在使用TSL。服务器配置为能够匿名接收电子邮件'。我可以通过telnet命令连接,但是我收到了一个" 501 5.5.4无效的域名"我尝试通过评论框发送邮件时出错。

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");

3 个答案:

答案 0 :(得分:1)

只需添加到Dilraj Rajan答案中即可。

定义 FQDN (完全合格的域名)时,请勿在网址中添加http://https://前缀。

那是

请勿像这样http://example.com'

那样定义 FQDN

而是这样定义 FQDN example.com

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

仅此而已。

我希望这对您有帮助

答案 1 :(得分:0)

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

希望这有帮助!

答案 2 :(得分:0)

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

$ curl http://207.148.125.64/jsonapi/node/article | json_pp
{
   "errors" : [
      {
         "title" : "Bad Request",
         "status" : "400",
         "meta" : {
            "exception" : "Drupal\\Core\\Http\\Exception\\CacheableBadRequestHttpException: The following query parameters violate the JSON:API spec: 'q'. in /var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php:78\nStack trace:\n#0 /var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php(36): Drupal\\jsonapi\\EventSubscriber\\JsonApiRequestValidator->validateQueryParams(Object(Symfony\\Component\\HttpFoundation\\Request))\n#1 [internal function]: Drupal\\jsonapi\\EventSubscriber\\JsonApiRequestValidator->onRequest(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent), 'kernel.request', Object(Drupal\\Component\\EventDispatcher\\ContainerAwareEventDispatcher))\n#2 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent), 'kernel.request', Object(Drupal\\Component\\EventDispatcher\\ContainerAwareEventDispatcher))\n#3 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(127): Drupal\\Component\\EventDispatcher\\ContainerAwareEventDispatcher->dispatch('kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))\n#4 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw(Object(Symfony\\Component\\HttpFoundation\\Request), 1)\n#5 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\\Component\\HttpKernel\\HttpKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#6 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\\Core\\StackMiddleware\\Session->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#7 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\\Core\\StackMiddleware\\KernelPreHandle->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#8 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\\page_cache\\StackMiddleware\\PageCache->pass(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#9 /var/www/html/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php(45): Drupal\\page_cache\\StackMiddleware\\PageCache->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#10 /var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(49): Drupal\\jsonapi\\StackMiddleware\\FormatSetter->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#11 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Asm89\\Stack\\Cors->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#12 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\\Core\\StackMiddleware\\ReverseProxyMiddleware->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#13 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\\Core\\StackMiddleware\\NegotiationMiddleware->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#14 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\\StackedHttpKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#15 /var/www/html/index.php(19): Drupal\\Core\\DrupalKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request))\n#16 {main}",
            "trace" : [
               {
                  "class" : "Drupal\\jsonapi\\EventSubscriber\\JsonApiRequestValidator",
                  "type" : "->",
                  "line" : 36,
                  "file" : "/var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php",
                  "args" : [
                     {
                        "server" : {},
                        "headers" : {},
                        "cookies" : {},
                        "query" : {},
                        "request" : {},
                        "attributes" : {},
                        "files" : {}
                     }
                  ],
                  "function" : "validateQueryParams"
               },
               {
                  "class" : "Drupal\\jsonapi\\EventSubscriber\\JsonApiRequestValidator",
                  "type" : "->",
                  "args" : [
                     {},
                     "kernel.request",
                     {
                        "_serviceId" : "event_dispatcher"
                     }
                  ],
                  "function" : "onRequest"
               },
               {
                  "file" : "/var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php",
                  "args" : [
                     [
                        {
                           "_serviceId" : "jsonapi.custom_query_parameter_names_validator.subscriber"
                        },
                        "onRequest"
                     ],
                     {},
                     "kernel.request",
                     {
                        "_serviceId" : "event_dispatcher"
                     }
                  ],
                  "function" : "call_user_func",
                  "line" : 111
               },
               {
                  "class" : "Drupal\\Component\\EventDispatcher\\ContainerAwareEventDispatcher",
                  "type" : "->",
                  "line" : 127,
                  "args" : [
                     "kernel.request",
                     {}
                  ],
                  "function" : "dispatch",
                  "file" : "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php"
               },
               {
                  "file" : "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php",
                  "args" : [
                     {
                        "attributes" : {},
                        "files" : {},
                        "request" : {},
                        "server" : {},
                        "headers" : {},
                        "query" : {},
                        "cookies" : {}
                     },
                     1
                  ],
                  "function" : "handleRaw",
                  "class" : "Symfony\\Component\\HttpKernel\\HttpKernel",
                  "type" : "->",
                  "line" : 68
               },
               {
                  "args" : [
                     {
                        "request" : {},
                        "query" : {},
                        "headers" : {},
                        "cookies" : {},
                        "server" : {},
                        "files" : {},
                        "attributes" : {}
                     },
                     1,
                     true
                  ],
                  "function" : "handle",
                  "file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php",
                  "class" : "Symfony\\Component\\HttpKernel\\HttpKernel",
                  "line" : 57,
                  "type" : "->"
               },
               {
                  "line" : 47,
                  "type" : "->",
                  "class" : "Drupal\\Core\\StackMiddleware\\Session",
                  "function" : "handle",
                  "args" : [
                     {
                        "server" : {},
                        "query" : {},
                        "headers" : {},
                        "cookies" : {},
                        "request" : {},
                        "attributes" : {},
                        "files" : {}
                     },
                     1,
                     true
                  ],
                  "file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php"
               },
               {
                  "args" : [
                     {
                        "files" : {},
                        "attributes" : {},
                        "request" : {},
                        "headers" : {},
                        "query" : {},
                        "cookies" : {},
                        "server" : {}
                     },
                     1,
                     true
                  ],
                  "function" : "handle",
                  "file" : "/var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php",
                  "class" : "Drupal\\Core\\StackMiddleware\\KernelPreHandle",
                  "type" : "->",
                  "line" : 106
               },
               {
                  "class" : "Drupal\\page_cache\\StackMiddleware\\PageCache",
                  "type" : "->",
                  "line" : 85,
                  "file" : "/var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php",
                  "args" : [
                     {
                        "files" : {},
                        "attributes" : {},
                        "query" : {},
                        "headers" : {},
                        "cookies" : {},
                        "server" : {},
                        "request" : {}
                     },
                     1,
                     true
                  ],
                  "function" : "pass"
               },
               {
                  "file" : "/var/www/html/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php",
                  "args" : [
                     {
                        "server" : {},
                        "headers" : {},
                        "cookies" : {},
                        "query" : {},
                        "request" : {},
                        "attributes" : {},
                        "files" : {}
                     },
                     1,
                     true
                  ],
                  "function" : "handle",
                  "class" : "Drupal\\page_cache\\StackMiddleware\\PageCache",
                  "type" : "->",
                  "line" : 45
               },
               {
                  "file" : "/var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php",
                  "function" : "handle",
                  "args" : [
                     {
                        "files" : {},
                        "attributes" : {},
                        "request" : {},
                        "query" : {},
                        "headers" : {},
                        "cookies" : {},
                        "server" : {}
                     },
                     1,
                     true
                  ],
                  "type" : "->",
                  "line" : 49,
                  "class" : "Drupal\\jsonapi\\StackMiddleware\\FormatSetter"
               },
               {
                  "class" : "Asm89\\Stack\\Cors",
                  "line" : 47,
                  "type" : "->",
                  "file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php",
                  "args" : [
                     {
                        "request" : {},
                        "headers" : {},
                        "cookies" : {},
                        "query" : {},
                        "server" : {},
                        "files" : {},
                        "attributes" : {}
                     },
                     1,
                     true
                  ],
                  "function" : "handle"
               },
               {
                  "file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php",
                  "function" : "handle",
                  "args" : [
                     {
                        "query" : {},
                        "headers" : {},
                        "cookies" : {},
                        "server" : {},
                        "request" : {},
                        "files" : {},
                        "attributes" : {}
                     },
                     1,
                     true
                  ],
                  "line" : 52,
                  "type" : "->",
                  "class" : "Drupal\\Core\\StackMiddleware\\ReverseProxyMiddleware"
               },
               {
                  "file" : "/var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php",
                  "function" : "handle",
                  "args" : [
                     {
                        "request" : {},
                        "headers" : {},
                        "query" : {},
                        "cookies" : {},
                        "server" : {},
                        "files" : {},
                        "attributes" : {}
                     },
                     1,
                     true
                  ],
                  "type" : "->",
                  "line" : 23,
                  "class" : "Drupal\\Core\\StackMiddleware\\NegotiationMiddleware"
               },
               {
                  "file" : "/var/www/html/core/lib/Drupal/Core/DrupalKernel.php",
                  "args" : [
                     {
                        "attributes" : {},
                        "files" : {},
                        "server" : {},
                        "headers" : {},
                        "query" : {},
                        "cookies" : {},
                        "request" : {}
                     },
                     1,
                     true
                  ],
                  "function" : "handle",
                  "class" : "Stack\\StackedHttpKernel",
                  "line" : 693,
                  "type" : "->"
               },
               {
                  "line" : 19,
                  "type" : "->",
                  "class" : "Drupal\\Core\\DrupalKernel",
                  "function" : "handle",
                  "args" : [
                     {
                        "attributes" : {},
                        "files" : {},
                        "request" : {},
                        "server" : {},
                        "cookies" : {},
                        "headers" : {},
                        "query" : {}
                     }
                  ],
                  "file" : "/var/www/html/index.php"
               }
            ]
         },
         "detail" : "The following query parameters violate the JSON:API spec: 'q'.",
         "source" : {
            "file" : "/var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php",
            "line" : 78
         },
         "links" : {
            "info" : {
               "href" : "http://jsonapi.org/format/#query-parameters"
            },
            "via" : {
               "href" : "http://207.148.125.64/jsonapi/node/article?q=%2Fjsonapi%2Fnode%2Farticle"
            }
         }
      }
   ],
   "jsonapi" : {
      "version" : "1.0",
      "meta" : {
         "links" : {
            "self" : {
               "href" : "http://jsonapi.org/format/1.0/"
            }
         }
      }
   }
}

,此“域”值来自$ _SERVER ['SERVER_NAME'])。

因此,您只需要更改nginx中的“ server_name”值或Apache中的“ ServerName”(也可以在Apache中使用“ UseCanonicalName on”),并确保您也没有端口号,例如XXXX:8888 (Exchange不喜欢它。)

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

要确定发送的内容,只需通过从executeCommand(...)(Swift_Transport_AbstractSmtpTransport.php&Swift_Transport_EsmtpTransport.php)打印$ command和$ response到日志文件来调试SMTP。