[英]PhpMailer doesn't send Email with .pdf file attached
I want to send a file via PhpMailer. 我想通过PhpMailer发送文件。 If the file has extension .txt or .png ecc.. It sends me the email with the file (it works) but if I want to send a .pdf I don't receive the email ... this is a part of my PHP code: 如果文件的扩展名为.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>';
}
This is the JS script with ajax: 这是带有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');
}
});
This is the HTML file input part: 这是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>
It seems that It can't upload due to its size maybe ... because I uploaded .txt and .png of 50KB but the .pdf is 1MB 由于它的大小,似乎无法上传...因为我上传了50KB的.txt和.png文件,但.pdf为1MB
UPDATE: When I debug I can see that the file is uploaded , so I think that it don't send the email... why???? 更新:调试时,我可以看到文件已上传,所以我认为它不发送电子邮件...为什么?
Don't use values directly from the $_FILES
superglobal as they are potentially forgeable. 不要直接使用$_FILES
超全局变量中的值,因为它们可能是可伪造的。 You must validate them using the built-in move_uploaded_file
or is_uploaded_file
functions. 您必须使用内置的move_uploaded_file
或is_uploaded_file
函数来验证它们。 The PHP docs on handling file uploads are excellent, so do what they say. 有关处理文件上传的PHP文档非常出色,所以请按照他们说的做。
Base your code on the send file upload example provided with PHPMailer which uses move_uploaded_file
before using the file. 您的代码基于PHPMailer随附的发送文件上载示例,该示例在使用文件之前使用move_uploaded_file
。 It's also a good idea to not use user-supplied filenames so as to avoid the possibility of file overwrite attacks - in the example you'll see it uses a hash of the filename (which will always be safe) to avoid doing that. 最好不要使用用户提供的文件名,以避免发生文件覆盖攻击的可能性-在本示例中,您将看到它使用文件名的哈希(永远是安全的)来避免这样做。
It's a good idea to check return values from critical functions - addAttachment()
returns false if it can't read the file you ask it to read, for example: 检查关键函数的返回值是一个好主意-如果addAttachment()
无法读取您要求读取的文件,则返回false,例如:
if (!$mail->addAttachment($filename, 'file.pdf')) die('Could not read file!');
Look at the file upload size limit set in your PHP config, and mirror that in your form - see here and here . 查看您在PHP配置中设置的文件上传大小限制,并在您的表单中进行镜像-请参见此处和此处 。 If you exceed this limit, you may find that you still have an entry in your $_FILES
array, but it may point to an empty or non-existent file, which is another reason to validate and check return values. 如果超过此限制,您可能会发现$_FILES
数组中仍然有一个条目,但是它可能指向一个空文件或不存在的文件,这是验证和检查返回值的另一个原因。 You can var_dump($_FILES)
to see exactly what your script is receiving - it may not contain what you think it should. 您可以var_dump($_FILES)
确切地查看脚本正在接收的内容-它可能不包含您认为的内容。
I can see that you've based your code on a very old example, so make sure you're running the latest version of PHPMailer too. 我可以看到您的代码基于一个非常旧的示例,因此请确保您也正在运行最新版本的PHPMailer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.