colt
(Linija)
June 28, 2010, 5:43pm
1
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.
cyberpol
(Cyberpol 777)
June 28, 2010, 5:48pm
2
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
*/
outrage
(Site Sense Web)
June 28, 2010, 6:22pm
3
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.
Y11
(Y!!)
June 28, 2010, 6:26pm
4
You can use PHP_EOL instead which is the correct new line character for either win (\r\n) or linux (\n).
outrage
(Site Sense Web)
June 28, 2010, 6:39pm
5
That’s good to know, thanks Y!!
colt
(Linija)
June 28, 2010, 7:29pm
6
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
samdark
(Alexander Makarov)
June 28, 2010, 8:04pm
7
No, it’s not possible since email clients are not able to use it.
colt
(Linija)
June 29, 2010, 10:26am
8
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
outrage
(Site Sense Web)
June 30, 2010, 9:53pm
9
CoLT:
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.
colt
(Linija)
July 1, 2010, 11:29am
11
Thanks all of you for response Really important to keep it simple as there are various email clients
Greetings,
CoLT