We help IT Professionals succeed at work.

html to email

rgb192
rgb192 used Ask the Experts™
on
add html to email

want to add
<br>

\n
\t
&nbsp;

will the output show <br> instead of linebreak
with different email clients
<?php
 
$Name = "Da Duder"; //senders name
$email = "email@adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
 
mail($recipient, $subject, $mail_body, $header); //mail command :)
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
That will show up as a return in the email, the \r\n will work for Linux or Windows

Author

Commented:
when i add

<br>

\n
\t
&nbsp;

to the mail body

sometimes is not a line break

is there code to allow html in mail()
Greg AlexanderLead Developer

Commented:
Add some additional headers
<?php
 
$Name = "Da Duder"; //senders name
$email = "email@adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
$mail_body = "The text for the mail...<br><b>with html</b>"; //mail body
$subject = "Subject for reviever"; //subject
 
$header = "From: \"".$from_name."\" <".$from_email.">\n"; 
$header .= "To: \"".$to_name."\" <".$to_email.">\n"; 
$header .= "Return-Path: <".$from_email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "Content-Type: text/html; charset=ISO-8859-1\n"; 

mail($recipient, $subject, $mail_body, $header); //mail command :)
?>

Open in new window

Lead Developer
Commented:
Forgive me, use this instead... linked to your vars
<?php
 
$Name = "Da Duder"; //senders name
$email = "email@adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
$mail_body = "The text for the mail...<br><b>with html</b>"; //mail body
$subject = "Subject for reviever"; //subject
 
$header = "From: \"".$email."\" <".$$email.">\n"; 
$header .= "To: \"".$recipient."\" <".$recipient.">\n"; 
$header .= "Return-Path: <".$email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "Content-Type: text/html; charset=ISO-8859-1\n"; 

mail($recipient, $subject, $mail_body, $header); //mail command :)
?>

Open in new window

if you add additional headers, it will include the HTML display.

$header = "From: \"".$from_name."\" <".$from_email.">\n";
$header .= "To: \"".$to_name."\" <".$to_email.">\n";
$header .= "Return-Path: <".$from_email.">\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\n";
Greg AlexanderLead Developer

Commented:
That's exactly the solution I made!?

Author

Commented:
will this allow both
<br>

and
\n

in the
$mail_body
Greg AlexanderLead Developer

Commented:
If you use html headers, it will transfer as html only, so the answer is no, it will not allow both.

Author

Commented:
thanks