Jump to content

Recommended Posts

I'm still struggling with my script.

I have a form that uses post to send the data on the form to my email.

I also have the script display a thank-you page on submit.

I want the email to display in html with a table.

At present I have all the $fields in an array().

The email looks messy as I have loads of fields. They are displayed in rows.

Can I somehow divide the email that is sent into some sort of order that makes it easier for me to see and print that looks the same as my form?

I got some help but not enough to get it to work.

I have put in the html headers in the script.

What I want is the script to take one of the form fields then re-build the form table and put the array field in the table to be send to me

Link to comment
https://forums.phpfreaks.com/topic/137349-solved-make-email-display-html/
Share on other sites

$headersĀ  = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\nTo: You <You@mail.com>\r\nFrom: the_user <user@email.com>\r\n";

$mail_sent = mail('youremail','test','testing',$headers);

if ($mail_sent) echo "it is working";
else echo "it is not working";

Thanks for the above, I already have the html headers in place in my script.

<?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 ......";

What I'm looking for is to take the $fields = array(); which are a lot and have them display as html in a table in the email.

$fields = array(); 
$fields{"Member_Name"} = "Members Name";
$fields{"telephone"} = "Members Contact Phone Number"; 
$fields{"Email"} = "Members Email Address";

This is the body I have in my script which grabs the fields in the array.

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

Ā 

Somehow I think I have to add the way the script sends the email here.

As in how can I have the email send my array fields that are in an html table/TD/TR instead of one long list from the array fields?

Ā 

Here is what I use, it works just fine, except for the From field, always shows the server address

$to = "test";
Ā  Ā  $from = "test";
Ā  Ā  $subject = "test Update";
$today = date("F j, Y h:i a");
//$updated = "Pick(s) have been Updated";
$user = $_SESSION['SESS_EMAIL'];

Ā  Ā  //begin of HTML message
Ā  Ā  $message = "
<style type=\"text/css\">
<!--
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #000000;
}

.mainT {
border: thin solid #990000;
}


-->
</style><html>
Ā  <body>
Ā  
<table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"mainT\">
Ā  <tr>
Ā  Ā  <td align=\"left\" valign=\"top\" bgcolor=\"#990000\"><div align=\"center\"><strong><h2>VEGASD SPORTS PICKS UPDATE</h2></strong></div></td>
Ā  </tr>
Ā  <tr>
Ā  Ā  <td valign=\"top\" align=\"left\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"6\">
Ā  Ā  Ā  <tr>
Ā  Ā  Ā  Ā  <td colspan=\"4\">The following \"picks\" information has been uploaded
to the picks packages database on vegasdsports.com</td>
Ā  Ā  Ā  </tr>
Ā  Ā  Ā  <tr>
Ā  Ā  Ā  Ā  <td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
Ā  Ā  Ā  Ā  Ā  <tr>
Ā  Ā  Ā  Ā  Ā  Ā  <td><strong>Date: </strong>{$today}</td>
Ā  Ā  Ā  Ā  Ā  Ā  <td>Ā </td>
Ā  Ā  Ā  Ā  Ā  </tr>
Ā  Ā  Ā  Ā  </table></td>
Ā  Ā  Ā  Ā  </tr>
Ā  Ā  Ā  <tr>
Ā  Ā  Ā  Ā  <td colspan=\"3\">
<strong>Sport:</strong> {$sport}<br>
<strong>Activation Date:</strong> {$active}<br /></td>
Ā  Ā  Ā  Ā  </tr>
Ā  Ā  Ā  <tr>
Ā  Ā  Ā  Ā  <td colspan=\"4\">
Ā  Ā  Ā  Ā  <strong>Single Pick:</strong><br />
Ā  Ā  Ā  Ā  {$sngl}<br /><br />
Ā  Ā  Ā  Ā  <strong>Monthly Pick:</strong><br />
Ā  Ā  Ā  Ā  {$mnth}<br /><br />
Ā  Ā  Ā  Ā  <strong>Season Pick:</strong><br />
Ā  Ā  Ā  Ā  {$ssn}<br /><br />
Ā  Ā  Ā  Ā  <strong>Lock Pick:</strong><br />
Ā  Ā  Ā  Ā  {$lck}<br /><br />
Ā  Ā  Ā  Ā  <strong>Special Package:</strong><br />
Ā  Ā  Ā  Ā  {$spl}<br /><br />
Ā  Ā  Ā  Ā  <strong>Special Pckg End Date:</strong><br />
Ā  Ā  Ā  Ā  {$$month_spl}<br /><br /></td>
Ā  Ā  Ā  </tr>
Ā  Ā  </table></td>
Ā  </tr>

</table>

</html>
Ā  </body>";
Ā   //end of message 
Ā  Ā  $headers = "Content-Type: text/html; charset=\"iso-8859-1\"";
Ā  Ā  $headers .= "Content-Transfer-Encoding: 7bit";
Ā  Ā  $headers .= "From: $from\r\n"; 

Ā  Ā  //options to send to cc+bcc
Ā  Ā  $headers .= 'Cc: test@test.com' . "\r\n";
$headers .= 'Bcc: test@test.com' . "\r\n";


mail($to, $subject, $message, $headers);Ā  Ā   
Ā  Ā  // now lets send the email.
Ā  Ā  //mail($to, $subject, $message, $headers);

As in how can I have the email send my array fields that are in an html table/TD/TR instead of one long list from the array fields?

Ā 

I was trying to show you an example of how I send HTML mail with using a table, like I believe you asked

This is the the help that someone gave me but I don't know how to impliment in my script, as when I tried it, it gives me an error?

It's using the $_POST to which I believe takes the array field and puts it in the table.

Ā 

//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

Ā 

Ā 

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

This is the body of my script but when I add the above it doesn't work.

Am I doing something silly that is easy to work-out?

Ā 

Ā 

This is the error I get.

Ā 

PHP Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\....\....co.uk\...\....\sform.php on line 172

Ā 

I have removed the http address and added ....'s so it doesn't show to the world.

Ā 

The line in my script at 172 is

line 170 $body = "Robert. You have received these contact details .com:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}

Line 171$Body .= "<table><tr>";

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

Line 173$Body .= "</tr><tr>";

Line 174$Body .= "<td>Members Contact Number</td><td> $_POST['telephone'] </td>";

Line 175$Body .= "</tr></table>";

Ā 

Thanks for any help on this. I have been over a week trying to make this happen and have bought and read all of Wrox Beginning PHP 5, to no avail. plus a couple of other php books but none show ecactly how to make an email do what I want. Trying so hard to understand but going round in circles with this script.

Also maybe I don't need all the code on line 170? I followed a book and web tutorial which showed how to send form fields to an email address with a header and an auto-reply to the email address of the user who fills in the field in the form.

A lot of views but is no-one able to show me how to make my script send my form fields as an html table instead of an array as plain text in an email?

Ā 

I have been to a lot of forums and I know people are good at php so please help me with this.

Had bits and pieces of help but nothing works as of yet.

<?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 = array();

$fields{"Member_Name"} = "Members Name";

$fields{"telephone"} = "Members Contact Phone Number";

$fields{"Email"} = "Members Email Address";

$fields{"Loan_Amount"} = "Loan_Amount Ā£";

$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";

$fields{"years"} = "Lived At Address (Years)";

$fields{"months"} = "Lived At Address (Months)";

$fields{"hometelephone"} = "Home Tel No";

$fields{"mobile"} = "Mobile Tel No";

$fields{"dateofbirth"} = "D.O.B";

$fields{"nationality"} = "Nationality";

$fields{"sex"} = "Sex";

$fields{"marital"} = "Marital Status";

Ā 

Ā 

$fields{"homeowner"} = "Home Owner";

$fields{"tenant"} = "Tenant";

$fields{"livingwithfamily"} = "Living With Family";

$fields{"residentialother"} = "Living Other - Specify";

$fields{"numberathome"} = "No of Household At Home";

$fields{"numberworking"} = "No of Household Working";

Ā 

Ā 

$fields{"employedfulltime"} = "Employed - Fulltime";

$fields{"soletrader"} = "SoleTrader";

$fields{"partner"} = "Partner";

$fields{"director"} = "Director";

Ā 

$fields{"occupation"} = "Occupation";

$fields{"employername"} = "Employers Name";

$fields{"employeraddress"} = "Employers Address";

$fields{"employertown"} = "Employers Town";

$fields{"employercounty"} = "Employers County";

$fields{"employerpostcode"} = "Employers Postcode";

$fields{"employeryears"} = "Years with Employer";

$fields{"employermonths"} = "Months with Employer";

$fields{"businesstelephone"} = "Employers Telephone Number";

$fields{"previousoccupation"} = "Previous Occupation";

$fields{"previousemployeryears"} = "Previous Employer Years";

$fields{"previousemployermonths"} = "previous Employer Months";

Ā 

$fields{"sortcode"} = "Bank Sort Code";

$fields{"bankaccountno"} = "Bank Account Number";

$fields{"bankname"} = "Bank Name";

$fields{"bankaddress"} = "Bank Address";

$fields{"banktown"} = "Bank Town";

$fields{"bankcounty"} = "Bank County";

$fields{"bankpostcode"} = "Bank Postcode";

Ā 

$fields{"bankyears"} = "How Long At Bank in Years?";

$fields{"bankmonths"} = "Months";

$fields{"chequecard"} = "Cheque Card";

$fields{"creditcard"} = "Credit Card";

$fields{"storecard"} = "Storecard";

Ā 

$fields{"directcredit"} = "Method of Payment/Direct Credit";

$fields{"bacstransfer"} = "BACS Transfer";

$fields{"directdebitmandate"} = "Direct Debit Mandate";

Ā 

$fields{"grossmonthlyincome"} = "Gross Monthly Income";

$fields{"regularincome"} = "Regular Overtime";

$fields{"spousegrossincome"} = "Spouse Gross Income";

$fields{"anyotherincome"} = "Any Other Income";

$fields{"totalgrossincome"} = "Total Gross Income";

Ā 

Ā 

$fields{"mortgagerent"} = "Mortage/Rent";

$fields{"counciltax"} = "Council Tax";

$fields{"loans"} = "Loans";

$fields{"creditcards"} = "Credit Cards";

$fields{"livingexpenses"} = "Living Expenses";

$fields{"totalexpenditure"} = "Total Expenditure";

Ā 

Ā 

$fields{"notes"} = "Notes and extra text to help with the case";

Ā 

// here I think is right in php to send an email to the email field collected from the email form field

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

$Body .= "<table><tr>";

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

$Body .= "</tr><tr>";

$Body .= "<td>Members Contact Number</td><td> $_POST['telephone'] </td>";

$Body .= "</tr></table>";

Ā 

$headers2 = "From: robert_macbeth@........co.uk";

$subject2 = "Thank-you for filling in the Form ";

$autoreply = "Somebody from...... will get back to you as soon as possible, Thank-you";

Ā 

if($from == '') {print "You have not entered an Email Address, please go back and try again";}

else {

if($name == '') {print "You have not entered a First 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.....co.uk/.../thankyou.html" );}

else

{print "We encountered an error ...@....co.uk"; }

}

}

?>

Ā 

I also don't need the if,else statements as all validation is done by .js on the server-side in my form. The form will not submit to this script unless it is filled in properly.

I tried removing them but got an error so I just left them although they do nothing.

Ā 

Premiso do you mean like this?

This is what I have:

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

Ā 

Is this what I should have?

$Body .= "<td width=\"200px\">Member Name</td><td width=\"400px\"> $_POST{'Member_Name'} </td>";

move your foreach loop inside the table and make a new row for each element

Ā 

$body = "Robert. You have received these contact details .com:<br /><br />"; 
$body .= "<table>";
foreach($fields as $a => $b)
{
Ā  $body .= "<tr><th>" . $a . "</th><td>" . $_REQUEST[$a] . "</td></tr>";
}
$Body .= "<tr>";
$Body .= "<td width=\"200px\">Member Name</td><td width=\"400px\"> $_POST['Member_Name'] </td>";
$Body .= "</tr><tr>";
$Body .= "<td>Members Contact Number</td><td> $_POST['telephone'] </td>";
$Body .= "</tr></table>";

Nope.

Ā 

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

Ā 

Like that. And please, please use the [ code] [ /code] tags (without the initial space) to surround code

Ā 

And a big note, that is to be used only inside of " (double quotes) to get it to display properly.

Thanks for the above, I will play with this, I also have been using


tags but it is loosing tons of lines when I hit post so it only displays some of the code.

By posting the code directly in the window it posts all the code, this is why I did this.

Problem your end with the site?

Got two solutions from two different people here to one problem, I know always different ways in php of doing something to achieve same end result, Which one do I use? I like the idea of the first initial body line with-out all the added text I have in my original script.

Is this a stripped down version that does exactly the same as the line in my script?

not sure who's post you're referring to but just in case you were thinking about using this, i should tidy up what i posted earlier..

Ā 

$Body = "Robert. You have received these contact details .com:<br /><br />"; 
$Body .= "<table>";
foreach($fields as $a => $b)
{
Ā  $Body .= "<tr><th>" . $a . "</th><td>" . $_REQUEST[$a] . "</td></tr>";
}
$Body .= "<tr>";
$Body .= "<td width=\"200px\">Member Name</td><td width=\"400px\">". $_POST['Member_Name'] ."</td>";
$Body .= "</tr><tr>";
$Body .= "<td>Members Contact Number</td><td>". $_POST['telephone'] ."</td>";
$Body .= "</tr></table>";

Nope.

Ā 

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

Ā 

And a big note, that is to be used only inside of " (double quotes) to get it to display properly.

This is what was posted before your post.

I tried your foreach loop.

It threw an error so I used the curly brackets

$body = "Robert. You have received these contact details .com:<br /><br />"; 
$body .= "<table>";
foreach($fields as $a => $b)
{
Ā  $body .= "<tr><th>" . $a . "</th><td>" . $_REQUEST[$a] . "</td></tr>";
}
$Body .= "<tr>";
$Body .= "<td width=\"200px\">Member Name</td><td width=\"400px\"> $_POST{['Member_Name']} </td>";
$Body .= "</tr><tr>";
$Body .= "<td>Members Contact Number</td><td> $_POST{['telephone']} </td>";
$Body .= "</tr></table>";

Ā 

It then worked. or is it working because the script can read past the now added curly brackets and is ignoring the rest of the body tags?

I will comment out the body tags and see what happens.

Sorry for the confusion here. We're actually dealing with two separate things. the first being my interpretation of:

Ā 

send my form fields as an html table instead of an array as plain text

Ā 

This is where the my last post comes into play. It will let you iterate through your field array and display each one on a new row in a html table.

Ā 

the second, is the errors you have been getting, which premiso quite rightly said:

Ā 

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

Ā 

YOu either have to concat arrays or use { } around them inside quotes.

Ā 

My first post, unfortunately had serveral errors from copying your code. You were using $body and $Body variables and there was still the problem premiso pointed out. I just changed the code i needed to get the loop to work. I should have cleaned up the errors before submitting so you weren't trying out code with blatant errors in it. My correction should work ok.

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.