Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Lauren Clark
Lauren Clark
33,155 Points

PHP Contact Form / Mailer working on Local but not when Hosted

I followed the steps to make the PHP contact form from the PHP course, and it's working fine, sends mail from MAMP perfectly. Now I've set it up on a client's server, which seems quite restrictive in terms of settings and it's not sending any mail out.

Is there something you're supposed to do when you go live with the contact form? I know there is the option to go through SMTP - if I use google's SMTP server - will it make any difference or is it purely the awful hosting (freezone, sucks)

Here's the code I'm using, modified for some javascript alerts...

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $phone = trim($_POST["phone"]);
    $message = trim($_POST["message"]);
    $subject = trim($_POST["subject"]);


    if ($name == "" OR $email == "" OR  $message == ""  OR  $phone == "" ) {
    //     echo "Please complete all the fields!";
    //     exit;
    // }
                    ?>
  <script type="text/javascript">
    alert("All fields are required. Please fill them in.");
    history.back();
  </script>
<?php
        exit;

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            // echo "alert('There was a problem with the information you entered').";    
            // exit;
            ?>
  <script type="text/javascript">
    alert("There was a problem with the information you entered");
    history.back();
  </script>
<?php
        exit;
    }

        }
    }

    if ($_POST["address"] != "") {
        echo "alert('Invalid Entry').";  
        exit;
    }

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

    if (!$mail->ValidateAddress($email)){
?>
  <script type="text/javascript">
    alert("Please enter a valid email address i.e (someone@gmail.com)");
    history.back();
  </script>
<?php
        exit;
    }

    $email_body = "";
    $email_body = $email_body . "Name: " . $name . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Phone: " . $phone. "<br>";
    $email_body = $email_body . "Message: " . $message;

    $mail->SetFrom($email, $name);
    $address = "lauren@mail.com";
    $mail->AddAddress($address, "Wishaw Guitar Lessons");
    $mail->Subject    = "Wishaw Website Contact Form Submission | " . $subject;
    $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;
}
?>

and the form html

            <form class="grid_5 contactForm alpha omega clearfix" action="contact.php" method="post" >

    <table>
                                     <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
                <p class="_17 centerText pad-left">Thanks for the email! We&rsquo;ll be in touch shortly!</p>
            <?php } else { ?>

                <p class="_17 centerText pad-left">Just fill in the form <br /> &amp; we'll get back to you.</p>
                        <tr>
                            <th>
                                <label for="name">Name</label>
                            </th>
                            <td>
                                <input type="text" name="name" id="name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>"

                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="email">Email</label>
                            </th>
                            <td>
                                <input type="text" name="email" id="email" value="<?php if (isset($email)) { echo htmlspecialchars($email); } ?>"
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="phone">Phone</label>
                            </th>
                            <td>
                                <input type="text" name="phone" id="phone" value="<?php if (isset($phone)) { echo htmlspecialchars($phone); } ?>"
                            </td>
                        </tr>
                          <tr>
                            <th>
                                <label for="phone">Subject</label>
                            </th>
                            <td>
                                <select name="subject" size="1" id="subject">
                                <option value="Lesson Booking Request">Lesson Booking Request</option>
                                <option value="General Enquiry">General Enquiry</option>
                                <option value="Student Email Support">Student Email Support</option>
                                <option value="Equipment Hire">Equipment Hire</option>
                                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="message">Message</label>
                            </th>
                            <td>
                                <textarea name="message" id="message"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
                            </td>
                        </tr> 
                        <tr style="display: none;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <input type="text" name="address" id="address">
                                <p>shh.</p>
                            </td>
                        </tr>                   
                    </table>
                    <input type="submit" value="Send" class="btn btn-3"/>
            </form>

               <?php } ?>
            </div>


    </section>

Please help!

6 Answers

Patrick Cooney
Patrick Cooney
12,216 Points

If you use google's SMTP it shouldn't matter whether the host has the mail port open or not. At least it shouldn't. I know with Mandrill you can send mail using their SMTP from a server without an active mail port.

Lauren Clark
Lauren Clark
33,155 Points

Just a note for this, anyone else suffering the same issue - the host seemed to be blocking any external SMTP! Had to use the hosts SMTP through domain. So smtp.yourdomainname.com and the username/password of your email account if you have one set up with the host. This is usually an alias for whatever their mail server is. It's now all working fine.

Contact the host and ensure they provide mail via the server, if so they will tell you how to implement it, or try the FAQ's for the host.

Lauren Clark
Lauren Clark
33,155 Points

Would I be able to send the email through the mail server on the system do you think? So if the domain is blah.com and there is an info@blah.com could i use SMTP at blah.com and the username and password?

Patrick Cooney
Patrick Cooney
12,216 Points

For this to work the host has to have an open mail port and you have to know the address where it can be located. Some hosts will tell you, some wont.

Lauren Clark
Lauren Clark
33,155 Points

Ok. I noticed with the send through google SMTP it has a username and password? So you have to set up a mail account with them to do it? (Sorry sounds obvious but wanted to check)

Patrick Cooney
Patrick Cooney
12,216 Points

Yeah, probably. I don't know for sure since I haven't tried it with google. It's not going to let you send stuff without some way of knowing who's sending it. You can also look into something like Mandrill or postmark or any other service that offers SMTP delivery. They usually have a quickstart guide with everything you need and allow you a few thousand free emails a month. It's worth checking out. I can vouch for Mandrill. Pretty easy to set up.

Lauren Clark
Lauren Clark
33,155 Points

Wow, mandrill looks a breeze. Why does Randy go for the paid-for stuff in his blog? Anyway, can't use Mandrill because the *&%& host doesn't allow text records. It's just the worst host I've ever.. Anyway. Thanks for your help, looks like I'll get there eventually :)

Patrick Cooney
Patrick Cooney
12,216 Points

In theory, if they give you smtp credentials to set up an external mail client (like Apple Mail or Microsoft Outlook) you should be able to use those same credentials in the PHP code.