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

时间:2018-07-11 07:43:11

标签: php phpmailer email-attachments

我想通过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

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

1 个答案:

答案 0 :(得分:0)

请勿直接使用来自$_FILES超全局变量的值,因为它们可能是可伪造的。您必须使用内置的move_uploaded_fileis_uploaded_file函数来验证它们。 PHP docs on handling file uploads非常好,他们说的也一样。

在使用文件之前,将代码基于使用move_uploaded_file的{​​{3}}。最好不要使用用户提供的文件名,以避免发生文件覆​​盖攻击的可能性-在该示例中,您将看到它使用文件名的哈希(永远是安全的)来避免这样做。 / p>

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

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

查看在PHP配置中设置的文件上传大小限制,并在您的表单中进行镜像-请参见send file upload example provided with PHPMailerhere。如果超出此限制,则可能会发现$_FILES数组中仍然有一个条目,但是它可能指向空文件或不存在文件,这是验证和检查返回值的另一个原因。您可以var_dump($_FILES)确切地查看脚本收到的内容-它可能不包含您认为的内容。

我可以看到您的代码基于一个非常旧的示例,因此请确保您也正在运行最新版本的PHPMailer。