Rakib
Rakib

Reputation: 13085

CakePHP renders HTML email with <p> tags at every new line

Here is the cake code..... notice the $email_body

<?php
$this->Email->reset();
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = array(
    'port'=>'465', 
    'timeout'=>'30',
    'host' => 'ssl://smtp.gmail.com',
    'username'=>'[email protected]',
    'password'=>'a_password_you_cant_see',
);
$this->Email->sendAs = 'html';
$this->Email->template = 'default';
$this->Email->from = '"NO-REPLY" <[email protected]>';
$this->Email->to = "[email protected]";
$this->Email->subject = "test PHP html email";
$email_body = "Hello message body
                <hr />
                This is rakib
                <br />
                <table width=\"100%\" bgcolor=\"#ff0\">
                    <tr>
                        <td>
                            In a table
                        </td>
                    </tr>
                </table>";
$this->Email->send($email_body);
?>

After sending this email out, when I view the Original mail contents via the Show Original button from GMail's drop down menu [at top-right corner of an email], here is what the HTML looks like:

<!-- Starting to render - email/html/default -->
<p> Hello message body</p>
<p>                         <hr /></p>
<p>                         This is rakib</p>
<p>                         <br /></p>
<p>                         <table width="100%" bgcolor="#ff0"></p>
<p>                             <tr></p>
<p>                                 <td></p>
<p>                                     In a table</p>
<p>                                 </td></p>
<p>                             </tr></p>
<p>                         </table></p>
<p> </p>
<!-- Finished - email/html/default -->

<p> and </p> tags got included at EVERY new line..... why is that? Using CakePHP 1.3

Upvotes: 0

Views: 2501

Answers (3)

Rakib
Rakib

Reputation: 13085

I found the solution to this... I needed to create my own default.ctp file in my app folder at app/views/elements/email/html/default.ctp and put the following in the file

<?php
echo $content;

That solved my problem. Please look below for the analysis of this problem.

[P.S. I did the same thing for app/views/elements/email/text/default.ctp for text based mails to print correctly]


The problem was that, since i didn't declare any default.ctp element in my own app folder, cake was falling back to the view element in its own core lib folder at cake/libs/view/elements/email/html/default.ctp . Over there, this is what it does.

<?php
$content = explode("\n", $content);
foreach ($content as $line):
    echo '<p> ' . $line . "</p>\n";
endforeach;
?>

That's where the <p> tags were coming from at every new line. Thanks to @thaJeztah's answer. In order to avoid this from happening, i needed to create my own default.ctp element in my app folder that would eventually override the default.ctp element in the core's lib folder.

That's one bad case scenario.

Upvotes: 1

thaJeztah
thaJeztah

Reputation: 29027

If you're directly setting the body of the email, CakePHP expects the text passed to be plain text.

By setting the type of email to HTML (Email->sendAs = 'html'), CakePHP will create a HTML version of your plain-text body by converting new-lines to <p> tags

In your case, you pass HTML as message body, but CakePHP assumes it is plain-text, therefore converts new lines to <p> tags as well

Read the documentation here: Sending a basic message

note

Although this should explain your question, please look at the answer that Sam provided as that will give you an answer on how you should send an HTML email!

Upvotes: 2

Sam Delaney
Sam Delaney

Reputation: 1305

When doing anything with emails, try and stick to the MVC principles. By writing your email's HTML (view) in what is likely to be a controller can make things a little messy and tends to bloat your code (e.g. composing markup in your controller).

Use templates to author the structure of your email and then use view variables ($this->set(...)) to apply specific values to it (See documentation).

I'm afraid I don't know why <p> tags are being inserted but I suspect it has got something to do with the newline character \n which is implicitly inserted each time you hit the return key.

In summary, move your markup to the template and everything should be better.

Upvotes: 2

Related Questions