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

Sending email form using Gmail

The code below was working on my local server, correctly sending emails and displaying the "Thanks"; however, when I uploaded to my public server, the emails stopped. Any idea? The errors display if you don't enter a proper email address or fill all the fields, but emails won't send. Thank you.

<?php

$pageTitle = "Contact Website";
include('inc/header.php'); 

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$reason = trim($_POST["contactReason"]);
$message = trim($_POST["message"]);
$email_body = "";

if ($name == "" OR $email == "" OR $message == "") {
    echo "<div class=\"submitError\">";
    echo "<a href=\"contact.php\"><image src=\"images/websitelogo.png\"></a>";
    echo "<h3>You must specify values for name, email address, and message.</h3>";
    echo "</div>";
    //exit; 
    } 

    foreach( $_POST as $value ){
    if ( stripos($value,'Content-Type:') !== FALSE ){
        echo "<div class=\"submitError\">";
        echo "<a href=\"contact.php\"><image src=\"images/websitelogo.png\"></a>";
        echo "<h3>There was a problem with the information you entered.</h3>";
        echo "</div>";
        //exit;
    }
    }

    if ($_POST["address"] != "") {
    echo "<div class=\"submitError\">";
    echo "<a href=\"contact.php\"><image src=\"images/websitelogo.png\"></a>";
    echo "<h3>Your form submission has an error.</h3>";
    echo "</div>";
    //exit;
    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$mail->ValidateAddress($email)){
    echo "<div class=\"submitError\">";
    echo "<a href=\"contact.php\"><image src=\"images/websitelogo.png\"></a>";
    echo "<h3>You must specify a valid email address.</h3>";
    echo "</div>";
    //exit;
    }

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->Username = "something@gmail.com";
$mail->Password = "password";

$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email Address: " . $email . "<br>";
$email_body = $email_body . "Reason for Contact: " . $reason . "<br>";
$email_body = $email_body . "Message: " . $message;

$mail->SetFrom($email, $name);

$address = "email@website.com";

$mail->AddAddress($address, "eSmith Books");

$mail->Subject = "Website Contact | " . $name;

$mail->MsgHTML($email_body);

if(!$mail->Send()) {
  echo "There was a problem sending the email: " . $mail->ErrorInfo;
  //exit;
}   

header("Location: contact.php?status=thanks");
//exit;
}

?>

<center><h1>Contact Website</h1></center>

<div class="page-section">  

    <div class="contact-form">                

            <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
                <center><h4>Thank you. We'll be in contact with you soon.</h4></center>

            <?php } else { ?>

                <form method="post" action="contact.php">

                    <table>

                        <tr>
                            <th>
                                <label for="name">Name</label>
                            </th>
                            <td>
                                <input type="text" name="name" id="name">
                            </td>
                        </tr>

                        <tr>
                            <th>
                                <label for="email">Email</label>
                            </th>
                            <td>
                                <input type="email" name="email" id="email">
                            </td>
                        </tr>

                        <tr style="display: none;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <input type="text" name="address" id="address">
                                <p>A Jedi would should leave this field blank.</p>
                            </td>
                        </tr>

                        <tr>
                            <th>
                                <label for="reason">Regarding</label>
                            </th>
                            <td>
                                    <select name="contactReason" id="contactReason">
                                        <option value="questions">Questions</option>
                                        <option     value="comments">Comments</option>
                                        <option value="submissions">Book     Submissions</option>
                                        <option value="services">Services     (editing, proofreading, etc.)</option>
                                    </select>
                            </td>    
                        </tr>   

                        <tr>
                            <th>
                                <label for="message">Message</label>
                            </th>
                            <td>
                                <textarea name="message"     id="message"></textarea>
                            </td>
                        </tr>    

                    </table>
                    <input type="submit" value="Send">

                </form>

            <?php } ?>      

    </div>

</div>  

<?php include('inc/footer.php'); ?>

5 Answers

Thanks. The code is now sitting live at my web hosting company and the issue remains: no email. I downloaded the files from the hosting company back to me and back into my local server and the emails work.

James Barnett
James Barnett
39,199 Points

Contact support at your hosting company and ask the that's what you pay them for.

Edd Figueiredo
Edd Figueiredo
2,268 Points

Dude, if your script was working before, than, it must have nothing to do with it... I had a similar problem, on my testing side, my php script was sending the emails just fine, but I had to tune the mail function on php.ini. When I uploaded to my production server on the internet, I had to change a little bit of my script to work, following the instruction given by my ISP. Maybe you should check if you have to obey some rule by your ISP as well.

Edd Figueiredo
Edd Figueiredo
2,268 Points

It's kinda of a pain, to work with hosting companies... I would like to have my own servers someday...

Hopeful fix is underway. Simple MX record change. Now for the up-to-24-hours-to-take-effect wait. Thanks for your help, guys!

Grabbed the Google MX record values from this link: https://support.google.com/a/answer/174125?hl=en

James Barnett
James Barnett
39,199 Points

Most times DNS changes propagate worldwide in about 15 minutes. You can check the status of a DNS change at https://www.whatsmydns.net/

Thank you for that tip. I was able to check its status, it's live, and nearly all is working now. The form is sending emails and I'm receiving them, but users submitting the form aren't being directed to the Thank you page.