来自功能的邮件显示为未知发件人?

时间:2018-02-19 15:06:37

标签: php html email-validation

我正在尝试显示联系表单上输入字段中的名称,以显示在提交电子邮件后输入的名称,我在php中更改了标题,并且它已从cgi-mailer更改为未知发件人?如何让它显示联系表格中电子邮件的人名?

由于

形成控制验证等的进程php

    // define variables and set to empty values
    $name_error = $email_error = "";
    $name = $email = $message = $success = "";
    $from = $_POST['name1'];

    //form is submitted with POST method
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name1"])) {
            $name_error = "Name is required";
        } else {
            $name = test_input($_POST["name1"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $name_error = "Only letters and white space allowed";
            }
        }

        if (empty($_POST["email"])) {
            $email_error = "Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $email_error = "Invalid email format";
            }
        }

        if (empty($_POST["message"])) {
            $message = "";
        } else {
            $message = test_input($_POST["message"]);
        }

        if ($name_error == '' and $email_error == '' ){
            $message_body = '';
            unset($_POST['submit']);
            foreach ($_POST as $key => $value){
                $message_body .=  "$key: $value\n";
            }

            $to = 'devonfoodmovement@gmail.com';
            $subject = 'Contact Form Submit';
            $headers = "From:" . $from;
            if (mail($to, $subject, $message, $headers)){
                $success = "Message sent";
                $name = $email = $message = '';
            }
        }

        /*if(isset($_POST['submit'])) {
            $from = $_POST['name1'];

            $headers = "From:" . $from;

        }*/

    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

Conotact form html

 <?php include('form_process.php');
        /*if (isset($_POST['contact-submit'])){

            $url = 'https://www.google.com/recaptcha/api/siteverify';
            $privatekey = "secretkeygoogle";

            $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADOR']);

            $data = json_decode($response);

            if(isset($data->success) AND $data->success==true) {

            }else{

            }


        }*/


    ?>
    <div class="home-btn"><a href="http://testsiteclash.co.uk/"><i class="fas fa-home"></i></a></div>
    <div class='grey'>
        <div class="container-contact">
            <form id="contact" method="post">
                <div id="column-contact-left">
                <div class='contact-logo'></div>
                <h3>Contact the Devon Food Movement</h3>
                <fieldset>
                    <input placeholder="Your name" type="text" tabindex="1" name="name1" value="<?= $name ?>" autofocus>

                </fieldset>
                <span class="error"><?= $name_error ?></span>
                <fieldset>
                    <input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2" >

                </fieldset>
                <span class="error"><?= $email_error ?></span>
                </div>
                <div id="column-contact-right">
                <fieldset>
                    <textarea placeholder="Type your Message Here...." name="message" value="<?= $message ?>" tabindex="3" ></textarea>
                </fieldset>
                <div class="g-recaptcha" data-sitekey="needtoinput" ></div>
                <span class="success"><?= $success; ?></span>
                <fieldset>
                    <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
                </fieldset>
                </div>
            </form>
        </div>
    </div>

新表格[rocess php with seperate mail function

    <?php


    // define variables and set to empty values
    $name_error = $email_error = "";
    $name = $email = $message = $success = "";

    //form is submitted with POST method
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name1"])) {
            $name_error = "Name is required";
        } else {
            $name = test_input($_POST["name1"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $name_error = "Only letters and white space allowed";
            }
        }

        if (empty($_POST["email"])) {
            $email_error = "Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $email_error = "Invalid email format";
            }
        }

        if (empty($_POST["message"])) {
            $message = "";
        } else {
            $message = test_input($_POST["message"]);
        }

        if ($name_error == '' and $email_error == '' ){
            $message_body = '';
            unset($_POST['submit']);
            foreach ($_POST as $key => $value){
                $message_body .=  "$key: $value\n";
            }

            $to = 'devonfoodmovement@gmail.com';
            $subject = 'Contact Form Submit';
            if (mail($to, $subject, $message, $headers)){
                $success = "Message sent";
                $name = $email = $message = '';
            }
        }

        /*if(isset($_POST['submit'])) {
            $from = $_POST['name1'];

            $headers = "From:" . $from;

        }*/

    }

    $from = $_POST['name1'];
    $headers = "From:" . $from;

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

    mail($headers);

    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

我做的最后一条评论的最新PHP

   if ($name_error == '' and $email_error == '' ){
        $message_body = '';
        unset($_POST['submit']);
        foreach ($_POST as $key => $value){
            $message_body .=  "$key: $value\n";
        }



        $email = $_POST['email'];
        $to = 'devonfoodmovement@gmail.com';
        $subject = 'Contact Form Submit';
        $headers = 'From:' . $email . "\n" . 'Reply-to: ' . $email . "\n"  ;
        if (mail($to, $subject, $message, $headers)){
            $success = "Message sent";
            $name = $email = $message = '';
        }
    }

2 个答案:

答案 0 :(得分:0)

您必须在表单上包含必填字段,如果需要,也可以询问名称。

   <?php
   function defaultMailing($to, $subject, $txt, $headers){
             if(mail($to, $subject, $txt, $headers)) return true; else return false;
    }
        ?>

在另一部分(表单行动的地方):

$to = $_POST['to'];
$subject = $_POST['subject'];
$txt = $_POST['message'];
//you don't need name, but if you asked for on the form:
$msg = "thanks, ".$_POST['name']." for your message, your request will be answered soon.";

//call to mail function and send the required (to, subject and text/html message) and the optional headers if you want
defaultMailing($to, $subject, $txt, $headers);

//do whatever you want with $msg or with $_POST['name'];

如果要在邮件中插入已发布的名称,只需修改$ txt var设置$ _POST ['name'],然后继续执行defaultMailing调用,例如:

$to = $_POST['to'];
$subject = $_POST['subject'];
$txt = $_POST['message'];

$name = $_POST['name'];    
$mailMessage = $name.' '.$txt;
//call to mail function and send the required (to, subject and text/html message) and the optional headers if you want
defaultMailing($to, $subject, $mailMessage, $headers);

答案 1 :(得分:0)

决定电子邮件是来自发件人,因为看似来自值必须是有效的电子邮件地址

            if ($name_error == '' and $email_error == '' and ($res['success']) ){
            $message_body = '';
            unset($_POST['submit']);

            foreach ($_POST as $key => $value){
                $message_body .=  "$key: $value\n";
            }

            $email = $_POST['email'];
            $to = 'devonfoodmovement@gmail.com';
            $subject = 'Contact Form Submit';
            $headers = 'From:' . $email . "\n" . 'Reply-to: ' . $email . "\n"  ;
            if (mail($to, $subject, $message, $headers)) {
                $sent = "Message sent";
                $name = $email = $message = '';
            }