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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nanharbisonCommented:
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
psimationCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
psimationCommented:
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

nanharbisonCommented:
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
nanharbisonCommented:
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
psimationCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.