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

WordPress

Server doesn't send email

Alright, so I created an wordpress template and added a page (contact) with a custom template (contact.php) which includes an contact form. The form action is contact-form.php. But the e-mail won't be sent to a server.

So here is the form:

    <form name="contactForm" action="contact-form.php" method="post">
        <input type="text" placeholder="Voornaam" name="Firstname" style="width: 335px;" required="required">
        <input type="text" placeholder="Achternaam" name="Lastname" style="width: 336px;" required="required">
        <textarea placeholder="Voer hier uw bericht in..." name="Message" required="required"></textarea>
        <input type="email" placeholder="E-mail adress" name="Email" required="required">
        <input type="submit" value="Verstuur" name="Send" class="fontWeight">
    </form>

And here is the contact-form.php (receiver):

<?php
    $firstname = $_POST['Firstname'];
    $lastname = $_POST['Lastname'];
    $email = $_POST['Email'];
    $message = $_POST['Message'];

    $Adminemail = '"Enzio" <mail@enziorossa.nl>';

    $subject = $firstname.' '.$lastname.' has messaged you via the website.';
    $subject2 = 'A copy of your message send to Nicalyo';

    $headers = "From: " . strip_tags($email) . "\r\n";
    $headers .= "Reply-To: ". strip_tags($email) . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $bericht = '<table>
            <tr><td colspan="2"><font color="red">The following person has send this message:</font></td><tr>
            <tr><td width="120">Full name: </td><td>'.$firstname.' '.$lastname.'</td></tr>
            <tr><td width="120">Date: </td><td>'.Date("d-m-Y").'</td></tr>
            <tr><td width="120">E-mail: </td><td>'.$email.'</td></tr>
            <tr><td colspan="2">&nbsp;</td><tr>
            <tr><td width="120">Message: </td><td>'.nl2br($message).'</td></tr>
        </table>';

    $bericht2 = '<font color="red">This is a copy of your message:<br></font><br>'.$bericht;

    if(mail($Adminemail, $subject, $bericht, $headers))
    {
        mail($email, $subject2, $bericht2, $headers);
        $pageTitle = "Dank u - Nicalyo";
        include('header.php');
        echo "<div class='thanks-loader'>";
        echo "<div class='container' style='text-align: center;'>";
        echo "<h1>Dank u, u hoort zo snel mogelijk van ons</h1>";
        echo "<div class='button'><a href='index.php'>Terug naar de site</a></div>";
        echo "</div>";
        echo "</div>";

        include('footer.php');
    }
    else
    {
        echo 'An error occured, please contact the site administrator.';
    }
?>

Does anyone know what the problem might be?

4 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

First of all, you should really send the email from within the functions.php file. Check out this post from Tom McFarlin: http://tommcfarlin.com/sending-data-post/

I would also second Matthew's suggest to use the WP Mail SMTP plugin - https://wordpress.org/plugins/wp-mail-smtp/. This plugin will let you use the native WP functions to send mail, but it will hijack it before sending and use the SMTP info instead.

You can do this for local development as well.

Hope this helps!

Matt Campbell
Matt Campbell
9,767 Points

Get an SMTP email plugin for WordPress, the built in PHP mailer is rubbish and doesn't work half the time.

Make sure you are running a SMTP server on your host. Are you on a shared webhosting?

How can I be sure I am running an STMP server? And as far as I know and understand your question, I'm not on a shared webhosting.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hi Enzio Rossa did you get your problem sending email resolved?

Yes, thank you ;)