0

So I can't seem to word the question right - but I would like to know how would you go about changing the way this

$Body = "";
$Body .=   "Name: ";  
$Body .= $Name;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

looks when it reaches my e-mail. When I receive the e-mail (using it as a contact form), I get this ->

Name: lloan
City: SomeCity
Email: some@email.com
Message: Testing

I tried <strong> $Body .= "Name: ";</strong> and that didn't work - I also tried it as $Body .= "<strong> Name: </strong>"; and again, nothing. Can anyone point me in the right direction? I really want to learn how to do this - I've googled it a couple of times, but I think it's the way I'm wording my question that's not helping me.

EDIT: So I'm able to do this now - $Body .= "<strong>Name: </strong>"; using the suggestions below of adding the headers and then adding that to the mail() function - however, how do I go about using it on a bigger scale? For example - What if I want to have a colored background or what not - Am I also able to use CSS in this kind of situation? The other questions on SO do not answer this in it's entirety.

lloan
  • 1,383
  • 8
  • 23
  • 3
    If you want to sent HTML email, you have to put `Content-type: text/html` in the header. – Barmar Jul 03 '13 at 23:21
  • possible duplicate of [Php mail: how to send html?](http://stackoverflow.com/questions/4897215/php-mail-how-to-send-html) – mario Jul 03 '13 at 23:23
  • http://fil.ya1.ru/PHP_5_in_Practice/index.htm#page=0768667437/ch16lev1sec1.html – Musa Jul 03 '13 at 23:24

3 Answers3

1

In the mail function add a parameter headers like this :

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

and call the mail function like this :

mail($to, $subject, $Body, $headers)

If you would like to format your message (the content of variable $Body), you are free to use as much HTML as you want.

1

Here's how:

$to = 'yourmailtarget@example.com';

$subject = 'Your email subject';

$headers = "From: abc@abc.com \r\n";
$headers .= "Reply-To: no-reply@abc.com \r\n";
$headers .= "CC: abc@abc.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = 'Put your html message here<br>';
$message .= "<b>HTML feature is now enabled</b><br>";
$message .= "<table border="1"><tr><td>Hello</td><td>World</td></tr></table>";
$message .= "For the styling, you can use inline styling:<Br>";
$message .= "<span style='color:#FF0000;'>This is red color</span>";
mail($to,$subject,$message,$headers,$parameters);

The most important part to answer your question is this line:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

That tells the system to produce the email using the HTML format.

Hope this helps

Jono20201
  • 3,215
  • 3
  • 20
  • 33
Ron
  • 514
  • 2
  • 13
  • How exactly could I use HTML after doing the above (which works btw) so that I could alter stuff like background-color and so on - At the moment, it only lets me use the `` tag :(! – lloan Jul 04 '13 at 00:43
  • @Alas: For the styling, you can use inline styling. I have updated my sample above with inline styling adding red color to one of the line – Ron Jul 04 '13 at 01:16
  • 1
    @RyonnSan why do you use `ISO-8859-1` and not `utf-8`? – vladkras Jul 04 '13 at 01:17
  • @vladkras: either way should be fine, but since majority of people is using utf-8, I update my code to using utf-8 – Ron Jul 04 '13 at 10:28
0

If you want to send HTML email you also need to send a text-only version as not all mail clients support HTML, and some mail systems specifically strip HTML versions.

This means you need a Content-type:multipart header, and multiple sections with their own headers, each delimited by a specific boundary. The actual format is quote complex, so use a library like swiftmail or PHPMailer