Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Send html form data to email

Hi,

I have a script that takes data from an html form and sends it to an email address. It works from my website hosted at Godaddy but does not work from the Apache server on my computer. I don't get any error messages and the server serves up a conformation message as if everything went well. Here is the code.

Thanks, Jeff

<?php

 $to = "jeffbusch@charter.net";
 $from = $_REQUEST['Email'];
 $name = $_REQUEST['FirstName'];
 $headers = "From: $from"; 
 $subject = "Prayer Request"; 

 $fields = array(); 
 $fields{"FirstName"} = "FirstName";
 $fields{"LastName"} = "LastName";
 $fields{"AddressLine1"} = "AddressLine1";
 $fields{"AddressLine2"} = "AddressLine2";
 $fields{"City"} = "City";
 $fields{"State"} = "State";
 $fields{"PostalCode"} = "PostalCode";
 $fields{"Country"} = "Country";
 $fields{"Phone"} = "Phone";
 $fields{"Fax"} = "Fax";
 $fields{"Email"} = "Email";
 $fields{"Title"} = "Title"; 
 $fields{"Comments"} = "Comments"; 

 $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@mywebsite.net"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting me. I will get back to you as soon as possible, usually within 48 hours.";

 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.mywebsite.net/thankyou.html" );
    } else {
      print "I've encountered an error sending your information, please try later.";
    }
 }
}

?>

1 Answer

PHP can send mail in one of two ways.

The first, and the default on non-Windows systems, is to use the local mail transfer agent installed on the system. This would be "sendmail" or an application compatible with it, the most popular probably being postfix.

The other is to connect via SMTP to some mail server.

You will either need to install a mail transfer agent on your local system (and set it up correctly), or edit PHP's configuration to specify an SMTP server address and port.

Source: PHP mail() not working

Thanks Volker, I'll have to look into that.