format form responses when emailing via php

Posted on 2007-10-14
Last Modified: 2007-10-14
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.

Question by:robyndorn
    LVL 70

    Expert Comment

    by:Jason C. Levine
    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.
    LVL 1

    Author Comment

    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  = '' . ', '; // note the comma
    $to .= '';

    // 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 <>, Kelly <>' . "\r\n";
    $headers .= 'From: Birthday Reminder <>' . "\r\n";
    $headers .= 'Cc:' . "\r\n";
    $headers .= 'Bcc:' . "\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.
    LVL 70

    Accepted Solution

    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 = '';
    LVL 1

    Author Comment

    you're awesome! :)
    LVL 70

    Expert Comment

    by:Jason C. Levine
    Finally, someone who appreciates me.

    Can I pay you to talk to my wife? :)

    Featured Post

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    Join & Write a Comment

    Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now