0

Recently, I have purchased a domain and hosting. I've also created a webpage having contact form in it. I want when the user wrote something in textarea of contact form and clicks submit then that message is sended to my gmail account and so that i can reply also to those messages. I've also used this script to do so but its not working.

This is sendemail.php file

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Thank you for contact us. As early as possible  we will contact you '
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = 'raunakhajela@gmail.com';//replace with your email

    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    echo json_encode($status);
    die;

This is my contact form code in index.php

<div class="contact" id="contact">
            <div class="container-3x">

                <div class="block-header"> <!-- Blocks Header -->
                    <h2 class="title">Contact Us</h2>
                </div>

                <div class="block-content"> <!-- Blocks Content -->

                    <form action="sendemailphp" method="post" class="trigger-form-animate">
                        <input type="text" value="" id="name" name="name" placeholder="Name *" />
                        <input type="text" value="" id="email" name="email" placeholder="Email *" />
                        <input type="text" value="" id="website" name="website" placeholder="Website *" />
                        <textarea type="text" value="" id="message" name="message" rows="3" placeholder="Your Message *" ></textarea>
                        <div class="clr"></div>
                        <button>Submit Message</button>
                    </form>

                </div>

            </div>

        </div>  

I've also tried "http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html" this tutorial but its not working. Unable to find "Send through Gmail (easier to set up)" this option in gmail on adding another accounts windoww.

How can i do that?? Plzz hlp

Raunak Hajela
  • 103
  • 2
  • 10
  • 1
    Suppressing errors doesn't help your cause using `@` and not [**checking for them**](http://php.net/manual/en/function.error-reporting.php) in the first place. – Funk Forty Niner Oct 20 '14 at 12:06
  • yes, remove `@` and also use `error_reporting(E_ALL);` and `display_errors(1);` And let's `var_dump($success);` Is it true or false? – vaso123 Oct 20 '14 at 12:09
  • guys i don't know much about php, just downloaded evento template from http://shapebootstrap.net/item/evento-free-music-event-template/ having sendemail.php file in it. I just replaced email address to my email , used this file on another template i've created and uploaded file onto my server but when i tried submitting message then it just displayed "success" but haven't received any message on my gmail account. – Raunak Hajela Oct 20 '14 at 12:19
  • This `action="sendemailphp"` are you sure you didn't meant `action="sendemail.php"`? In doing that, it worked for me. Tested `{"type":"success","message":"Thank you for contact us. As early as possible we will contact you "}` <= response. – Funk Forty Niner Oct 20 '14 at 12:21
  • sry guys it's just syntax error while writing this ques but yes i've tried action="sendemail.php". Yet it didn't worked out... – Raunak Hajela Oct 20 '14 at 12:24
  • The only thing is that, you don't have a `subject` form element which may explain why and mail might be sent to Spam instead. As in `name="subject"`. Either add one of replace `$subject = @trim(stripslashes($_POST['subject']));` with `$subject = "Form submission";` – Funk Forty Niner Oct 20 '14 at 12:24
  • You could also add `` to your form. Either this or what I've outlined just above will work. – Funk Forty Niner Oct 20 '14 at 12:31
  • ohk thanxx fred bro...will try this if didn't worked out will mention in comment... – Raunak Hajela Oct 20 '14 at 12:36
  • @RaunakHajela You're welcome Raunak. I've posted an answer below which you can have a look at, with some additional information. – Funk Forty Niner Oct 20 '14 at 12:38
  • fred can i get your email so that i can contact you for asking future ques..?? – Raunak Hajela Oct 20 '14 at 12:42
  • i checked your profile but der z not any messgae option so that i can contact you... – Raunak Hajela Oct 20 '14 at 12:50
  • Sure there is, click the "Say Hello" link. Btw, you do know how the Stack system works right? If an answer was given and it resolved the problem, you tick the checkmark next to the answer till it turns green. Just thought you might like to know that, because if you ask many questions but do not accept any, future answerers may be reluctant to give "answers". ;) – Funk Forty Niner Oct 20 '14 at 12:53
  • So, is your code working now? – Funk Forty Niner Oct 20 '14 at 13:38
  • Guys both solutions are not working. Now what to do?? – Raunak Hajela Oct 20 '14 at 14:32

2 Answers2

3

You don't have a named subject form element which may explain why mail is failing and may very well be sent to Spam instead.

For example: <input type="text" name="subject">. Either add one of replace.

You will however need to make sure that this is filled out using a conditional statement
(see further below for an example).

$subject = @trim(stripslashes($_POST['subject'])); 

with

$subject = "Form submission";

You could also add this instead to your form:

<input type="hidden" name="subject" value="Form submission">

Either this or what I've outlined just above will work.

Many Email clients will either send mail to Spam or reject it altogether if a "subject" is missing, which is clearly missing from your code.

Checking if subject has been filled example:

if(isset($_POST['subject']) && !empty($_POST['subject'])){

// do someting

}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

try this,both will work

 <?php
  $to = "usermailid@gmail.com";
    $subject = "Welcome to";
    $message = " Hi $username,<br /><br />
    Thank you for signing up with us.<br />

    Thanks <br />";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    // More headers
    $headers .= 'From: <yourmailid@gmail.com>' . "\r\n";
    $mail=mail($to,$subject,$message,$headers);
    if($mail)   
    {
    $to = "admin@gmail.com";

    $subject = "Following Customer Signed Up";
    $message = " $username,Customer is signed up with us,<br /><br />
    Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
    Phone:$phone<br/>Zip Code:$zip<br/>Message:$message_cust<br/><br/><br/>
    Thanks <br />";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    // More headers
    $headers .= 'From: <yourmailid@gmail.com>' . "\r\n";
    $mail=mail($to,$subject,$message,$headers);
    }
     ?>
Hara Prasad
  • 704
  • 6
  • 15