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

PHPMailer not working!

Hi Randy and Guys,

This is really frustrating me....if anyone can help me out I would really appreciate it!

I'm basically making a website for my wedding and the form i'm trying to receive is for the RSVP page. I have followed the steps Randy suggested in his videos exactly.

I've uploaded the website onto the server and the form works perfectly. I get the '.php?status=thanks' page, BUT I don't receive an email??

I've tried everything and i'm out of ideas. I've posted my code below, so can anyone see where i'm going wrong?

<?php 
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = trim($_POST["name"]);
        $email = trim($_POST["email"]);
        $resultYes = trim($_POST["result_option_YES"]);
        $resultNo = trim($_POST["result_option_NO"]);
        $transportYes = trim($_POST["transport_option_YES"]);
        $transportNo = trim($_POST["transport_option_NO"]);
        $veggie_option = trim($_POST["veggie_option"]);
        $nutr = trim($_POST["nutr-comment"]);
        $comments = trim($_POST["comments"]);

        if ($name == "" OR $email == "") {
            echo "Please enter your name and email!";
            exit;
        }

        foreach( $_POST as $value) {
            if (stripos($value, 'content-type:') !==False){
                echo "There was a problem with the info you provided.";
                exit;
            }
        }

        if ($_POST["address"] !=="") {
            echo "Your form has an error";
            exit;
        }

        require_once('inc/phpmailer/class.phpmailer.php');      

        $mail = new PHPMailer();

        if (!$mail->ValidateAddress($email)){
            echo "You must specify a valid email address.";
            exit;
        }

        $email_body = "";
        $email_body = $email_body . "Name: " . $name . "<br>";
        $email_body = $email_body . "Email: " . $email . "<br>";
        $email_body = $email_body . "Attending: " . $resultYes . $resultNo . "<br>";
        $email_body = $email_body . "Transport: " . $transportYes . $transportNo . "<br>";
        $email_body = $email_body . "Veggie: " . $veggie_option . "<br>";
        $email_body = $email_body . "Nutritional Info.: " . $nutr . "<br>";
        $email_body = $email_body . "Comments: " . $comments;

        $mail->SetFrom($email, $name);
        $address = "joeli1989@gmail.com";
        $mail->AddAddress($address, "Joe Li");
        $mail->Subject    = "RSVP Form | " . $name;
        $mail->MsgHTML($email_body);

        if(!$mail->Send()) {
          echo "There was a problem sending the form.: " . $mail->ErrorInfo;
          exit;
        } 
        header("Location: rsvp.php?status=thanks");
        exit;

        }
?>

6 Answers

I'm not the greatest at this, but did you put this script in the proper file location that you have listed: "inc/phpmailer/class.phpmailer.php"

Hey Justin,

Yeah it's definately in the correct file.

Justin - It looks like, you forgot the SMTP information in your PHP script.

Here's a great tutorial on how to setup your phpmailer.php script for use with gmail

Hi James,

This is my first form so I'm still learning....can you tell me why I need to incorporate the SMTP?

Thanks,

Joe

Hi James,

It works! I can't thank you enough for your help. The only problem is, it doesn't display the ?thanks page...instead it displays:

SMTP -> FROM SERVER:220 mx.google.com ESMTP g6sm12727629qav.6 - gsmtp SMTP -> FROM SERVER: 250-mx.google.com at your service, [2607:f298:5:105b::ef:72e5] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250 PIPELINING SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS SMTP -> FROM SERVER: 250-mx.google.com at your service, [2607:f298:5:105b::ef:72e5] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250-ENHANCEDSTATUSCODES 250 PIPELINING SMTP -> FROM SERVER:250 2.1.0 OK g6sm12727629qav.6 - gsmtp SMTP -> FROM SERVER:250 2.1.5 OK g6sm12727629qav.6 - gsmtp SMTP -> FROM SERVER:354 Go ahead g6sm12727629qav.6 - gsmtp SMTP -> FROM SERVER:250 2.0.0 OK 1364658938 g6sm12727629qav.6 - gsmtp SMTP -> FROM SERVER:221 2.0.0 closing connection g6sm12727629qav.6 - gsmtp

Warning: Cannot modify header information - headers already sent by (output started at /home/charlotteandjoewedding/charlotteandjoewedding.com/inc/phpmailer/class.smtp.php:121) in /home/charlotteandjoewedding/charlotteandjoewedding.com/rsvp.php on line 113

Do you have any ideas?

Sorry to bother you!

Thanks,

Joe

Did you ever find the answer? I just started a site and am getting that in my website, though it works fine in localhost/ searching online for the answer now, Hard to find I guess when you're new at it :) Gina