使用 PHPmailer 添加到邮件脚本:缺少字段验证、重定向

Adding to mail script using PHPmailer: missing fields validation, redirects

曾几何时,我有一个使用旧版本 PHPmailer 的邮件脚本的运行良好的版本。在不知道 PHP 的情况下,我设法将它推得相当远(部分是通过使用 Forms to Go 和做一些修改),并让它重定向到自定义 "incomplete" 和 "thank you" 页面, 如果未填写必填字段,则让它拒绝发送。

我不得不重新开始设计,因为现在通过 Gmail 路由基于域的电子邮件已成为一种非常主流的做法。为了到达那里,我更新到最新的 PHPmailer,经过大量的反复试验和研究,终于让它可以用于 Gmail 设置。但在这期间,我失去了一些功能。下面的脚本可以发送邮件——但即使必填字段(实际上,所有内容!)都是空的,它也会发送。所以它需要:

  1. 当字段填写不正确时停止发送,并且
  2. 希望重定向到我的自定义页面,而不是只显示一般消息。

<?php

// CUSTOM: collect data from our web form
$name = $_REQUEST['name'];
$address = $_REQUEST['address'];
$tel = $_REQUEST['tel'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

//set required fields + redirect if incomplete


//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');

require 'mailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;


//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
// Gmail pulls from custom domain due to alteration of MX records
$mail->Host = 'server.mydomain.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "me@myemailaddress.com";

//Password to use for SMTP authentication
$mail->Password = "mypassword";

//Set who the message is to be sent from
$mail->setFrom('me@myemailaddress.com', 'web form');

//Set who the message is to be sent to
$mail->addAddress('me@myemailaddress.com', 'My Name');

//Set an alternative reply-to address
$mail->clearReplyTos();
$mail->addReplyTo($address);

//Set the subject line
$mail->Subject = $subject;

 $mail->Body = "Name : $name\n\n"
  . "Email : $address\n"
  . "Telephone : $tel\n"
  . "Message :\n\n $message\n"
  . "";


//send the message, check for errors
if (!$mail->send()) {
      $output .= "Mailer Error: ". $mail->ErrorInfo;
    }

    else
    {
       ob_clean();
       header('Location: thankyou.php');
       exit();
    }
    echo $output;

任何人都可以帮助我了解如何编辑它以获得该功能吗?

蜜罐也是一种奖励,但也许这要求太高了。 [编辑:看起来这个线程中的蜜罐解决方案会起作用: ...明天测试。]

            <?php
            /**

             */
             if( !isset($_REQUEST['name'],$_REQUEST['address'],$_REQUEST['subject'],$_REQUEST['message']) 
||

( !$_REQUEST['name'] || !$_REQUEST['address'] || !$_REQUEST['subject'] || !$_REQUEST['message'])

){
                    /** 
                     * It means that the required field 
                     * is not filled up all
                     * if any one filed is not required, then remove that from the above condition the whole $_REQUEST['var_not_required']
                     */

                     /**
                      * you can display a error message or redirect       
                      * 1. Error message
                      * 2. Redirect
                      */

                      #1
                      die("Some of the fields are not filled up properly");

                      #2
                      header('location: http://yoururl');
                      /**
                       * Use exit to stop execution of the rest, to prevent sending or attempt to send
                       * with exit
                       */
                       exit();


             }
            $name = $_REQUEST['name'];
            $address = $_REQUEST['address'];
            $tel = $_REQUEST['address'];
            $subject = $_REQUEST['subject'];
            $message = $_REQUEST['message'];