gpt4 book ai didi

java - Sendmail.php 网络表单发送空白电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:36 25 4
gpt4 key购买 nike

好吧,对于这一切,我有点菜鸟...但是,在今天尝试让它运行近 10 个小时后,我现在正处于崩溃的边缘,我尝试的一切似乎都失败了,所以任何建议都将不胜感激。

电子邮件发送正常,但到达后仅显示:

姓名:

姓氏:

电子邮件:

消息:

除此之外,我无法完成任何其他事情。

我的 PHP 代码是:

<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);

$name = $_POST['name'];
$surname = $_POST[‘surname’];
$email = $_POST[‘email’];
$message = $_POST[‘message’];
$subject = "Contact Form Submission";

$email_from = $email;
$email_to = 'admin@park.co.uk';

$body = 'Name: ' . $name . "\n\n" . 'Surname: ' . $surname . "\n\n" . 'Email: ' . $email . "\n\n" . 'Message: ' . $message;

$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;

主要JS代码如下:

   //contact form
var form = $(‘.contact-form’);
form.submit(function () {
$this = $(this);
$.post($(this).attr(‘action’),$(‘.contact-form’).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
$this.closest(‘.contact-form’).find(“input[type=text], input[type=email], textarea”).val(“”);
},’json’);
return false;
});
//goto top
$(‘.gototop’).click(function(event) {
event.preventDefault();
$(‘html, body’).animate({
scrollTop: $(“body”).offset().top
}, 500);
});

HTML 代码是:

<h4>Contact Form</h4>
<form id="main-contact-form" class="contact-form" name="contact-form"

method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group"> <input class="form-control" required="required"

placeholder="Name" type="text"> </div>
<div class="form-group"> <input class="form-control" required="required"

placeholder="Surname" type="text"> </div>
<div class="form-group"> <input class="form-control" required="required"

placeholder="Email" type="text"> </div>
<div class="form-group"> <button type="submit" class="btn btn-primary btn-lg">Send
Message</button> </div>
</div>
<div class="col-sm-7"> <textarea name="message" id="message" required="required"

class="form-control" rows="8" placeholder="Message"></textarea>
<input name="subject" value="Form submission" type="hidden"> </div>
</div>
</form>
</div>

如果有人能帮助我完成这项工作,我将非常感激,我的理智也会如此!

提前致谢。

最佳答案

很难确定是什么原因导致了您的问题,但存在几个问题,包括:

  • 您的输入没有 serialize() 完成其工作所需的 name 属性
  • 正如 @Machavity 指出的,您的代码中有一些弯引号,这些引号应该是直引号
  • 无论发生什么情况,您的 PHP 页面都会回显电子邮件已成功发送
<小时/>

如果是我,我会使用 ajax 来实现此目的,具体方法如下:

找到完整的工作示例 here

HTML:

  <div class="row">
<h4>Contact Form</h4>
<div id="data"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input class="form-control" required="required" placeholder="Email to send to.. (Added for the demo)" name="to" type="text">
</div>
<div class="form-group">
<input class="form-control" required="required" placeholder="Name" name="name" type="text">
</div>
<div class="form-group">
<input class="form-control" required="required" placeholder="Surname" name="surname" type="text">
</div>
<div class="form-group">
<input class="form-control" required="required" placeholder="Email" name="email" type="text">
</div>
<div class="form-group">
<textarea required class="form-control" rows="8" placeholder="Message" name="message"></textarea>
<input name="subject" value="Form submission" type="hidden">
</div>
</div>
</div>
<div class="form-group">
<button id="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</form>
</div>

JavaScript:

$(function() {  
//contact form
$('#main-contact-form').submit(function(event) {
event.preventDefault(); // stop the normal form submission
var sendVars = $(this).serialize();
$.ajax({
type: "POST",
url: "sendemail.php",
data: sendVars,
dataType: 'json',
success: function(data) {
// do stuff...
console.log(data);
$('#data').text(data.message).fadeIn().delay(3000).fadeOut();
$('#main-contact-form').find('input, textarea').val('');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
//goto top
$('.gototop').click(function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $('body').offset().top
}, 500);
});
});

PHP

<?php
//ini_set('display_errors',1); //error reporting if needed
//ini_set('display_startup_errors',1);
//error_reporting(-1);
header('Content-type: application/json');
$status = array( 'fail'=>array('type'=>'failure',
'message'=>'Email Failed!'),
'succeed'=>array('type'=>'success',
'message'=>'Email Sent!'));

$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']): null;
$surname = isset($_POST['surname']) ? htmlspecialchars($_POST['surname']): null;
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']): null;
$message = isset($_POST['message']) ? htmlspecialchars($_POST['message']): null;
$subject = "Contact Form Submission";

$email_from = $email;
$email_to = isset($_POST['message']) ? htmlspecialchars($_POST['to']): null;

$body = 'Name: ' . $name . "\n\n" . 'Surname: ' . $surname . "\n\n" . 'Email: ' . $email . "\n\n" . 'Message: ' . $message;

if(mail($email_to, $subject, $body, 'From: <'.$email_from.'>')){
echo json_encode($status['succeed']);
die;

}
else{
echo json_encode($status['fail']);
}
?>

关于java - Sendmail.php 网络表单发送空白电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com