简体   繁体   中英

PHP Contact Form blank emails

Looks like send.php is being accessed and sending blank emails. Need to make it so send.php doesn't send email, unless the submit button is used on a form. I was told 'isset $_post' would help me, just not clear on how to execute it.

Here's send.php code:

    <?php

$recipient  =   'test@test.com';

$email      =   $_REQUEST['Email'];

$subject    =   'Contact Form Submission';

$content    =   "The following has been submitted on your website \n\n";

$redirect   =   'thankyoupage.html';

$user = $_REQUEST['Email'];
$usersubject = "Subject";
$userheaders = "From: test@test.com\n";
$usermessage = "Blah blah blah";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);

foreach($_POST as $k=>$v)
    {
        $content .= "$k: $v\n\n";
    }

mail_it(stripslashes($content), $subject, $email, $recipient);

if (isset($_POST['redirect']))
    {
        header("Location:".$_POST['redirect']);
    }
        else
    {
        header("Location: $redirect");
    }

function mail_it($content, $subject, $email, $recipient)
    {

        $headers .= "From: ".$email."\n";
        $headers .= "Reply-To: ".$email."\n";

        if ($bcc) $headers .= "Bcc: ".$bcc."\n"; 

        $headers .= "X-Priority: 0\n";
        $headers .= "X-Mailer: bjm Formmail \n";
        $message = $content."\n\n";

        if( !mail($recipient, $subject, $message, $headers))
        {
            echo "Sorry - an error occured trying to send the mail";
        }

    }

?>

add this to your php script:

if ($Name == '')
{
    die("<p align='center'><font face='Arial' size='6' color='#FF0000'>Please use our<a href=''> contact form</a> to send a message to us</font></p>");
}

add it just after:

$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email`enter code here`'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

if you are getting blank emails it means someone is running the action="contact.php" script from the browser like www.yousite.com/contactsend.php

this code above will stop it

You need to use \\r\\n at the end of header lines. But if you only have one header you don't need it at all.

You need to make sure the code is only executed when there is a post request. You can do that by wrapping everything in:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
   ...
}

If you want to send mail, only when someone hits submit button, simply use this

if (isset($_POST['submit'])){
   send_mail();      //and define send mail somewhere else 
}

You have to check wether the request method was POST. If so, you should send the email:

<?php

$recipient  =   'test@test.com';

$email      =   $_REQUEST['Email'];

$subject    =   'Contact Form Submission';

$content    =   "The following has been submitted on your website \n\n";

$redirect   =   'thankyoupage.html';

$user = $_REQUEST['Email'];
$usersubject = "Subject";
$userheaders = "From: test@test.com\n";
$usermessage = "Blah blah blah";


foreach($_POST as $k=>$v)
    {
        $content .= "$k: $v\n\n";
    }

if($_SERVER["REQUEST_METHOD"] == "POST") {
     mail_it(stripslashes($content), $subject, $email, $recipient);
}

if (isset($_POST['redirect']))
    {
        header("Location:".$_POST['redirect']);
    }
        else
    {
        header("Location: $redirect");
    }

function mail_it($content, $subject, $email, $recipient)
    {

        $headers .= "From: ".$email."\n";
        $headers .= "Reply-To: ".$email."\n";

        if ($bcc) $headers .= "Bcc: ".$bcc."\n"; 

        $headers .= "X-Priority: 0\n";
        $headers .= "X-Mailer: bjm Formmail \n";
        $message = $content."\n\n";

        if( !mail($recipient, $subject, $message, $headers))
        {
            echo "Sorry - an error occured trying to send the mail";
        }

    }

?>

This makes it all happen:

if($_SERVER["REQUEST_METHOD"] == "POST") {
     mail_it(stripslashes($content), $subject, $email, $recipient);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM