我試圖製作一個窗體,將字段異步提交到數據庫,然後發送一個HTML構造電子郵件到提交的電子郵件地址。數據庫部分工作正常,但我不能發送HTML電子郵件(但沒有HTML電子郵件的作品,請參閱下面的更多信息)。與表單提交後異步發送HTML電子郵件與PHP
這裏的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 <[email protected]>';
$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格式。
任何人都可以請幫忙嗎?提前致謝!
編輯:多一點信息,我試圖將郵件標題添加到郵件標題之前,但它沒有工作。但是,如果我嘗試使用相同的代碼來非異步發送郵件,它就會起作用。所以我想知道這是否與異步有關。
也許它被垃圾郵件過濾器阻止。檢查你的郵件日誌,看看郵件是否發生。 – Barmar
我會改變你的問題的標題,以更符合你的實際問題,這是用PHP郵件發送HTML郵件。 – raduation
'text/html'通常應該有';之後的charset = xxx'選項,可能垃圾郵件過濾器會因爲丟失而阻止它。 – Barmar