简体   繁体   English

如何在表单提交PHP上向用户发送响应电子邮件

[英]How to send response email to user on form submit PHP

I have a rather basic php form that submits on the same page it is called "registration.php". 我有一个相当基本的php表单,它在同一页面上提交,称为“ registration.php”。 I have it set to collect the form data, store it in the $to, $subject and $body variables then I call the mail() method to send off an email to myself. 我将其设置为收集表单数据,将其存储在$ to,$ subject和$ body变量中,然后调用mail()方法向自己发送电子邮件。

please see code below ( Don't worry! there is a question comin' right up! ): 请参见下面的代码(不用担心!马上就会出现一个问题!):

<?php
ob_start(); //output buffering because I like it.

if (isset($_POST['submit'])) {
  // Process the form
  $message = "Thank you for registering! We will respond to your request shortly";
  $name = $_POST['name'];
  $email = $_POST['email'];
  $address = $_POST['address'];
  $comments = $_POST['userComment'];
  $date = gmdate("M d Y");


$to = "myemail@outlook.com";
$subject = "Registration Submission";
$body = " Date: $date \n Registrant Name/Name's: $name \n Registrant E-mail: $email \n \n User Comments: \n $comments \n \n";

mail($to,$subject,$body);

}

?>
<form id="contactForm" name="contactForm" action="registration.php" method="post">
                <table>
                    <tr>
                        <td><label for="name"><strong>Registrant Name / Names</strong></label></td>
                        <td><input required='required' type="text" id="name" name="name" /></td>
                    </tr>
                    <tr>
                        <td><label for="email"><strong>Registrant E-mail</strong></label></td>
                        <td><input required='required' type="email" maxlength="40" id="email" name="email" /></td>
                    </tr>
                    <tr>
                        <td><label for="subject"><strong>Registrant Address</strong></label></td>
                        <td><input required='required' type="text" maxlength="100"  id="address" name="address" /></td>
                    </tr>
                    <tr>
                        <td>Message&#58;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td id="textAreaInput" colspan="2"><textarea  rows="3" id="userComment" name="userComment" placeholder="Let us know your thoughts!"></textarea></td>
                    </tr>
                </table>
                <br />
                <button type="submit" name="submit" id="submit">Submit</button>
            </form>

What I want to know is Can I send a custom response email to the users collected $email with a custom message using just php? 我想知道的是,我可以仅使用php将自定义响应电子邮件发送给带有自定义消息的用户收集的$ email吗? can I send other variables through the mail() method? 我可以通过mail()方法发送其他变量吗? like $to2, $subject2, $body2 or must they be named $to, $subject & $body when passed? 像$ to2,$ subject2,$ body2,还是通过时必须将它们命名为$ to,$ subject和$ body?

I have a working solution using PHPMailer found here: 我在这里找到了使用PHPMailer的有效解决方案:

https://github.com/PHPMailer/PHPMailer

I am just curious as to weither or not there is an easy process using PHP that wouldn't rely on a required library. 我只是好奇是否有一个使用PHP的简单过程而不依赖于所需的库。

I think all you have to do is 我想你要做的就是

if(mail($to,$subject,$body)){
   mail($email,"Thanks!!","Thank you very much for registering!!!")
}

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

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