Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bjett92

macrumors 6502a
Original poster
Oct 22, 2007
733
1
Indy, IN
I've been trying to create a small web design service, and am working on making a request form that emails me all of their information when they submit the form. However, the email never sends. Is there anything wrong with my code?

PHP Code:
Code:
<?php
// Pick up the form data and assign it to variables
$first_name = $_REQUEST['first_name'] ; 
$last_name = $_REQUEST['last_name'] ; 
$email = $_REQUEST['email'] ; 
$aim = $_REQUEST['aim'] ; 
$yim = $_REQUEST['yim'] ; 
$type = $_REQUEST['type'] ; 
$site_name = $_REQUEST['site_name'] ; 
$comments = $_REQUEST['comments'] ; 

// Build the email (replace the address in the $to section with your own)
$to = 'bjett92@comcast.com';
$subject = "New Request";
$message = "$first_name $last_name ($email, AIM: $aim, YIM: $yim) needs a $type site named $site_name.  Additional comments are: $comments";
$headers = "From: $email";

// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);

// Redirect
header("Location: thankyou.php");
?>

And the HTML form:
Code:
<form id="request" action="mail.php" method="post">
First Name: <input type="text" name="first_name" /><br />
Last Name: <input type="text" name="last_name" /><br />
E-mail: <input type="text" name="email" /><br />
AIM Screen Name: <input type="text" name="aim" /><br />
YIM Screen Name: <input type="text" name="yim" /><br />
Type of Site: <select name="type"><option value="Business">Business</option><option value="Personal">Personal</option><option value="other">Other</option></select><br />
Name of Site: <input type="text" name="site_name"><br />
Additional Comments:<br /><textarea name="comments"></textarea><br />
<input type="submit" value="Send Request" /><input type="reset" value="Clear" />
</form>

Thanks for any help.
 
Biggest thing missing:
PHP:
require_once("Mail.php");

Here's a snippet from what I have on my own comment form,
PHP:
    $headers = array ('From' => $from,
      'To' => $to, 'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host, 'auth' => $auth));
    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      echo "<p class='alert'>Error occurred: ".
         $mail->getMessage() ." Please try again another time.</p>";
      // close up shop
      include "foot.php"; exit;
    }
    else {
      // success message ....
    }
SMTP servers are often needed, at least it was in my case. For me I used earthlink's SMTP mail server, which is how they wanted me to use it.
 
Biggest thing missing:
PHP:
require_once("Mail.php");

Here's a snippet from what I have on my own comment form,
PHP:
    $headers = array ('From' => $from,
      'To' => $to, 'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host, 'auth' => $auth));
    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      echo "<p class='alert'>Error occurred: ".
         $mail->getMessage() ." Please try again another time.</p>";
      // close up shop
      include "foot.php"; exit;
    }
    else {
      // success message ....
    }
SMTP servers are often needed, at least it was in my case. For me I used earthlink's SMTP mail server, which is how they wanted me to use it.

Thanks, but where does the
PHP:
require_once("mail.php")
go? I'm extremely new to PHP. :D
 
Honestly, I've never had any luck with the mail() funtion.

What I used is an open source PHP program called PHPMailer. It gives you so many options compared to mail(), and it's simple to set up. I used it when I was about 4 months into learning PHP casually.

Source:
http://sourceforge.net/projects/phpmailer

Tutorial
http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html

I don't know how well the tutorial is that I gave you, but just search for PHP Mailer Tutorial and your bound to find dozens.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.