gpt4 book ai didi

PHP 邮件脚本不发送文本区域并复制正文中的所有其他内容

转载 作者:行者123 更新时间:2023-11-28 01:21:38 38 4
gpt4 key购买 nike

我正在尝试使用我制作的以下脚本发送电子邮件。但是似乎遇到了一个奇怪的问题,我需要帮助。

邮件脚本

// Get field values.
$name = strip_tags($_POST["name"]);
$email = strip_tags($_POST["email"]);
$message = $_POST["msg"];

// Check if e-mail address is valid.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set e-mail and subject.
$to = "mail@mydomain.dk";
$subject = "You have a new message.";

// Set header values.
$headers = "From: " . $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// Set request body.
$message = "<html>";
$message .= "<body>";
$message .= "<p><b>From:</b><br>" . $name . "</p>";
$message .= "<p><b>Email:</b><br>" . $email . "</p>";
$message .= "<p><b>Message:</b><br>" . $message . "</p>";
$message .= "</body>";
$message .= "</html>";

mail($to, $subject, $message, $headers);
echo "Your email was sent!";

} else {

echo "Invalid Email, please provide an correct email.";

}

HTML

<form id="contact-form" data-toggle="validator" data-disable="true" role="form">
<div class="form-group">
<label for="name">Navn</label>
<input type="text" name="name" id="contact-name" class="form-control" data-minlength="2" data-error="Please provide a valid name." required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" name="email" id="contact-email" class="form-control" data-minlength="5" data-error="Please provide a valid e-mail address." required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="message">Your message:</label>
<textarea name="msg" id="contact-email" data-minlength="10" data-error="Your message must be at least 10 characters long." class="form-control" required></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<button id="submit" value="send" class="btn btn-primary">Send</button>
<div id="success"></div>
</div>
</form>

Javascript

$(document).ready(function(){

$('#success').css('display', 'none');

$('#submit').click(function(e) {
e.preventDefault();
$.ajax({
url: "php/form.php",
data: $("#contact-form").serialize(),
type: 'POST',
statusCode: {
500: function(data) {
$('#success').css('display', 'none');
$('#success').css('color', '#A94442');
$('#success').html('Your message was not sent.');
$('#success').fadeIn(200);
},
404: function(data) {
$('#success').css('display', 'none');
$('#success').css('color', '#A94442');
$('#success').html('Your message was not sent.');
$('#success').fadeIn(200);
},
200: function(data) {
console.log(data);
$('#success').css('display', 'none');
$('#success').css('color', '#74C274');
$('#success').html('Your message was sent.');
$('#success').fadeIn(200);
}
}
});
});
});

电子邮件已发送和接收,但文本区域未发送通过,并且似乎在邮件正文中发送了两次“电子邮件”和“姓名”字段。

电子邮件输出如下所示:

From:
Someone

Email:
someone@someone.com

Besked:

From:
Someone

Email:
someone@someone.com

非常感谢您的帮助。几个小时以来一直在努力解决这个问题。

最佳答案

错误位于此处:

$message .=     "<p><b>Message:</b><br>" . $message . "</p>";

您对要发送的消息和 PHP 接收的消息使用相同的变量。

此代码将有效:

// Get field values.
$name = strip_tags($_POST["name"]);
$email = strip_tags($_POST["email"]);
$message_text = $_POST["msg"];

// Check if e-mail address is valid.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set e-mail and subject.
$to = "mail@mydomain.dk";
$subject = "You have a new message.";

// Set header values.
$headers = "From: " . $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// Set request body.
$message = "<html>";
$message .= "<body>";
$message .= "<p><b>From:</b><br>" . $name . "</p>";
$message .= "<p><b>Email:</b><br>" . $email . "</p>";
$message .= "<p><b>Message:</b><br>" . $message_text . "</p>";
$message .= "</body>";
$message .= "</html>";

mail($to, $subject, $message, $headers);
echo "Your email was sent!";

} else {

echo "Invalid Email, please provide an correct email.";

}

关于PHP 邮件脚本不发送文本区域并复制正文中的所有其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719184/

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