We help IT Professionals succeed at work.
Get Started

Understanding and modifying a send email autoreply

John Carney
John Carney asked
on
501 Views
Last Modified: 2013-12-13
With this email response to a form submission, I would like to include the info entered by the user in my autoreply to him. How do I do that?

Also, could you tell me the meaning of some things I don't understand?

What is the meaning of this line: "foreach($fields as $a => $b) ..."  Neither $a or $b have been mentioned yet in the code, so what do they represent? I can't find "=>" as a php operator, so what does it mean?  

In fact, so I can really understand how it works, what does each element in the whole line do:
foreach($fields as $a => $b){       
      $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

Thanks,
John
<?php 
$to = $_REQUEST['sendto'] ; 
$from = $_REQUEST['Email'] ; 
$name = $_REQUEST['Name'] ; 
$headers = "From: $from"; 
$subject = "Web Contact Data"; 
$fields = array(); 
$fields{"Name"} = "Name";
$fields{"Company"} = "Company"; 
$fields{"Email"} = "Email"; 
$fields{"Phone"} = "Phone"; 
$fields{"list"} = "Mailing List"; 
$fields{"Message"} = "Message"; 
$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@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 == '') {$ermsg = "You have not entered an email, please go back and try again";} 
	else { 
		if($name == '') {$ermsg = "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.discretedata.com/Test/thankyou.html" );} 
			else {$ermsg = "We encountered an error sending your mail, please notify me@mysite.com"; }
		} 
	} 
?>

Open in new window

Comment
Watch Question
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE