Jump to content

Recommended Posts

I keep coming back because I don't know enough about php and need help.

My email I have requested as html email.

I had to give up trying to use a table with TD/TR to display the data as the email is having issues sending the $body on $_POST

 

The entire form fields have come through but the entire email body is one continious line. How would I make the script seperate each $field from my form?

This is some of the code for the form fields.

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

 

This is the $body part I use

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

I also tried adding this but it sends all the $fields plus adds another load of the fields under the first lot, again as one continious line.

#foreach($fields as $a => $b)
#{
#  $body .= "<tr><td>" . $a . "</td><td>" . $_REQUEST[$a] . "</td></tr>";
#}

 

Thanks for looking and Merry Xmas to all

Link to comment
https://forums.phpfreaks.com/topic/138407-body-of-email-is-one-long-line/
Share on other sites

How can it be so hard for me to simply send an email with all the form fields displayed in the body of the email, this is not new so how does everyone else do it? Am I missing something obvious here? been going in circles for weeks now and still not working right as to my email

Depending on your server sometimes \n and \r\n doesn't work, for some reason on my GoDaddy servers both the \n and \r\n won't work when I'm sending out my emails, try the following method if \n and \r\n doesn't work for you.

 

(This method is not the best way to do it but it does work)

 

$body = "....You have received these contact details from form...:

";

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

 

I just placed in a hard return (hit enter to create a new line) which works for certain providers.

Thanks Crayon Violet, but that never worked, still the same, one continious line of an email,as in about 20 lines with no spaces or breaks, would take ages to type an email like this but i get it all in a split second :-\

I will now try MinidaK03's method.

Something just like this usually works for me just fine (Thats when the \n or \r\n method does not work)

 

<?php
      $to = "[email protected]";
      $headers = "From: [email protected]";
$subject = "anything";
      $message = "Line one
      
      This is now on the third line
This is on the fourth line

this is on the sixth line";
      mail($to,$subject,$message,$headers);
?>

 

But your right it may have something to do if your setting html headers

This is what I just done to see if any difference, and it sent me an email with all the fields now in one long list as a message and not as an html message.

<?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" ) ; 
// I have commented out the headers below and used the one below this
#$headers = 
#      'X-Mailer: PHP/' . phpversion() . "\r\n" .
#      "MIME-Version: 1.0\r\n" .
#     "Content-Type: text/html; charset=utf-8\r\n" .
#     "Content-Transfer-Encoding: 8bit\r\n\r\n";

// I used this instead, but now no html email
$headers = "From: $from";
$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";

//Joint Applicant on ride side of form
$fields{"title1"} = "2 Title";
$fields{"surname1"} = "2 Surname";
$fields{"forname1"} = "2 Forename(s)";
$fields{"residential1"} = "2 Residential Address";
$fields{"town1"} = "2 Town";
$fields{"county1"} = "2 County";
$fields{"post1"} = "2 Postcode";
$fields{"years1"} = "2 Years Lived at Address";
$fields{"months1"} = "2 Months";
$fields{"hometelephone1"} = "2 Home Telephone";
$fields{"mobile1"} = "2 Mobile Telephone";
$fields{"dateofbirth1"} = "2 D.O.B";
$fields{"nationality1"} = "2 Nationality";
$fields{"sex1"} = "2 Sex";
$fields{"marital1"} = "2 Marital Status";

$body = "You have received these contact details from .com:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: [email protected]"; 
$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/r.../thankyou.html" );} 
else 
{print "We encountered an error sending your mail, please notify [email protected]"; } 
}
}
?> 

 

This is the entire code.

Also I don't need the if else statements but get errors when i remove them. I have .js server-side validation on every field on the form before it will submit to the php script.

The company I work for is hosted under godaddy (this host sucks btw) and I am able to use space as my separation of lines or \r

 

I just noticed youre trying \n try \r instead

 

off topic: I had to call tech support and ask if they run under "localhost" or an IP address per database and the "tech support rep" asked me "whats localhost?"....sigh.

got two seperate hosts, two different companies, like I say now I have removed the html headers, I get a long list of all the fields in the array, I will settle for a way of making the $body of these divide into two columns with rows underneath each coloumn. I spent weeks on this and am going round in circles and no further ahead than when I started out.

Would you know how to help me format the body tag to display the fields in columns or formatted rows?

Thanks chronister. Looking and trying now, problem is my form took about 2 days to design, make and impliment.

been making forms for years and always used .js to check the validation, client and server-side. Collecting or sending the data on a form was never my problem, PHP was always done by others.

And what a learning curve, but I will not give up till I learn and understand some of it.

 

I have gone back to the html version of my script with the email containing the form fields in a table. But it only sends a certain amount of the table data and disregards the script after a certain line.

I think I asked you in another topic about this. Does it stop "reading" after a particular point? Is that point the same everytime?

 

Yes chronister, my script stops reading after a certain line. It actually gets half way through reading/processing the line and sends the email with all fields formatted and doesn't get past a certain point in the script line. Also I have started trying to use the PHPMailer but not having much luck with it for what I'm trying to do. Can you shed any light on why a script will stop been read on processing?

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.