1

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; \r\n"; 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\r\n";

    $headers .= "Content-Type: text/html; \r\n";
    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">&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>
  • Can you debug the value for $message that you pass to wp_mail()? Are the tags replaced by &#60; and &#62;? – fischi Feb 23 '16 at 9:49
  • No. when i don't set header it is showing as <a href='example.com'>example</a> – milan patel Feb 23 '16 at 9:51
  • Can you please post a debug of that value? – fischi Feb 23 '16 at 11:20
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.