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



根据用户的选择,我有两个不同的文件(.pdf(要发送。在实践中,当用户选择"用户"时,他必须发送与该选择相关联的附件。通过在PHP文件中添加代码,可以进行提交,但不会重置表单。我选文件错了吗?只有数组"的文件;0";发送,即使选择了第二个

这是选择:

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

这是PHPMailer:

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
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 = "xF0x9Fx93x86 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,
});
}

我认为您在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();
}
}

您的JS负责执行此操作。您正在调用event.preventDefault();,但没有亲自执行任何清除表单的操作,因此不会发生任何事情。您需要告诉表单显式重置,例如在formSuccess()函数中调用此函数:

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

在PHP中,不要使用提交者的地址作为发件人地址。在那里使用您自己的地址,并将提交者的地址放在使用addReplyTo()的回复中。否则,它将是伪造的,你的信息可能不会被传递。

最新更新