Need help with emailing form with PHP

Discussion in 'Web Design and Development' started by bjett92, Feb 10, 2008.

  1. bjett92 macrumors 6502a

    Joined:
    Oct 22, 2007
    Location:
    Indy, IN
    #1
    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.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    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.
     
  3. bjett92 thread starter macrumors 6502a

    Joined:
    Oct 22, 2007
    Location:
    Indy, IN
    #3
    Thanks, but where does the
    PHP:
    require_once("mail.php")
    go? I'm extremely new to PHP. :D
     
  4. iTim314 macrumors 6502

    Joined:
    Jun 5, 2005
    Location:
    U.S.
    #4
    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.
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Anywhere near the top before you use the mail function.
     

Share This Page