PhpMailer 不发送附有 .pdf 文件的电子邮件

PhpMailer doesn't send Email with .pdf file attached

我想通过 PhpMailer 发送文件。如果文件的扩展名为 .txt 或 .png ecc.. 它会向我发送包含该文件的电子邮件(它有效)但是如果我想发送 .pdf 我不会收到电子邮件......这是我的一部分PHP代码:

$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Surname = $_POST['Surname'];
$Residence = $_POST['Residence'];
$Phone = $_POST['Phone'];

$Name = filter_var($Name, FILTER_SANITIZE_STRING);
$Surname = filter_var($Surname, FILTER_SANITIZE_STRING);
$Email = filter_var($Email, FILTER_SANITIZE_EMAIL);
$Residence = filter_var($Residence, FILTER_SANITIZE_STRING);;
$Phone = filter_var($Phone, FILTER_SANITIZE_STRING);;

$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPSecure = "ssl";
$mail->Username = "xxx"; //account with which you want to send mail. Or use this account. i dont care :-P
$mail->Password = "xxx"; //this account's password.
$mail->SetFrom('xxx');
$mail->Port = "465";
$mail->isSMTP();  // telling the class to use SMTP
$rec1="xxx"; //receiver. email addresses to which u want to send the mail.
$mail->AddAddress($rec1);
$mail->Subject  = "Eventbook";
$mail->isHTML(true);
$mail->Body     = "<h1>Contact from Website</h1>
<ul>
<li>Nome: {$Name}</li>
<li>Cognome: {$Surname}</li>
<li>Comune di residenza: {$Residence}</li>
<li>Email: {$Email}</li>
<li>Telefono: {$Phone}</li>
</ul>";
$mail->WordWrap = 200;
$mail->addAttachment($_FILES['curriculum']['tmp_name'], $_FILES['curriculum']['name']);
if(!$mail->Send()) {
echo 'Message was not sent!.';
$errore = $mail->ErrorInfo;
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo  //Fill in the document.location thing
'<script type="text/javascript">
                        if(confirm("Your mail has been sent"))
                        document.location = "/";
        </script>';
}

这是ajax的JS脚本:

var formData = new FormData();
        formData.append('Name', $('#Name').val());
        formData.append('Surname', $('#Surname').val());
        formData.append('Residence', $('#Residence').val());
        formData.append('Email', $('#Email').val());
        formData.append('Phone', $('#Phone').val());
        formData.append('Curriculum', $('#Curriculum')[0].files[0]);
        $.ajax({
            method: 'POST',
            url: "scripts/register.php",
            data: formData,
            processData: false,
            contentType: false,
            success: function (Curriculum) {
                alert('Success');
            }
        });

这是HTML文件输入部分:

<input type="file" name="Curriculum" id="Curriculum" style="display: none;" class="form__input" />
           <label for="Curriculum" id="LabelCurriculum" class="form__input" style="background-color: white; display: block; width: 100%; padding: 20px; font-family: Roboto; -webkit-appearance: none; border: 0; outline: 0; transition: 0.3s;">Click to upload</label>

似乎由于它的大小而无法上传...因为我上传了 50KB 的 .txt 和 .png 但 .pdf 是 1MB

更新:调试时我可以看到文件已上传,所以我认为它没有发送电子邮件...为什么????

不要使用直接来自 $_FILES 超全局的值,因为它们可能是可伪造的。您必须使用内置的 move_uploaded_fileis_uploaded_file 函数来验证它们。 PHP docs on handling file uploads人很优秀,说到做到。

在使用文件之前,将您的代码基于使用 move_uploaded_filesend file upload example provided with PHPMailer。不使用用户提供的文件名以避免文件覆盖攻击的可能性也是一个好主意 - 在示例中,您将看到它使用文件名的哈希值(这将始终是安全的)来避免这样做。

检查关键函数的 return 值是个好主意 - 如果它无法读取您要求它读取的文件,则 addAttachment() returns false,例如:

if (!$mail->addAttachment($filename, 'file.pdf')) die('Could not read file!');

查看您的 PHP 配置中设置的文件上传大小限制,并将其反映在您的表单中 - 请参阅 here and here。如果超过此限制,您可能会发现 $_FILES 数组中仍有一个条目,但它可能指向一个空文件或不存在的文件,这是验证和检查 return 的另一个原因值。您可以 var_dump($_FILES) 准确查看您的脚本接收到的内容 - 它可能不包含您认为应该包含的内容。

我可以看出您的代码基于一个非常古老的示例,因此请确保您也是 运行 最新版本的 PHPMailer。