简体   繁体   English

HTML电子邮件表格PHP

[英]HTML email form PHP

I have a HTML form that works with PHP code, and i get the emails sent to me, however, i can get an email if there is no subject, no email and no message. 我有一个适用于PHP代码的HTML表单,并且我收到了发送给我的电子邮件,但是,如果没有主题,没有电子邮件也没有消息,我可以收到一封电子邮件。 Maybe some one know how to fix that? 也许有人知道该如何解决? Here's the PHP code: 这是PHP代码:

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'benasleng@gmail.com';
$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) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to benasleng@gmail.com');
        window.location = 'index.html';
    </script>
<?php
}
?>
<?php
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;
    }
}

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

before calling mail() function, you should check if fields are compiled . 在调用mail()函数之前,应检查字段是否已编译。

This is just a draft example because i suggest you to use more complex checks than just empty() function, expecially on $field_email, for example: 这只是一个草稿示例,因为我建议您使用比empty()函数更复杂的检查,尤其是在$ field_email上,例如:

if (empty($field_name) || !filter_var($field_email, FILTER_VALIDATE_EMAIL) || empty($field_message)) {
  ?>
    <script language="javascript" type="text/javascript">
        alert('All fields must be correctly compiled');
        window.location = 'index.html';
    </script>
<?php

} else {


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

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to benasleng@gmail.com');
        window.location = 'index.html';
    </script>
<?php
}
}

Try adding headers and using the mail() function: 尝试添加标题并使用mail()函数:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Mail <mail@mail.someadress.com>';

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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