PHPMailer - 附件已发送但表单未重置

PHPMailer - The attachment is sent but the form is not reset

根据用户制作的 selection,我有两个不同的文件 (.pdf) 要发送。实际上,当用户 select 发送 select 'user' 时,他必须发送与该 selection 关联的附件。通过在 PHP 文件中添加代码,可以提交但不会重置表单。 我做文件 selection 是不是错了?仅发送数组“0”的文件,即使第二个 select 是 selected

这是select:

<select name="user" id="user">
 <option disabled selected>Richiesta prestazione per</option>
 <option value="adulti">Adulti</option>
 <option value="minori">Minorenni</option>
</select>

这是PHP邮寄者:

    <?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true)
  
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_REQUEST['email'] ;
$phone = $_POST['phone'];
//$user = $_POST['user']; // i tried to disable this //
$pay = $_POST['pay'];
$message = $_REQUEST['message'];
$cod = mt_rand(100000, 999999);

//replacing them with this
$user = array (0 => './documents/test.pdf', 1 => './documents/test2.pdf');
$file = $user[(int) $_POST['user']];

try {
 //Server settings
 //$mail->SMTPDebug = 2; // Enable verbose debug output
 $mail->isSMTP(); // Set mailer to use SMTP
 $mail->CharSet = 'UTF-8';
 $mail->Host = '_MYMAIL_'; // Specify main and backup SMTP servers
 $mail->SMTPAuth = true; // Enable SMTP authentication
 $mail->Username = '_MYMAIL_'; // SMTP username
 $mail->Password = '_MYPASS_'; // SMTP password
 $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
 $mail->Port = 587; // TCP port to connect to

//Recipients
 $mail->setFrom($email, $name);
 $mail->addAddress('_MYMAIL_', '_MYNAME_'); // Add a recipient
 $mail->addReplyTo($email, $name);

// Content
 $mail->isHTML(true); // Set email format to HTML
 $mail->Subject = "\xF0\x9F\x93\x86 Richiesta di Consulenza Online";
 $mail->Body = _MYMESSAGE_;

 if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent';
}

$mail->ClearAddresses();
$mail->ClearaddReplyTo();

// Add the admin address
$mail->AddAddress($email);
$mail->setFrom('_MYMAIL_', '_MYNAME_');
$mail->Subject = _MYSUBJECT_;
$mail->addReplyTo('_MYMAIL_', '_MYNAME_');
$mail->AddAttachment($file) **//I add this to attach the .pdf file**
$mail->Body = 

<body>
 
   <h2>Codice prenotazione: '.$cod.'</h2>
 
   <table>
     <thead>
       <tr>
         <th>Nome</th>
         <th>Cognome</th>
         <th>Adulto/Minore</th>
         <th>Pagamento</th>
         <th>Telefono</th>
       </tr>
     </thead>
     <tbody>
       <tr>
         <td>'.$name.'</td>
         <td>'.$surname.'</td>
         <td>'.$user.'</td> //this is line 114
         <td>'.$pay.'</td>
         <td>'.$phone.'</td>
       </tr>
       <tr>
         <td colspan="5">Messaggio:<br><br>'.$message.'</td>
       </tr>
     </tbody>
   </table>
 </body>;

$mail->Send();


} catch (Exception $e) {
 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

这是文件 .js 中的调用 Ajax:

    $("#formBook").submit(function (event) {
    // cancels the form submission
    event.preventDefault();
    submitForm();
  });
  
  function submitForm() {
     //Initiate Variables With Form Content
     var name = $("#name").val();
     var surname = $("#surname").val();
     var email = $("#email").val();
     var phone = $("#phone").val();
     var user = $("#user").val();
     var message = $("#message").val();
     var pay = $("#pay").val();
  
    $.ajax({
      type: "POST",
      url: "email_monia.php",
      data: "name=" + name + "&email=" + email + "&message=" + message + "&user=" + user + "&phone=" + phone + "&pay=" + pay + "&surname=" + surname,
      success: function (data) {
        if (data == 'Message sent') {
          formSuccess();
        }
      }
    });
  }

function formSuccess() {
$("#formBook")[0].reset();
// custom toast
iziToast.show({
  color: 'light',
  icon: 'fas fa-paper-plane',
  message: '<strong>Grazie!</strong></br>Il tuo messaggio è stato inviato',
  messageLineHeight: 20,
  messageSize: 15,
  position: 'center', // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter
  progressBarColor: '#00909D',
  backgroundColor: ' #9ad3ca',
  titleColor: '#222',
  messageColor: '#222',
  iconColor: '#F7F7F7',
  closa: false,
  displayMode: 'once',
  pauseOnHover: false,
  resetOnHover: false,
});
}

您的 JS 负责执行此操作。您正在调用 event.preventDefault();,但是您自己没有做任何事情来清除表格,所以什么也没有发生。你需要明确地告诉表单重置,例如通过在你的 formSuccess() 函数中调用它:

document.getElementById("formBook").reset();

在您的 PHP 中,不要使用提交者的地址作为发件人地址。在那里使用您自己的地址,并使用 addReplyTo() 将提交者的地址放在回复中。否则将是伪造的,您的消息可能无法发送。

我认为您在 user 字段而不是索引中提交名称,在这种情况下,您应该将附件数组更新为关联数组

//replacing them with this
$user = array ( 
    'adulti' => './documents/test.pdf',
    'minori' => './documents/test.pdf'
);
$file = $user[$_POST['user']];

formSuccess函数中

$("#formBook").reset();

你能验证你的回答吗

$.ajax({
  type: "POST",
  url: "email_monia.php",
  data: "name=" + name + "&email=" + email + "&message=" + message + "&user=" + user + "&phone=" + phone + "&pay=" + pay + "&surname=" + surname,
  success: function (data) {
    // check response here
    console.log(data);
    if (data == 'Message sent') {
      formSuccess();
    }
  }