X

A blog about digital & online

Remove P and BR Tags from HTML Email Output – Contact Form 7

Contact form 7 is a hugely popular and free WordPress plugin, so you’d think something like this would be built in, but I had the following issue:

There is no way to use functions.php filters or settings in the plugin to stop the HTML email output from adding <p> and <br /> tags into your outgoing emails.

After hours of Googling, I discovered that the popular method of disabling WPCF7_AUTOP in your config.php, doesn’t work for the HTML emails. In fact this one has the WordPress community pretty puzzled, such as in this thread.

I eventually discovered  a solution, by editing the plugin files (only way I could find):

In Contact Form 7’s core plugin files, find the file:

wp-content > plugins > contact-form-7 > includes > mail.php

Go to about line # 82 in the file, where you will find:

return wp_mail( $recipient, $subject, $body, $headers, $attachments );

Immediately above that line, add in a new line:

$body = str_replace(‘<p>’, ”, $body);
$body = str_replace(‘</p>’, ”, $body);
$body = str_replace(‘<br />’, ”, $body);

PRESTO!

only problem is that it will remove all of these tags, even if you placed them manually in your email templates.

So, to fix that, just add a special class to your tags in your lovely email template. Example: <p class=”myclass”> <br class=”myclass” />. This will prevent the above solution from ruining your template & content.

 

About the Author

Phil Smulian
Phil Smulian likes to get his hands dirty when it comes to solving code problems. He also really loves making user experiences better than they were when he found them. Find him on Twitter @Bowsie

6 Comments

  1. Webbro Web Designs Says :
    Posted on September 15, 2016 at 3:19 pm

    There is actually another way to accomplish this.

    Minify the whole html email so there is no space and contact form 7 cannot add any or tags to the email and this solves it.

    Thanks

    • Webbro Web Designs Says :
      Posted on September 15, 2016 at 3:20 pm

      That was p or br tags

      • MediatorrM
        Posted on September 15, 2016 at 3:48 pm

        Thanks for the alternative solution Danny. That would work too I reckon.

  2. Mathias
    Posted on March 8, 2016 at 1:56 pm

    I recieve a server error by pasting your suggestion in the mail.php file… ?

    • MediatorrM
      Posted on September 15, 2016 at 3:51 pm

      Sorry I didn’t answer you sooner Mathias, I’m sure you solved that issue by now. I see that when pasting the text from here, I get formatted quote marks (my website styling’s fault). The fix would be to replace quote marks with plain text quotes.

  3. Nguyen Duy Linh
    Posted on November 26, 2015 at 3:51 pm

    You just need to comment the line: $body = wpautop( $body ); in mail.php

Leave a reply

Your email address will not be published. Required fields are marked *