设置标题后无法在电子邮件中发送链接

时间:2016-02-23 09:38:09

标签: php html wordpress html-email

我无法在电子邮件中发送链接 我正在使用wp_mail()函数发送电子邮件 当我设置标题$headers .= "Content-Type: text/html; \r\n";时,此标头会删除收到的电子邮件中的href属性。

我正在使用wp_editor for textarea。我也试过html textarea但结果是一样的。

当我在没有设置标题的情况下发送邮件时,我将锚标记作为文本 <a href=\'http://example.com\'>Example</a>

这是我的代码。

jQuery('.for_email').click(function(e){
        e.preventDefault();
        var mail_data = jQuery('.email_popup_form').serialize();
        //console.log(mail_data);
        jQuery.ajax({
            url:"<?php echo admin_url('admin-ajax.php'); ?>",
            data:'action=mail_link_popup&'+mail_data,
            success:function(res)
            {
                //console.log(jQuery('#mycustomeditor_afds_ifr').contents().find('#tinymce').html());
            }
        });
    });

这是php代码

add_action('wp_ajax_mail_link_popup','mail_link_popup');
function mail_link_popup()
{
//  $headers .= "MIME-Version: 1.0\r\n";

    $headers .= "Content-Type: text/html; \r\n";
    wp_mail($_REQUEST['to_email'],$_REQUEST['subject'],$_REQUEST['message_test'],$headers);
}  

这是HTML表单

 <div class="modal fade" id="email_link_myModal" role="dialog">
    <div class="modal-dialog modal-lg">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h3 class="modal-title">Email</h3>
        </div>
        <div class="modal-body">
          <div class='row'>
              <div class='container'>
              <div class='col-md-9'>

                <form class="form-horizontal email_popup_form" role="form">

                  <div class="form-group">
                    <label class="control-label col-sm-2" for="email">To:</label>
                    <div class="col-sm-10">
                      <input type="email" class="form-control to_email" id="email" name='to_email'>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label col-sm-2" for="pwd">From:</label>
                    <div class="col-sm-10"> 
                      <input type="email" class="form-control send_email" id="pwd" name='send_email' >
                    </div>
                  </div>

                  <div class="form-group"> 
                    <label class="control-label col-sm-2" for="sub">Subject:</label>
                    <div class="col-sm-10"> 
                      <input type="text" class="form-control subject" id="sub" name='subject'>
                    </div>
                  </div>

                  <div class="form-group"> 
                      <label class="control-label col-sm-2" for="sub"></label>
                      <div class="col-sm-10"> 
                          <?php 
                          $content = '<div class="cont_test"></div>';
                            $editor_id = 'mycustomeditor_afds';
                            $settings = array(
                            'media_buttons'=>false,
                            'textarea_name'=>'message_test' ,
                            'editor_class'=>'add_link_test' ,
                            'media_buttons' => false,
                            'quicktags'     => TRUE
                            );
                            //wp_editor( $content, $editor_id ,$settings);

                          ?>
                        <textarea class='cont_test' name='message_test'></textarea>
                      </div>
                  </div>

                  <div class="form-group"> 
                    <div class="col-sm-offset-2 col-sm-10">
                      <button type="submit" class="btn btn-default for_email">Send Email</button>
                    </div>
                  </div>
                </form>
              </div>
              </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
</div>

2 个答案:

答案 0 :(得分:0)

这不是Wordpress中内容标题的工作原理。您需要通过add_filter

设置标题
add_filter( 'wp_mail_content_type', 'setHeader' );
wp_mail( 'me@example.net', 'The subject', '<a href="http://some.com"></a>' );
remove_filter( 'wp_mail_content_type', 'setHeader' ); 

function setHeader(){
    return 'text/html';
}

P.S。不要忘记重置内容类型。

答案 1 :(得分:0)

代码没有问题。 我刚刚在stripslashes()

中添加了邮件正文
add_action('wp_ajax_mail_link_popup','mail_link_popup');
function mail_link_popup()
{
    $headers = 'Content-type: text/html;charset=utf-8';
    wp_mail($_REQUEST['to_email'],$_REQUEST['subject'],stripslashes($_REQUEST['message_test']),$headers);

}

它现在正在运作