?
Solved

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

Posted on 2008-11-06
10
Medium Priority
?
517 Views
Last Modified: 2012-06-27
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!!

0
Comment
Question by:lilyyan
  • 4
  • 3
  • 3
10 Comments
 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 1000 total points
ID: 22899809
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
 
LVL 17

Accepted Solution

by:
psimation earned 1000 total points
ID: 22899843
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
 
LVL 17

Assisted Solution

by:psimation
psimation earned 1000 total points
ID: 22899879
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 1000 total points
ID: 22899894
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
 

Author Comment

by:lilyyan
ID: 22900150
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
 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 1000 total points
ID: 22900366
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
 
LVL 17

Assisted Solution

by:psimation
psimation earned 1000 total points
ID: 22902182
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
 

Author Comment

by:lilyyan
ID: 22907981
thanks very much for all replies and helps !!
0
 

Author Comment

by:lilyyan
ID: 22949380
A Special Appreciation for @nanharbison  ! i used PHPMailer : )
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 22955279
PHPMailer is awesome, isn't it? I use it all the time!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 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