我尝试制作一个表单,将字段异步提交到数据库中,然后将html确认电子邮件发送到提交的电子邮件地址。数据库部分工作正常,但我无法发送html电子邮件(但没有html电子邮件可用,请参阅下面的更多信息)。
这里是我使用的jquery和php:
jQuery(点击时调用此函数):
function commentFio() {
var name = $('.fio #name_js').val();
var name = encodeURIComponent(name);
var email = $('.fio #email_js').val();
var email = encodeURIComponent(email);
var telephone = $('.fio #telephone_js').val();
var telephone = encodeURIComponent(telephone);
var hkid = $('.fio #hkid_js').val();
var hkid = encodeURIComponent(hkid);
var comment = $('.fio #comment_js').val();
var comment = encodeURIComponent(comment);
if($('.fio #marketing').prop('checked')) {
var marketing = '1';
}
else {
var marketing = '0';
}
if($('.fio #term').prop('checked')) {
var term = '1';
}
else {
var term = '0';
}
var ajaxURL = '<?php echo $campaign_root_directory; ?>/ajax.php?dir=<?php echo $campaign_root_directory; ?>&task=submit_comment&youtuber=fio&name=' + name + '&email=' + email + '&telephone=' + telephone + '&hkid=' + hkid + '&comment=' + comment + '&marketing=' + marketing + '&term=' + term;
$('.video_overlay_wrap .content_wrap').scrollTop(0);
$(".video_overlay_wrap .fio .vote.block").html(load_gif);
$(".video_overlay_wrap .fio .vote.block").load(ajaxURL, function() {
$(".video_overlay_wrap .fio .comment.block").html(load_gif);
$(".video_overlay_wrap .fio .comment.block").load("<?php echo $campaign_root_directory; ?>/ajax.php?dir=<?php echo $campaign_root_directory; ?>&task=fetch_comment&youtuber=fio");
});
}
PHP(检测网址中的任务变量):
// Localize and sanitize
$youtuber = strip_tags(mysql_real_escape_string($_GET['youtuber']));
$name = strip_tags(mysql_real_escape_string($_GET['name']));
$email = strip_tags(mysql_real_escape_string($_GET['email']));
$telephone = preg_replace('/\D/', '', strip_tags(mysql_real_escape_string($_GET['telephone'])));
$hkid = strip_tags(mysql_real_escape_string($_GET['hkid']));
$comment = strip_tags(mysql_real_escape_string($_GET['comment']));
$marketing = strip_tags(mysql_real_escape_string($_GET['marketing']));
$term = strip_tags(mysql_real_escape_string($_GET['term']));
// Clear default
if($name==$field_name) {
$name = '';
}
elseif($email==$field_email) {
$email = '';
}
elseif($telephone==$field_telephone) {
$telephone = '';
}
elseif($hkid==$field_hkid) {
$hkid = '';
}
elseif($comment==$field_comment) {
$comment = '';
}
// Set check box value
if($marketing==1) {
$marketing = 'I agree to receive promotional material.';
}
else {
$marketing = '';
}
if($term==1) {
$term = 'I have read and agree to the Terms and Privacy Policy.';
}
else {
$term = '';
}
// Validate fields
if($youtuber!='' && $name!='' && $email!='' && filter_var($email, FILTER_VALIDATE_EMAIL) && $telephone!='' && is_numeric ($telephone) && $hkid!='' && strlen($hkid)<=10 && strlen($hkid)>=7 && $comment!='' && $term!='') {
$insert = mysql_query("
INSERT INTO
comment_index (
last_update,
cookie_id,
youtuber,
name,
email,
telephone,
hkid,
comment,
marketing,
term,
ip,
user_agent
)
VALUES (
'$current_time',
'$cookie_id',
'$youtuber',
'$name',
'$email',
'$telephone',
'$hkid',
'$comment',
'$marketing',
'$term',
'$ip',
'$user_agent'
)
");
if($insert) {
// Set Email Content
$recipient = $email;
$subject = "Subject";
$message = '
this is the first line.<br>
this is the second line.
';
// Send email
$sender = 'Sender Name <info@email.com>';
$headers = "From:" . $sender . "\r\n";
$headers .= 'Content-type: text/html' . "\r\n";
$mail_result = mail($recipient,$subject,$message,$headers);
if($mail_result) {
$output = 'Success!';
}
else {
$output = 'Fail...';
}
}
else {
$output = 'Error';
}
我认为问题出在$headers .= 'Content-type: text/html' . "\r\n";
上,因为如果删除该行,电子邮件会成功发送,但不会以html格式发送。
有人可以帮忙吗?提前谢谢!
编辑:更多信息,我尝试将字符集添加到邮件标题之前,但它没有用。但是,如果我尝试使用相同的代码来发送邮件而不是异步,它可以工作。所以我想知道这是否与async有关。
答案 0 :(得分:0)
用以下内容替换标题上的最后一个追加操作:
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
您没有指定charset
。阅读this以了解为何需要它。