align php email form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

align php email form

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: align php email form

Post by social_experiment »

what happens if you change the 20 in that statement to 0 ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: align php email form

Post 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]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: align php email form

Post 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???
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: align php email form

Post by VladSun »

Your code is vulnerable to headers injection. Sanitize your input data or get ready to have your mail server blacklisted.
There are 10 types of people in this world, those who understand binary and those who don't
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: align php email form

Post by jonnyfortis »

thats not good, maybe i scrap all the formatting and start again
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: align php email form

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: align php email form

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: align php email form

Post 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 :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply