使用相同的 php 邮件程序脚本的多个表单

我正在处理 php 邮件,这里的邮件工作正常。我有多个表单与一个单一的 php 邮件功能合并,并且我有相同的表单字段。现在我正在尝试从哪个表单发送邮件我想在 Gmail 中添加一些字符串或标题,以便我可以理解这封邮件来自哪个表单。HTML


<form action="contact.php" method="POST" class="needs-validation form-horizontal" id="contact" novalidate>

    //here form fileds

</form>

邮件


<?php

// Receiver mail id 

$mail_to = 'abcd@gmail.com';


// Mail Subject 

$subject = 'test';


if ($_SERVER["REQUEST_METHOD"] == "POST") {


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

        $first_name = $_POST['first_name'];

    }

    // Message body


    $msg = '<html><body><p>';


    $msg .= '<b> First Name : </b>' . $first_name . '<br/>';


    $msg .= '</p>';

    $msg .= '</body></html>';


    // Mail headers

    $headers  = "MIME-Version: 1.0\r\n";

    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $headers .= 'From: demo1@gmail.com' . "\r\n";


    if( mail( $mail_to, $subject, $msg, $headers )) {

        echo "Thank You!";

    } else {

        die("Error!");

    }

   }

 ?>


慕森王
浏览 177回答 3
3回答

森林海

您可以将隐藏输入添加到您声明表单名称的表单中:<form action="contact.php" method="POST" class="needs-validation form-horizontal" id="contact" novalidate>&nbsp; &nbsp; //here form fileds&nbsp; &nbsp; <input id="source_form_name" name="source_form_name" type="hidden" value="Some name here"></form>然后通过电子邮件发送输入值:<?php// Receiver mail id&nbsp;$mail_to = 'abcd@gmail.com';// Mail Subject&nbsp;$subject = 'test';if ($_SERVER["REQUEST_METHOD"] == "POST") {&nbsp; &nbsp; if ( isset($_POST['first_name']) ) {&nbsp; &nbsp; &nbsp; &nbsp; $first_name = $_POST['first_name'];&nbsp; &nbsp; }&nbsp; &nbsp; // Message body&nbsp; &nbsp; $msg = '<html><body><p>';&nbsp; &nbsp; $msg .= '<b> First Name : </b>' . $first_name . '<br/>';&nbsp; &nbsp; $msg .= '</p>';&nbsp; &nbsp; $msg .= '<p>';&nbsp; &nbsp; $msg .= '<strong>Form Name: </strong>' . $_POST['source_form_name'];&nbsp; &nbsp; $msg .= '</p>';&nbsp; &nbsp; $msg .= '</body></html>';&nbsp; &nbsp; // Mail headers&nbsp; &nbsp; $headers&nbsp; = "MIME-Version: 1.0\r\n";&nbsp; &nbsp; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";&nbsp; &nbsp; $headers .= 'From: demo1@gmail.com' . "\r\n";&nbsp; &nbsp; if( mail( $mail_to, $subject, $msg, $headers )) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Thank You!";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; die("Error!");&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp;?>

眼眸繁星

只需添加一个隐藏字段,其中包含您要提交的表单的“名称”,例如:<form>&nbsp; <input type="hidden" name="formname" value="form1"/>&nbsp;</form>&nbsp;&nbsp;<form>&nbsp; <input type="hidden" name="formname" value="form2"/>&nbsp;</form>比您可以访问名称 $_POST['formname']

烙印99

首先在你的表单中创建一个隐藏输入,你可以设置表单名称,然后当你提交你的帖子时,你可以获取它的值并创建一个 swtich 案例来将一个值 os 字符串邮件传递给每个表单。如果您更需要它,您可以创建一个表格表格,每个表格都有一个 id、名称、string_mail,而不是在加载并提交表格时,您可以获得要发送的字符串邮件,这样您只需要插入一个数据库表,不需要 switch case,对于每个新表单,您需要在数据库中插入一个新值。希望能帮到你。
打开App,查看更多内容
随时随地看视频慕课网APP