【问题标题】:Asynchronously Send HTML Email with PHP After Form Submit提交表单后使用 PHP 异步发送 HTML 电子邮件
【发布时间】:2015-11-18 15:22:22
【问题描述】:

我正在尝试制作一个表单,将字段异步提交到数据库中,然后将 html 确认电子邮件发送到提交的电子邮件地址。 db 部分工作正常,但我无法发送 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(检测url中的task变量):

            // 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 格式。

有人可以帮忙吗?提前致谢!

编辑:更多信息,我之前尝试将字符集添加到邮件标题中,但没有成功。但是,如果我尝试使用相同的代码不异步发送邮件,它就可以工作。所以我想知道这是否与异步有关。

【问题讨论】:

  • 也许它被垃圾邮件过滤器阻止了。检查您的邮件日志以查看邮件是否已发送出去。
  • 我会更改您问题的标题,使其更符合您的实际问题,即使用 PHP 邮件发送 HTML 电子邮件。
  • text/html 后面通常应该有一个 ; charset=xxx 选项,也许垃圾邮件过滤器正在阻止它,因为它丢失了。
  • 它没有被垃圾邮件过滤器阻止,$mail_result 永远不会返回带有 html 标头的 true,因此甚至不会发送电子邮件

标签: php jquery email


【解决方案1】:

用这个替换标题上的最后一个附加操作:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

您没有指定charset。阅读this 了解为什么需要它。

【讨论】:

  • 这不起作用,根本没有发送电子邮件。我可以发送的唯一方法是完全删除第二个 $headers。
  • 在字符集中尝试UTF-8 而不是iso-8859-1
猜你喜欢
  • 2015-03-28
  • 2016-07-26
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 2018-11-05
相关资源
最近更新 更多