设置标题后无法在电子邮件中发送链接
问题描述:
I am not able send links in email.
I am using wp_mail()
function to send email.
When i set header $headers .= "Content-Type: text/html;
";
this header removes href
attribute in received email.
i am using wp_editor for textarea. I also tried html textarea but the result was same.
When i send mail without setting header, i am getting anchor tag as text.<a href=\'http://example.com\'>Example</a>
here is my code.
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());
}
});
});
And this is php code
add_action('wp_ajax_mail_link_popup','mail_link_popup');
function mail_link_popup()
{
// $headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html;
";
wp_mail($_REQUEST['to_email'],$_REQUEST['subject'],$_REQUEST['message_test'],$headers);
}
Here is HTML form
<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">×</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>
答
Nothing is wrong with the code.
I just added message body in 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);
}
And it's working now
答
Thats not how content header works in Wordpress. You need to set header through 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. Do not forget to reset content types.