首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >405 Ajax POST / Nginx配置错误

405 Ajax POST / Nginx配置错误
EN

Stack Overflow用户
提问于 2017-06-18 15:45:58
回答 1查看 6.3K关注 0票数 0

我正在尝试使用Ajax表单和swiftmailer发送电子邮件。它可以在本地运行,但不能在生产环境中运行。

当contact_me.php不是从表单中获取参数,而是显式地编写参数时,即使是从服务器也会发送电子邮件,所以我认为Swiftmailer是有效的。

contact_me.js

代码语言:javascript
复制
// Contact Form Scripts

$(function() {

    $("#contactForm input,#contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                dataType: "text",
                cache: false,
                success: function() {
                    // Success message       
                },
                error: function() {
                    // Fail message                    
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

contact_me.php

代码语言:javascript
复制
<?php
// Autoload for swiftmailer
require_once '../vendor/autoload.php';

// Check for empty fields
if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['message'])   ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
   echo "No arguments Provided!";
   return false;
   }


$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$email_subject = "TrustPair nouveau contact :  $name";
$email_body = "New form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";

// Add here swiftmailer code, need to return true
// Create the Transport
$transport = (new Swift_SmtpTransport('mail.gandi.net', 465, "ssl"))
  ->setUsername('name@domain.com')
  ->setPassword('password')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create the message
$message = (new Swift_Message())
    // Give the message a subject
    ->setSubject($email_subject)
    // Set the From address with an associative array
    ->setFrom(['noreply@domain.com' => 'Domain no reply'])
    // Set the To addresses
    ->setTo(['firstmailto@gmail.com', 'secondmailto@gmail.com'])
    // Give it a body
    ->setBody($email_body)
    ;

// Send the message
$result = $mailer->send($message);
echo $result;
// result is equal to the nbr of message recipients

if ($result == 0) {
    return false;
} else {
    return true;
}


?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-18 18:07:20

Nginx服务器不允许带有静态页面的POST请求(即。*.html)。

hacks可以解决这个问题。在我的例子中,它修复了405错误,但电子邮件没有发送。

解决方案是将index.html更改为index.php,请务必使用adapt your Nginx configuration来反映此更改。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44612803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档