• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

format form responses when emailing via php

I have a form which emails user inputs via php. I'd like to be able to format those responses considerably using html so that the email isn't just a bland list of responses.

Could anyone point me to a relevant tutorial or an example that I could back engineer to see how this is done.

  • 3
  • 2
1 Solution
Jason C. LevineNo oneCommented:
Hi Robyn,

Look at the following:


and Example 1117. Sending HTML email

on the mail() manual page:


Essentially, all you need to do is send an extra couple of headers to the mail() function so the clients know to render the HTML as HTML.
robyndornAuthor Commented:
Thanks for the fast response jason1178. And I had actually been through that, being new to all this however I still think I'm missing something. I can see how to include html in an email when the html is included in the php script. But I'm missing how to include within that html 'user responses'.

I guess I'm looking at this one in particular:

// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
  <title>Birthday Reminders for August</title>
  <p>Here are the birthdays upcoming in August!</p>

// 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";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

the html section includes no user responses. I'm sure I'm missing something blatantly obvious.
Jason C. LevineNo oneCommented:
Not blatantly obvious, but not that hard once you see it:

$message = "<html><head><title>A message</title></head><body><p>$_POST['message']</p></body></html>";

Just add the PHP code for your posted form variables to the HTML and make sure to use double-quotes so the PHP code gets evaluated.  In other words this:

$message = "";

not this:

$message = '';
robyndornAuthor Commented:
you're awesome! :)
Jason C. LevineNo oneCommented:
Finally, someone who appreciates me.

Can I pay you to talk to my wife? :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now