如何通过ajax上传html电子邮件模板数据



我正在尝试从数据库中编辑电子邮件模板的内容,并将其发送给我的用户。现在,每当我点击发送按钮时,只有一半的html电子邮件内容被发送到用户的电子邮件地址。

这是我的代码

$this->validate($request, [
'to' => 'required|email',
'contents' => 'required',
'subject'=> 'required'
]);
$data = [
'to_user' => $request->to,
'content' => $request->contents,
'type' => 'email'
];
$unique = str_random(6) . '-'.str_random(5);
File::put("/var/www/resources/views/mails/" . $unique.".blade.php", $request->contents);
$datas = [
'from' => 'support@example.com',
'from_name' => 'Example',
'reply_to' => 'support@example.com',
'reply_to_name'=> 'Example Support Team',
'subject'=> $request->subject,
'type' => 'email',
'code' => $unique
];
AppDeliveryLog::create($data);
Mail::to($request->to)->send(new DeliveryMail($datas));

这是发送到控制器的请求的屏幕截图。检查屏幕截图!

这是Ajax代码

$('.sendEmail').on('click', function () {
email = $('#semail').val();
subject = $('#subject').val();
content = CKEDITOR.instances.CustomerEmailTemplate_content.getData();
$(".sendEmail").text("Sending... Please wait.");
$('.sendEmail').prop('disabled', true);
request = $.ajax({
url: "/ajax/send/email",
type: "post",
data: "to=" + email + "&subject=" + subject + "&contents=" + content + "&_token=" + $('meta[name="csrf-token"]').attr('content')
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR) {
// Log a message to the console
if (response.result === "success") {
iziToast.success({
title: 'Success',
message: 'Email Sent Successfully.',
position: 'topRight',
timeout: '10000',
pauseOnHover: true,
});
} else {
iziToast.warning({
title: 'Success',
message: response.message,
position: 'topRight',
timeout: '10000',
pauseOnHover: true,
});
}
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown) {
// Log the error to the console
console.error(
"The following error occurred: " +
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
$(".sendEmail").text("Send Email");
$('.sendEmail').prop('disabled', false);
});
});

我没有收到任何错误,但只存储了一半的电子邮件。

这是因为你的HTML内容被解释为许多post字段,你的屏幕截图显示了一个名为subject的属性,其他内容,然后是一个称为<html><head>等的属性。一个可能的解决方案是在通过HTTP请求发送模板之前序列化模板,然后取消序列化。

相关内容

最新更新