Sending email (html)


Why I can’t use ‘\n’ new line in email? Also I can’t get HTML work.

I’m trying to send simple mail with this code:


				mail(Yii::app()->params['adminEmail'],'test subject',$body,$headers);

				Yii::app()->user->setFlash('reminder','reminder sent.');


For simple $body I use:

$body='sample text\n\n';


$body='\n\nsample text2';

For HTML $body I use:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" xml:lang="en" lang="en">





$body='sample text<br/ ><br/ >';


$body='<br/ ><br/ >sample text2';




I’m doing this in View as for testing purpose.

Thanks for help!

Best regards,

Sarunas D.

About \n:

Single cuotes will print the characters \ and n.

Double cuotes will print the \n special character.

echo 'Something\nOther thing';

/* Will print this:

Something\nOther thing



echo "Something\nOther thing";

/* Will print this:


Other thing


In order to get HTML email to work you should set the content type with headers like this:

// To send HTML mail, the Content-type header must be set

$headers  = 'MIME-Version: 1.0' . "\r\n";

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

$headers .= ->your other headers<-

Also I have found on some platforms I need to remove the "\r" - for example, replace all occurrences of "\r\n" with "\n".

Good luck.

You can use PHP_EOL instead which is the correct new line character for either win (\r\n) or linux (\n).

That’s good to know, thanks Y!!

Thanks for quick and direct answers!!!

I’ve fixed all with your help ;))

Now how do you send fancy emails? It supports inline CSS but I can’t get internal styles work :) or is it even possible to use external CSS file?

Thanks again! :)


No, it’s not possible since email clients are not able to use it.

Yeah, I see… :)

It’s a bit OT, but how to properly prepare nice looking email letters without cascade formatting?

What is your best practice guys?



Just use inline styles like:

<div style="width:100px; border:1px solid #666;" >

    <p style="text-align: center;" >My paragraph text...</p>


Most email clients will accept basic css. Just keep it simple :)


In fact, most email clients will accept external css or images, the only problem is, you may have to choose to allow this from your email client which by default is blocked, therefore not a good idea.

Thanks all of you for response ;) Really important to keep it simple as there are various email clients :)