PDA

View Full Version : formatting php email form



jonnyfreak
07-11-2013, 11:54 AM
i have the following form script


//email to customer
$to = $EmailAddress;
$subject = 'Successful Order from Webshop';
$message = 'Thank you for your order '. $CustomerName .
'Your order is on its way';
$headers = 'From: webmaster@shop.com' . "\r\n" .
'Reply-To: webmaster@shop.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

i want to format it with some html. how is the best way to this. It is actually just the $message that i need to add the html to

thanks in advance

traq
07-11-2013, 03:22 PM
1) Write the HTML you wish to send. Keep in mind that email clients often have poor HTML support (so, stick to the basic tags, use tables for layout, keep all CSS styling inline, assume javascript is not available at all), and that many email clients (rightly!) block images by default.

2) Add content-type and mime headers to your email:
$headers = "From: webmaster@shop.com\r\n" .
"Reply-To: webmaster@shop.com\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"Mime-version: 1.0" .
"Content-type: text/html; charset=UTF-8\r\n"; // and make sure you're _actually_using_ this charset

letom
07-11-2013, 07:41 PM
Please refer this for sending both HTML and TEXT mails through PHP (http://cyberexploration.blogspot.in/2013/06/sending-html-and-plain-text-mails-in-php.html)
if any support required, don't hesitate to ask.

jonnyfreak
07-15-2013, 10:22 AM
1) Write the HTML you wish to send. Keep in mind that email clients often have poor HTML support (so, stick to the basic tags, use tables for layout, keep all CSS styling inline, assume javascript is not available at all), and that many email clients (rightly!) block images by default.

2) Add content-type and mime headers to your email:
$headers = "From: webmaster@shop.com\r\n" .
"Reply-To: webmaster@shop.com\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"Mime-version: 1.0" .
"Content-type: text/html; charset=UTF-8\r\n"; // and make sure you're _actually_using_ this charset


Hello I tried your suggestion


//email to customer
$to = $EmailAddress;
$subject = 'Successful Order from webshop';
$message = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example: ' . $OrderID . '
</body>
</html>';
$headers = 'From: shop@webshop.com' . "\r\n" .
'Reply-To: noreply@webshop.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
"Mime-version: 1.0" .
"Content-type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);

but that literally just emailed


<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like Variable display Example: 1155 </body> </html>

traq
07-15-2013, 04:00 PM
Hello I tried your suggestion
but that literally just emailed

<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like Variable display Example: 1155 </body> </html>


I missed a couple newlines between the headers.


//email to customer
$to = $EmailAddress;
$subject = 'Successful Order from webshop';
$message = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example: ' . $OrderID . '
</body>
</html>';
$headers = 'From: shop@webshop.com' . "\r\n" .
'Reply-To: noreply@webshop.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion(). "\r\n";
"Mime-version: 1.0\r\n" .
"Content-type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);

jonnyfreak
07-15-2013, 05:09 PM
I missed a couple newlines between the headers.


//email to customer
$to = $EmailAddress;
$subject = 'Successful Order from webshop';
$message = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example: ' . $OrderID . '
</body>
</html>';
$headers = 'From: shop@webshop.com' . "\r\n" .
'Reply-To: noreply@webshop.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion(). "\r\n";
"Mime-version: 1.0\r\n" .
"Content-type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);


thanks for your help so far, i am still having an issue echoing the results out. i modified your code earlier as was looking on the php.net site but i think the issue is the variable and where the speech marks etc go


// multiple recipients
$to = $row_rsEvent['BillingEmail'] . ', '; // note the comma
$to .= 'email@domain.com';

// subject
$subject = 'Your Order Confirmation ';

// message
$message = "
<html>
<head>
<title>Your Order has been successful</title>
</head>
<body>
<img src='http://www.webshop.com/images/email-logo.jpg'/>
<p>Thank you for your order, the order details are below,</p>
<table width='600' border='1' cellpadding='10'>
<tr>
<td><p>Order ID</p></td>
<td><p>".$row_rsEvent['OrderID']."</p></td>
</tr>
<tr>
<td>Total Amount</td>
<td>".$row_rsEvent['Total']."</td>
</tr>
<tr>
<td>Description Primary Order</td>
<td></td>
<td>".$row_rsEvent['event_desc']."</td>
</tr>
</table>
<p>you will be contacted by us shortly</p>
<p>thanks</p>
<p>webshop</p>
<p>name</p>
</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: Jakes <jake@domain.com>, dan <dan@domain>' . "\r\n";
$headers .= 'From: <shop@webshop.com>' . "\r\n";

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

traq
07-15-2013, 06:51 PM
thanks for your help so far, i am still having an issue echoing the results out.
You don't try to echo out anything in the example you posted. Do you mean you're still having problems sending the email? Can you describe the specific problems you're running in to (i.e., what you expect vs. what actually happens)?


i modified your code earlier
What modifications were you trying to make?


i think the issue is the variable and where the speech marks etc go
Which variable? Also, try to describe what problems it is causing and why (or why you think so).

By "speech marks," I assume you mean the quotes (double ( " ) and single ( ' )), correct? They all seem to be in the right places.

The only problem that jumps out at me is that you try to set two recipients in the $to param, and then try to set another two recipients manually in the $headers. I don't exactly know what would happen, there, but I would expect some sort of problem (one overwriting the other, at least).

Try this:
<?php

/* ...snip... */

// change this:
// mail($to, $subject, $message, $headers);

// to this:
if( mail($to, $subject, $message, $headers) ){
print "mail sent.";
}
else{
print "mail failed.";
}let me know what happens.

The other possibility is your $row_rsEvent - you don't show any code where you assign that variable; not knowing what values it holds makes it hard to say if there might be any problems or not. Another thing to consider is that, if any of it comes from user input, you need to validate/ sanitize it before using t in your email headers (to prevent a header injection attack).