Page 1 of 1

align php email form

Posted: Tue Dec 04, 2012 3:46 am
by jonnyfortis
Hello i have a form script that works well but the output email makes all the content centred.i really need it aligned left. can anyone help please?

this is what i have so far

Code: Select all

<?php 
 $to = $_REQUEST['sendto'] ; 
 $from = $_REQUEST['email'] ; 
 $name = $_REQUEST['fullName'] ; 
 $headers = "From: $from"; 
 $subject = "website application"; 

 $fields{"title"} = "Title"; 
 $fields{"fullname"} = "Full Name"; 
 $fields{"teleNumber"} = "Telephone Number";
 $fields{"mobileNumber"} = "Mobile Number";
 $fields{"email"} = "Email Address";
 $fields{"contactMethod"} = "Preferred method of contact"; 
 $fields{"currentAddress"} = "Current Address";

 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 
 $headers2 = "From: noreply@website.com"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible.";
 
 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a 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.website.com/index.php" );} 
 else 
 {print "We encountered an error sending your mail, please notify webmaster@website.com"; } 
 }
}
 ?> 
im thinking its this line of code i need to modify?

Code: Select all

 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
thanks in advance

Re: align php email form

Posted: Tue Dec 04, 2012 6:16 am
by social_experiment
what happens if you change the 20 in that statement to 0 ?

Re: align php email form

Posted: Thu Jan 03, 2013 8:17 am
by jonnyfortis
what happens if you change the 20 in that statement to 0 ?
the formatting is no longer centred BUT some of the feilds are side by side and some are underneath each other

[text]We have received the following information:

Title: Mr
Full Name:
Telephone Number: test12
Mobile Number: test12
Email Address: test@website.co.uk
Preferred method of contact: Telephone
Current Address: test12
Where are you interested in renting: anywhere What is the maximum rental payment per month: test12 What sort of property are you interested in: House
Smoker: no
Employment status: Unemployed
How many people will live in the property: 1 How many children: 1 Intended moving in date: 17 Intended moving in month: Feb Intended moving in year: 2014 Have you funds available: No
[/text]

Re: align php email form

Posted: Fri Jan 04, 2013 3:57 pm
by califdon
It appears that the formatting is the way you want it, up through the "current address" information, using social-experiment's suggestion. The remainder of the output, which is improperly formatted, doesn't seem to be coming from the code you provided to us. Where is it coming from???

Re: align php email form

Posted: Sat Jan 05, 2013 4:24 am
by VladSun
Your code is vulnerable to headers injection. Sanitize your input data or get ready to have your mail server blacklisted.

Re: align php email form

Posted: Sat Jan 05, 2013 5:54 pm
by jonnyfortis
thats not good, maybe i scrap all the formatting and start again

Re: align php email form

Posted: Sat Jan 05, 2013 6:36 pm
by social_experiment
http://tinyurl.com/sanitizeInput
Information on how to sanitize user input; take a look at some of those urls

re the formatting; try putting a new line at the end of the data instead of in front of it. You could easily turn the output into html and use <br /> or css for the layout of the email if you wanted to.

Re: align php email form

Posted: Mon Jan 07, 2013 4:04 am
by jonnyfortis
Thanks the formatting has been sorted, looks fine now


$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("\n%0s: %s\n",$b,$_REQUEST[$a]); }

with regards to the other issues i have of "sanitize" i am trying to figure out what it currently is so a bit worrying. I am reading the links you sent me

Re: align php email form

Posted: Mon Jan 07, 2013 4:12 am
by social_experiment

Code: Select all

$headers = "From: $from";
Because the $from value comes from the user, i'm guessing it is their email address, it can contain data which might turn your server into one that sends out lots of spam. Have a look at the url below; it explains the concept of email header injection. Just a note, if you are getting this data from a form, use $_POST instead of $_REQUEST.

http://foundationphp.com/blog/2010/12/3 ... injection/

hth :)