0
我想用Ajax表單和swiftmailer發送電子郵件。它在當地工作,但不在生產中。405 Ajax POST/Nginx配置錯誤
當contact_me.php不是從表單中獲取參數,而是明確寫入的時候,甚至會從服務器發送郵件,所以我認爲Swiftmailer正在工作。
contact_me.js
// 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
<?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('[email protected]')
->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(['[email protected]' => 'Domain no reply'])
// Set the To addresses
->setTo(['[email protected]', '[email protected]'])
// 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;
}
?>
是否有任何錯誤? – hassan
jquery.min.js:4 POST http://example.com/mail/contact_me_test.php 405(不允許) \t發送@ jquery.min.js:4個 \t AJAX @ jquery.min.js:4 \t submitSuccess @ contact_me.js:22 \t(匿名)@ jqBootstrapValidation.js:76個 \t調度@ jquery.min.js:3個 \t r.handle @ jquery.min.js:3 – user3707264
你的代碼是'網址: 「././ mail/contact_me.php」,錯誤信息爲:/ mail/contact_me_test.php。它甚至沒有試圖發佈到同一個文件? (你也應該刪除URL前面的././ mail/...,並使用根目錄:/ mail/...) –