Search code examples
phpemailphpmailerhtml-email

POST HTML (E-Mail Template) trough PHP and send formatted Mail not working


My idea is simple the user should have an Option to send mails trough the website. I use tinyMCE as an editor on my site so the user can design his own style of the mail (HTML Text).

When I hit "Send" the mail that I get is either plain HTML Code or I get weird \n or \r and not an formatted HTML Mail.

This is my HTML part:

<form method="post">
      <textarea name='body' id='mail' rows='25' cols='100'></textarea>
</form>

This is my PHP part:

 $headers = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

  $mmail->isHTML(true);
  $mmail->addCustomHeader($headers);
  $mmail->AddAddress($address, $address);
  $mmail->Subject = "test";
  $mmail->Body = $body;
  $mmail->Send();

I've added the headers that you are supposed to add but it didn't work.

I've tryed with all of these examples to send an nice formated HTML E-mail:

$body = stripslashes($_POST['body']);
$body = $_POST['body'];
$body = str_replace("\\r", "", str_replace("\\n", "\n", html_entity_decode($_POST['body'])));
$body = str_replace("\'", "'",$body);

but none of these worked for me.

Now my question is how can I send HTML trough my website to send an HTML formatted e-mail? I'm I missing some extra options to configure on my $mmail or I'm I doing something wrong or is there an setting to tweak on the PHP server itself?

Thank you in advaned.


Solution

  • The problem was PHP added backslashes \ to the double quotes on the HTML Post. So I used str_replace to remove them.

    Then I converted the content with htmlspecialchars_decode.

    Again I had a new Problem that in my Mail I had "rn" displayed in my content.

    I used str_replace again to remove those "rn".

    This worked fine, now my Mails are being displayed properly as nice styled HTML.

    PHP Code:

    $body = str_replace('\\', '', stripslashes($body));
    $body = htmlspecialchars_decode($body);
    $body = str_replace('rn', '', $body);
    

    Thanks for the suggestions @KenLee