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

Adrian Pineda
Adrian Pineda
13,778 Points

How Do Can I add the mailto attribute to this php file?

I want to make this file point to a specific email address but don't know how. Can someone please let me know where and what attribute I need to enter?

<?php

if (isset($_REQUEST['action'])) { if ($_REQUEST['action'] == "contact_form_request") {

    $ourMail = "support@webtemplatemasters.com"; 

    $required_fields = array("name", "email", "message");
    $pre_messagebody_info = "";

    $errors = array();
    $data = array();
    parse_str($_REQUEST['values'], $data);

    //check for required and assemble message

    if (!empty($data)) {
        foreach ($data as $key => $value) {
            $name = strtolower(trim($key));
            if (in_array($name, $required_fields)) {
                if (empty($value)) {
                    $errors[$name] = "Enter please " . $name . " right!";
                }
            }

            if ($name == "email") {
                if (!isValidEmail($value)) {
                    $errors[$name] = "Enter please " . $name . " right!";
                }
            }
        }
    }

    $result = array (
        "is_errors" => 0,
        "info" => ""
    );

    if (!empty($errors)) {
        $result['is_errors'] = 1;
        $result['info'] = $errors;
        echo json_encode($result);
        exit;
    }

    $pre_messagebody_info.="<strong>Name</strong>" . ": " . $data['name'] . "<br />";
    $pre_messagebody_info.="<strong>E-mail</strong>" . ": " . $data['email'] . "<br />";
    $pre_messagebody_info.="<strong>Website</strong>" . ": " . $data['website'] . "<br />";

    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers.= "From: " . $data['email'] . "\r\n";

    $after_message = "\r\n<br />--------------------------------------------------------------------------------------------------\r\n<br /> This mail was sent via contact form";

    if (mail($ourMail, "Email from contact form", $pre_messagebody_info .="<strong>Message</strong>" . ": " . nl2br($data['message']) . $after_message, $headers)) {
        $result["info"] = "success";
    } else {
        $result["info"] = "server_fail";
    }

    echo json_encode($result);
    exit;
}

}

function isValidEmail($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); }

?>

THANKS!!!!

3 Answers

Hey Aaron,

In this PHP contact form, the mail is being sent to the email stored in the variable $ourMail which is currently "support@webtemplatemasters.com". If you change what is inside $ourMail to your email address, the PHP contact form will send to the correct address. How you can tell that this is the email address is to look at the mail command near the bottom of the page. The very first argument is where the email is sent to, and it is the $ourMail variable.

Adrian Pineda
Adrian Pineda
13,778 Points

Thanks Marcus, Actually that is exactly what I did, and for some reason I was still not receiving the emails. This file is a php file named contact-send.php. Could it have something to do with the HTML? Here's the code:

<form method="post" action="contact-send.php" class="contact-form">

The only issue I am having is that the emails are not reaching my inbox. All the interactive elements from the form work. Would I maybe need to do anything from the c-panel?

I can't see your HTML, but if this is in a separate file, you have to make sure to send the form data to this file to be processed. You do that with the action attribute. You would want to have your form set up like this:

<form action="contact-send.php" method="post">
<!--FORM ELEMENTS AND THINGS AND STUFF HERE-->
</form>
Adrian Pineda
Adrian Pineda
13,778 Points

Yeah that is exactly what I have and it is still not reaching me! I am going to delete the site from the sever and upload again, well see how it goes! Thanks!

Make sure you have all of your name attributes set up correctly and they match up with the PHP as well. Oh and it looks like you might need to change your first if-statement to the name of the page like this:

<?php

if (isset($_REQUEST['action'])) { if ($_REQUEST['action'] == "contact-send") {}
?>

There's a lot going on there, and I didn't thoroughly look at every single piece of code.

Adrian Pineda
Adrian Pineda
13,778 Points

Thanks! I got it! My issue wasn't the code it was that I had similar files named in a similar way, and changes I thought I had made were never made. Lots of wasted time but lesson learned!

Thanks again!!!

Oh awesome! I'm glad you got it figured out! :)