Home > Back-end >  Sending HTML via ajax - adds a backsklash in a tag
Sending HTML via ajax - adds a backsklash in a tag

Time:11-26

the following problem. I get a textblock with placeholder tags, replace these placeholders with the correct content and place this textblock in a textarea. jquery val().

In this textarea everything is displayed correctly. line breaks and also a URL link. (the HTML of the a tag)

If I now get this val() and send it via Ajax to a PHP function which sends a mail, the problem occurs. The textblock is the textbody of the mail.

The text block is displayed correctly in the mail, Bold tags, line breaks, no problem.

But with the a tag I get backslashes in.

I have now looked a lot, with escaping, json, etc but it will not succeed.

Probably I have lost my way. Can someone tell me where it is and point me in the right direction.

Here are a few code excerpts.

$( ".jsClickClaimsSendSH" ).click(function() {
    var ajaxurl = $(this).data("ajax");
    var action = 'claim_defects_breach_of_contract_send_mail';
    
    var claimtype = $(this).data("claimtype");
    var leadID = $(this).data("leadid");
    var uniqueid = $(this).data("uniqueid");
    
    var subject = $("#claimsmailbodycontainer .claimssubjectbody").val();
    var mailbody = $("#claimsmailbodycontainer .claimsmailbody").val();

    $.ajax({
        type: "POST",
        url: ajaxurl,
    
        data: {
            action : action,
            claimtype : claimtype,
            leadID : leadID,
            uniqueid : uniqueid,
            subject : subject,
            mailbody : mailbody
        },
        success: function( result ){
        }
    });         
    
    
});

In the PHP function it looks like this

$mailbody = $_POST['mailbody'];


$headers = [];
$headers[] = 'From: '.$mailFrom;
$headers[] = 'Reply-To: '.$mailReplyTo;        
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'X-Mailer: PHP/' . phpversion(); 

ob_start();
include_mail_template_php ($getPageLanguage.'/claimmail');
$message = ob_get_contents();
ob_end_clean(); 

// '%%text-body%%' is placeholder tag in the mail template for the $mailbody
$variables = array(
    '%%text-body%%',
); 

// set replacements for variables
$values = array(
    $mailbody
);

$message = str_replace( $variables, $values, $message );


wp_mail($empfaenger, $subject, $message, $headers);

The HTML of the a tag in the mail looks like this. You cant click on that link.

   <a href="http://localhost/abc/page/?l=6194a9edac24c" target="_blank">more</a>

additions1

In the request from the jquery call, the HTML still looks good.

  <a href="http://localhost/abc/page/?l=6194a9edac24c" target="_blank">hier</a>

The part of the mail template where a placeholder tag is located. This placeholder will be replaced with the mail body text.

<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
    <td style="padding: 20px; font-family: sans-serif; font-size: 15px; line-height: 20px; color: #555555; background-color: white; white-space: pre-line; ">
        <p style="margin: 0;">
            %%text-body%%
        </p>
    </td>
</tr>  

additions2

Where does the a day come from. I have saved a text with ACF and the placeholder is like this

  <a href="%%LinkInvoice%%" target="_blank">more</a>

CodePudding user response:

Have you tried using the wp_unslash() function.

Usage in this case would be something like:

$message = str_replace( $variables, $values, $message );
$message = wp_unslash( $message ); // Strip slashes here!


wp_mail($empfaenger, $subject, $message, $headers);

Or maybe even...

// set replacements for variables
$values = array(
    wp_unslash( $mailbody )
);

Your question doesn't give enough information to where the example <a> tag output is coming from.

  • Related