Sending email (html)

Hello,

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:


				$headers="From: test@mail.com\r\nReply-To: test@mail.com";

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

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

				$this->refresh();

For simple $body I use:


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

$body.=$somestring;

$body='\n\nsample text2';

For HTML $body I use:




$body='

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

	<title>Emailas</title>

</head>


<body>';


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

$body.=$somestring;

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


$body.='

</body>

</html>';



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

*/



While




echo "Something\nOther thing";

/* Will print this:

Something

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! :)

CoLT

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?

Greetings,

CoLT

Just use inline styles like:


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

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

</div>

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

Edit:

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 :)

Greetings,

CoLT