user2847429
user2847429

Reputation:

Sending mail directly from a form

I have an Contact us page on my website. what i want is when someone fills the form and click on send button. The message should be arrived to my gmail. i wrote the following code for it. its not working. is there any other way i can accomplish the same.

Html code:

<form id="ContactForm" action="contacts.php" method="post">
    <div>
        <div  class="wrapper"> <strong>Name:</strong>
            <div class="bg">
                <input type="text" class="input" name="name">
            </div>
        </div>
        <div  class="wrapper"> <strong>Email:</strong>
            <div class="bg">
                <input type="text" class="input" name="email">
            </div>
        </div>
        <div  class="textarea_box"> <strong>Message:</strong>
            <div class="bg">
                <textarea cols="1" rows="1" name="message"></textarea>
            </div>
        </div>
        <a href="javascript:;" onclick="document.getElementById('ContactForm').submit();" class="button"><span>Send</span></a> <a href="#" class="button"><span>Clear</span></a> </div>
          </form>

php code

<?php
session_start();
$to = "[email protected]";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>

Upvotes: 0

Views: 458

Answers (3)

Lithilion
Lithilion

Reputation: 1129

PHP form:

<?php

header( 'Content-Type: text/html; charset=utf-8' );

// Your Email
$receiver = '[email protected]';


if (isset($_POST['send']))
{






    $name = $_POST['name']

    $email = $_POST['email'];

    if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
    {
        die( 'Please fill in all fields!' );
    }
    else
    {
        $subject   = $_POST['subject'];
        $message = $_POST['message'];
    }




    $mailheader  = "From: Your Site <noreply@" .$_SERVER['SERVER_NAME']. ">\r\n";
    $mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
    $mailheader .= "Return-Path: noreply@" .$_SERVER['SERVER_NAME']. "\r\n";
    $mailheader .= "MIME-Version: 1.0\r\n";
    $mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
    $mailheader .= "Message-ID: <" .time(). " noreply@" .$_SERVER['SERVER_NAME']. ">\r\n";
    $mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";


    if (@mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
    {

        echo 'Email send!';
    }
}

?>

HTML form:

<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>

Upvotes: 0

Dan Johnson
Dan Johnson

Reputation: 1482

Firstly, you should check your inputs for PHP injection.

$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);

Apart from that, there doesn't seem to be anything wrong with your mail script. The problem is most likely caused from your PHP server. Does your web hosting definitely provide PHP mail? Most free web hosts do not provide this as they are often used for spamming.

Upvotes: 1

ducin
ducin

Reputation: 26437

Sorry, but your code is crappy (especially, those concatenations). Use Swift mailer which provides OOP-style and does all the header job for you. And make sure you've got any mail server installed (did you check if you have any?).

Upvotes: 0

Related Questions