Contact Form

I’m sure a lot of people have first looked at php for the same reason I have, you build a contact form for a webpage and then you want it to send an email.

With the code below, a person enters their details then a javascript box pops up to say thanks, email sent etc or to say that it’s invalid please try again - so far so good.

The email then sends to address, my problem is that when I open the email I only receive the message section, I don’t get the persons name, email add or telephone number, which is not much use.

If anybody can see where I’m going wrong an point me in the right direction it would be appreciated - I need sleep :wink:

HTML Snippet

Contact Form

Name

Email Address
Telephone Number
Message

contact.php

[php]<?php
$field_name = $_POST[‘cf_name’];
$field_email = $_POST[‘cf_email’];
$field_phone = $_POST[‘cf_phone’];
$field_message = $_POST[‘cf_message’];

$mail_to = ‘[email protected]’;
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message = 'E-mail: '.$field_email."\n";
$body_message = 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>

<?php } else { ?>
<script language="javascript" type="text/javascript">
	alert('Message failed. Please, send an email to [email protected]');
	window.location = 'index.html';
</script>
<?php } ?>[/php]

Heres hoping somebody can help…

I would recommend using a 3rd party email library, either PHPMailer or Swiftmailer (The One I recommend). However both are good.

This is how I do my contact form ->

First My Class:

[php]<?php
namespace Library\Email;
/*

  • In order to use swiftmailer the below is needed…
    /
    use Swift_SmtpTransport;
    use Swift_Message;
    use Swift_Mailer;
    class Email {
    public $result = \NULL;
    public function __construct(array $data) {
    $this->result = $this->email($data);
    }
    private function email(array $data) {
    /
    Setup swiftmailer using your email server information /
    if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
    $transport = Swift_SmtpTransport::newInstance(EMAIL_HOST, EMAIL_PORT); // 25 for remote server 587 for localhost:
    } else {
    $transport = Swift_SmtpTransport::newInstance(EMAIL_HOST, 25);
    }
    $transport->setUsername(EMAIL_USERNAME);
    $transport->setPassword(EMAIL_PASSWORD);
    /
    Setup To, From, Subject and Message /
    $message = Swift_Message::newInstance();
    $name = $data[‘name’];
    $email_from = $data[‘email’];
    $subject = $data[‘reason’] . ’ email address ’ . $data[‘email’];
    $comments = $data[‘phone’] . ’ ’ . $data[‘website’] . ’ ’ . $data[‘comments’];
    /

    * Email Address message is going to
    */
    $message->setTo([
    [email protected]’ => ‘Joe Smith’// Example email address:
    ]);
    $message->setSubject($subject); // Subject:
    $message->setBody($comments); // Message:
    $message->setFrom($email_from, $name); // From and Name:
    $mailer = Swift_Mailer::newInstance($transport); // Setting up mailer using transport info that was provided:
    $result = $mailer->send($message, $failedRecipients);
    if ($result) {
    return TRUE;
    } else {
    echo “
    ” . print_r($failedRecipients, 1) . “
    ”;
    return FALSE;
    }
    }
    }[/php]

then the contact form (I know I should had done it the other way around ;D )
[php]<?php
require_once ‘…/private/initialize.php’;

use Library\Database\Database as DB;
use Library\Email\Email;

$db = DB::getInstance();
$pdo = $db->getConnection();

$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (isset($submit) && $submit === ‘submit’) {
/* The Following to get response back from Google recaptcah */
$url = “https://www.google.com/recaptcha/api/siteverify”;

$remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
$response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer);
$recaptcha_data = json_decode($response);
/* The actual check of the recaptcha */
if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) {
    $data['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $data['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $data['phone'] = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $data['website'] = filter_input(INPUT_POST, 'website', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $data['reason'] = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $data['comments'] = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_FULL_SPECIAL_CHARS);

    $send = new Email($data);
} else {
    $success = "You're not a human!";
}

}
require_once ‘…/private/includes/header.inc.php’;
?>

    <fieldset>

        <legend><?php echo (isset($success)) ? $success : 'Contact Form'; ?></legend>

        <label for="name" accesskey="U">Name</label>
        <input name="name" type="text" id="name" tabindex="1" autofocus required="required" />

        <label for="email" accesskey="E">Email</label>
        <input name="email" type="email" id="email" tabindex="2" required="required" />

        <label for="phone" accesskey="P" >Phone <small>(optional)</small></label>
        <input name="phone" type="tel" id="phone" tabindex="3">

        <label for="website" accesskey="W">Website <small>(optional)</small></label>
        <input name="website" type="text" id="website" tabindex="4">

        <div class="radioBlock">
            <input type="radio" id="radio1" name="reason" value="message" tabindex="5" checked>
            <label class="radioStyle" for="radio1">message</label>
            <input type="radio" id="radio2" name="reason" value="order">
            <label class="radioStyle" for="radio2">order</label>  
            <input type="radio" id="radio3" name="reason" value="status">
            <label class="radioStyle" for="radio3">status inquiry</label>    
        </div>

        <label class="textBox" for="comments">Comments</label>
        <textarea name="comments" id="comments" spellcheck="true" tabindex="6" required="required"></textarea> 
        <div class="g-recaptcha" data-sitekey="6LfPlQoUAAAAAPgD3PpnQ_uGTzc87UALiFgQ3XnK"></div>
        <input type="submit" name="submit" value="submit" tabindex="7">
    </fieldset>
</form>
<?php require_once '../private/includes/footer.inc.php'; [/php]

and here’s a link to my repository -> https://github.com/Strider64/Slice-of-Technology

Now, I don’t expect you to do it that way, but it should give you a clue on how to do it.

HTH John

P.S. I know I have validated my data coming into my form, but I will get around to do it.

Sponsor our Newsletter | Privacy Policy | Terms of Service