簡體   English   中英

如何在表單提交PHP上向用戶發送響應電子郵件

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

我有一個相當基本的php表單,它在同一頁面上提交,稱為“ registration.php”。 我將其設置為收集表單數據,將其存儲在$ to,$ subject和$ body變量中,然后調用mail()方法向自己發送電子郵件。

請參見下面的代碼(不用擔心!馬上就會出現一個問題!):

<?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>

我想知道的是,我可以僅使用php將自定義響應電子郵件發送給帶有自定義消息的用戶收集的$ email嗎? 我可以通過mail()方法發送其他變量嗎? 像$ to2,$ subject2,$ body2,還是通過時必須將它們命名為$ to,$ subject和$ body?

我在這里找到了使用PHPMailer的有效解決方案:

https://github.com/PHPMailer/PHPMailer

我只是好奇是否有一個使用PHP的簡單過程而不依賴於所需的庫。

我想你要做的就是

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