Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP mail() is adding spaces

Featured Replies

Hi,

 

I am utterly awful in php however I have found this bit of code and modified it to do what I want:

 

<?php 
$to = "--Removed the e-mail address--";
$from = $_POST["fmail"] ; 
$name = $_POST["fname"] ; 
$headers = "From: $from"; 
$subject = "Contact Form"; 

$fields = array(); 
$fields{"fname"} = "Name";  
$fields{"fmail"} = "Email"; 
$fields{"fphone"} = "Phone"; 
$fields{"ftype"} = "Type of Query"; 
$fields{"fmessage"} = "Message"; 

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

$headers2 = "From: noreply@YourCompany.com"; 
$subject2 = "Thank you for contacting us"; 
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";

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://google.com" );} 
else 
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 
}
}
?>

 

My only problem is that on the e-mail it sends contains extra spaces before each line (with exception of the first line).

 

Message:

We have received the following information:

               Name: Name
              Email: e-mail@www.com
              Phone: Phone Number
      Type of Query: Catering
            Message: Message Content

 

Any ideas as to why?

 

Thanks in advance.

$body = "We have received the following information:\n\n"; foreach($fields as $a => $ { $body .=$b."\n"; }

[/code]

 

Try changing the $body line to the above!

 

Sam

  • Author

Unfortunately that gives this error, line 22 is the line with the "$body =..." "Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /home/vhosts/anlens.freeoda.com/index.php on line 22".

$body = "We have received the following information:\n\n";
foreach($fields as $value) { $body .= $value . "\n"; }

Try that ... (Sorry)

 

[edit] had to change $b to $value as this forum keeps changing my code :S [/edit]

  • Author

Exactly the same error.

 

EDIT: It had the same error until you edited it, sorry for the confusion.

  • Author

Thanks, that didn't quite work as it removed the submitted data, I managed to fix it with this though:

 

$body = "We have received the following information:\n\n";
foreach($fields as $a => $value) { $body .= $value . ": " . $_REQUEST[$a] . "\n"; }

 

Thanks for the help once more, I'm starting to like php, I might have to put some time in to learning it.

I was getting to the values :) Glad it helped

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.