Jump to content

display email as html email


stealthmode666

Recommended Posts

I have a form which I process by php and send to an email on submit.

I want the email to diplay as HTML.(email client has no problem receiving and displaying html, I know about this part)

 

At present I have the fields displayed as an array in the email.

I have read how to tell the file to display as html but in the tutrial I'm not sure where to put the code in my script. Also once I do this, how can I have one of the fields of data I process to show in the email as formated html?

$to = ( isset ($_REQUEST['sendto']) ? $_REQUEST['sendto'] : "default 'to' email goes here" ); 
$from = ( isset ($_REQUEST['Email']) ? $_REQUEST['Email'] : "default 'from' email goes here" ) ; 
$name = ( isset ($_REQUEST['Member_Name']) ? $_REQUEST['Member_Name'] : "Default member name goes here" ) ; 
$headers = "From: $from"; 
$subject = "Members ......";

$fields = array(); 
$fields{"Member_Name"} = "Members Name"
$fields......

$body = ".........:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: ...@.."; 
$subject2 = "Thank-you..."; 
$autoreply = "Somebody ....Thank-you";
if($from == '') {print "....";} 
else { 
if($name == '') {print "....";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send){header( "Location: http://www./../thankyou.html" );} 
else 
{print "......"; } 
}
}
?>

 

The code, tutorials and books I'm looking at show how to send an html email but how do I include it in this script above?

 

<?php
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

The above tutorial just sends "Hello World" as a H2 formatted header.

 

So I'm asking how to include some of this code to my above script and take my fields in my array display in the email as formatted html?

I know this is two questions in one post.

Thanks for any guidance

Link to comment
Share on other sites

Thanks.

is this how to use it?

<?php
$to = ( isset ($_REQUEST['sendto']) ? $_REQUEST['sendto'] : "default 'to' email goes here" ); 
$from = ( isset ($_REQUEST['Email']) ? $_REQUEST['Email'] : "default 'from' email goes here" ) ; 
$name = ( isset ($_REQUEST['Member_Name']) ? $_REQUEST['Member_Name'] : "Default member name goes here" ) ; 
$headers = "From: $from"; 
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$subject = "......"; 

 

I notice you use a different charset? will this matter?

Also if this is how to do the first part of my question, how can I display a field value in my array as html in the email?

Link to comment
Share on other sites

yes, that is the correct way. I have it as utf-8 because everyone can handle in pretty much...

 

As for the code help, this appears to be a third-party application which is a different board if you want help with it.

Else, you can re-write it and I'll help you with that.

Link to comment
Share on other sites

Thank-you for your help, yes, I would like to re-write my script that will now allow me to display the fields I have in an array as formatted text.

I will use a table with TD and TR and possibly use css.

What I am aiming to do is have the form page that is displayed in html almost re-displayed in the email when the form is processed.

I need this so I can print the form as it appears on the web as filled by the user.

 

Still learning MySQL, after xmas before I begin to insert the form data into a MySQL table which I can then use to extract different parts.

Link to comment
Share on other sites

So what you want to do is have a table with inputs that you submit and they can be redisplayed in pretty much the same format on the table?

I'd start with the form. Build your table, add the text fields and inputs in the correct places and get your form ready to submit using method "POST". When you have your form finished, post here again with your HTML for the form so that I can be on the same page with you.

Link to comment
Share on other sites

I already have the form working which has about 100 fields and use .js to validate it on the server so my .php script doesn't need to validate, all it has to do is send the form fields.

What I'm thinking is, do i use the same table and td/tr's in my process script that I use on the web. Then somehow extract the form field data and insert it via my .php script?

 

Link to comment
Share on other sites

it's over 100 fields :)

It's a loan application form with joint application if needed. So i need to reconstruct the form in php that will take the data fields I have in an array and put the $fields into the reconstructed form.

A simple way to show me how to do one or two $fields that will display as html will get me going. very good with xhtml, css so I'll manage to work through putting in each field

Link to comment
Share on other sites

//Mail Body
$Body  = "Your Application was submitted";
$Body .= "<table><tr>";
$Body .= "<td width=\"200px\">Name</td><td width=\"400px\"> $_POST['Name'] </td>";
$Body .= "</tr><tr>";
$Body .= "<td>Age</td><td> $_POST['Age'] </td>";
$Body .= "</tr></table>";
//And so on

I don't know if you can use CSS in email. give it a try if you'd like, but my guess would be no css.

 

Do you already know how $_POST works?

Link to comment
Share on other sites

 

I don't know if you can use CSS in email. give it a try if you'd like, but my guess would be no css.

 

Do you already know how $_POST works?

 

You can use limited CSS in email, but it has to be inline, and you pretty much need to stay away from structural elements (position:absolute and whatnot).  With structure you are pretty much limited to tables if you want to be safe.

Link to comment
Share on other sites

Use Post, other wise your Query String will get massive which is sometimes very bad.

Yes, basically, you will need to write your entire HTML again just instead of inputs, you will have the values they submitted. Is emailing them their own form really necessary? Could yourself time and just send them an email that they were successful.

Link to comment
Share on other sites

I need to print the form page as filled-in by the member to keep in file which has also to be kept as paper copy away from web for legal reasons.

I will work through the coding, don't mind how long it takes, good practice :)

I gave the member the option to fill-in the form on-line and also created a .pdf version which they can download/print and send.

My php is poor, but every day i'm learning more.

Link to comment
Share on other sites

I tried it out but got in a mess.

This is the error I got.

 

PHP Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\W....\.....co.uk\w...\r.....\form.php on line 183

 

This is line 183 in my script.

$Body .= "<td width=\"200px\">Members Name</td><td width=\"400px\"> $_POST['Member_Name']</td>";

 

I commented out all the $fields in the array.

 

Will I post the script I used?

Link to comment
Share on other sites

This is my script.

Lokking to make it so the form fields are displayed in html on the email the script sends to me instead of a list of >100 fields.

You will see I have commented out the array fields.

<?php
$to = ( isset ($_REQUEST['sendto']) ? $_REQUEST['sendto'] : "default 'to' email goes here" ); 
$from = ( isset ($_REQUEST['Email']) ? $_REQUEST['Email'] : "default 'from' email goes here" ) ; 
$name = ( isset ($_REQUEST['Member_Name']) ? $_REQUEST['Member_Name'] : "Default member name goes here" ) ; 
$headers = "From: $from"; 
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$subject = "Members Data From .....";
// fields taken from form. validation on form is done by .js server-side script

still part of the same script

#$fields = array(); 
#$fields{"Member_Name"} = "Members Name";
#$fields{"telephone"} = "Members Telephone/Contact Number"; 
#$fields{"Loan_Amount"} = "Loan_Amount";
#$fields{"Email"} = "Members Email";
#$fields{"title"} = "Main Applicants Title";
#$fields{"surname"} = "Applicants Surname"; 
#$fields{"forename"} = "Forename(s)";
#$fields{"residential"} = "Residential Address";
#$fields{"town"} = "Town";
#$fields{"county"} = "County";
#$fields{"postcode"} = "Postcode";

 

still same script from above.

#new code to insert table and data
$Body  = "Your Application was submitted";
$Body .= "<table><tr>";
$Body .= "<td width=\"200px\">Members Name</td><td width=\"400px\"> $_POST['Member_Name']</td>";
$Body .= "</tr><tr>";
$Body .= "<td>Members Telephone Number</td><td> $_POST['telephone']</td>";
$Body .= "</tr></table>";

/*$body = "Robert. You have received .....:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } */

 


$headers2 = "From: me@someone.co.uk"; 
$subject2 = "Thank-you for filling in the Form ..."; 
$autoreply = "Somebody from .....";

if($from == '') {print "You have not entered an Email Address, please go back and try again";} 
else { 
if($name == '') {print "You have not entered your Members Name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{header( "Location: http://www.../r.../thankyou.html" );} 
else 
{print "We encountered an error ..."; } 
}
}
?> 

 

As it looks, I feel I have not got the right way to insert a table with php to display the form fields.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.