form data send to an email address through php mail() function

hello:

i hve a form when user click the submit button, the form information will be put in the message body of the a email.
I want to format the sending data into a html table and then put it into email message
my question is:

will the table remain its shape when the receiver receive the email ? namely does the email "understand" the html tags??

thanks so much for your replies!!

lilyyanAsked:
Who is Participating?
 
psimationConnect With a Mentor Commented:
That depends on the headers you set.

By default, mail() will only send as plain text.

Having said that - I suggest you use the following mailer class - since it's :

a) better to use than mail() - if you use it with properly setup smtp server with authentication
b) will allow you to send HTML "as is" by simply toggling some variables ( of which there are plenty of examples in the documentation.
c) You can do much more with it than simply using mail() - like embedding images, adding attachments, having alternative text-only body for e-mail clients that cannot render HTML

Go here to download the package:

http://phpmailer.sourceforge.net                               
0
 
nanharbisonConnect With a Mentor Commented:
no, the php mail function does not recognize html. You need the PHPMailer class on sourceforge:
http://sourceforge.net/projects/phpmailer

Then you can use html to format the email. But the html you use for email is like html from years ago, for exampple, you have to use font tags, if you remember pre-CSS formatting!

0
 
psimationConnect With a Mentor Commented:
PS, if you are adament in using mail(), then you can simply set additional headers like so:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
nanharbisonConnect With a Mentor Commented:
Here is an email function I wrote which sends form data using the PHPMailer class, and the call to the function made on the page that handles the form

include_once('PHPMailer/class.phpmailer.php');
 
function Sendform($name, $address, $city, $state, $zip, $country, $homephone, $email, $fax)) {
	$mail = new PHPMailer();
	$body = '<html>
<body>
<table border="0" cellpadding ="5">
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Name</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$name.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Street Address</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$address.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">City</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$city.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">State</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$state.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Zipcode</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$zip.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Country</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$country.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Home phone</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$homephone.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Fax</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$fax.'</font></td>
</tr>
<tr>
	<td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">Email Address</font></td><td><font face="Arial, Helvetica, sans-serif" size="2" color="#000000">'.$email.'</font></td>
</tr>
</table>
</body>
</html>';
 
	$mail->From     = $email;
	$mail->FromName = $name;
	$mail->Subject = "Request From Website";
 
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
 
	$mail->MsgHTML($body);
	$mail->IsHTML(true);  
	$mail->AddAddress("person_one@website.com", "John Doe");
	$mail->AddAddress("person_two@website.com", "Jane Doe");
	
	//$mail->AddAddress($email, $name);
 
	$mail->Send();
}
 
//the call to the function
Sendform($_POST['name'], $_POST['address'], $_POST['city'], $_POST['state'], $_POST['zip'], $_POST['country'], $_POST['telephone'], $_POST['email'], $_POST['fax']);

Open in new window

0
 
lilyyanAuthor Commented:
thanks for all replies:

question for: psimation : )
>>Go here to download the package:
http://phpmailer.sourceforge.net

1. i don't maintain the php web server, guess i don't have permission to intall the phpmailer?
2.  $headers  = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

this will make the mail function undstand the html tags ??

question for nanharbison : )
do i need  install PHPMailer, i'm using php5.0


0
 
nanharbisonConnect With a Mentor Commented:
No, you don't have to install this PHP code, psimation and I are recommending the same thing, PHPMailer. You only have to upload it to your website. Do you have ftp access?
0
 
psimationConnect With a Mentor Commented:
lilyyan

As nanharbison said, we are recommending the same "solution" wrt PHPMailer - the only "slight" difference is - I am additionally recommending that you use the phpmailer with a properly authenticated smtp server so that your e-mail get stamped with the proper headers and will thus fair much better against spam traps. Remember, you are already trying to send HTML e-mail here - that is already going to give it *some* score on most spam trappers to begin with...

And no, you don't need to "install" it, you simply upload the two classes and your one code file to your webfolder.

Then, wrt the mail() headers - here is an example from the php manual showing how to add more headers and specifically the Content-Type headers.

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

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

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// 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);
?>
0
 
lilyyanAuthor Commented:
thanks very much for all replies and helps !!
0
 
lilyyanAuthor Commented:
A Special Appreciation for @nanharbison  ! i used PHPMailer : )
0
 
nanharbisonCommented:
PHPMailer is awesome, isn't it? I use it all the time!
0
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.

All Courses

From novice to tech pro — start learning today.