0

I am first to try this, I have given $to:my gmail when I click on submit it didn't show any error but I am not receiving any mail to my gmail. What's wrong? I did:

<html>
<body>
<form method="post" name="myemailform" action="form-to-email.php">
    <p>
        <label for='name'>Enter Name: </label><br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Enter Email Address:</label><br>
        <input type="text" name="email">
    </p>
    <p>
        <label for='message'>Enter Message:</label> <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
</form>

</body>
</html>

PHP:- form-to-email.php

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'tom@amazing-designs.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

$to = "kasani.prabha@GMAIL.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

And what about $email_from? It should be a valid email; I mean if I enter abc@abc.ac some wrong email while submitting, I will not receive the mail?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kasani Prabhakar
  • 109
  • 1
  • 2
  • 7

1 Answers1

0

email from is not validated by the SMTP server but the problem will start when the end user is trying to reply back it will be delivered or bounce from to the email address added in email_from. SMTP provider also verify that you should be the owner of the email domain like in the case of

                      abc@abc.com

you should be the owner of the

                     abc.com

For this they give some kind of key which need to be added in domain DNS entry . The reason is to avoid the Spam.

Devesh
  • 4,500
  • 1
  • 17
  • 28