【发布时间】:2020-12-05 12:27:14
【问题描述】:
我正在使用 PHPMailer 发送一个 PDF 文档,它运行良好,但我想在我的表单中添加一个输入文件以发送 2 个附件,我的意思是,接收 PDF 和我在输入文件中上传的文件。由于某种原因,我没有得到输入文件。
当我发送 PDF 时它可以工作,但是当我尝试通过表单中的文件输入发送另一个附件时不起作用。
我收到的错误是:
*Line 150 Undefined index: foto // 有代码 $file = $ _FILES ['foto'] ['tmp_name'];
*第 150 行尝试访问 null 类型值的数组偏移量。
*第 187 行未定义变量:file // 有代码 $mail-> addAttachment($file, $_FILES ['foto'] ['name']);
*第 187 行未定义索引:照片
*第 187 行尝试访问 null 类型值的数组偏移量
请有人帮我解决这个问题!
数据> 我使用 Emailtrap 接收电子邮件。 我表单的输入文件名称为“foto”, 我的表单是这样开始的:
<form class="nobottommargin" method="post" id="bolsa" enctype="multipart/form-data" action="formpdf.php" >...
而我的formpdf.php文件是这样的:
$file= $_FILES['foto']['tmp_name'];
//Corriendo la funcion
sendEmail($pdf,$enquirydata);
//Function to send the information
function sendEmail($pdf,$enquirydata){
$emailbody='';
$emailbody .= '<h1>Email recibido de '. $enquirydata['Fnombre'].'</h1>';
$emailbody .=' <h3>Ver documentos Adjuntos</h3>';
//Para enviar el mensaje en el cuerpo
// foreach ($enquirydata as $title=> $data){
// $emailbody .= '<strong>'. $title . '</strong>: '. $data . '<br />';
// }
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = false; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.mailtrap.io'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'f4f540749a12b0'; // SMTP username
$mail->Password = '20097ae51199a2'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 2525; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('Test@example.com', 'PHPMailer');
$mail->AddAddress('maguilar@verdehn.com', 'Solicitud Empleo');
$mail->addStringAttachment($pdf, 'solicitud.pdf');
$mail->addAttachment($file,$_FILES['foto']['name']);
// Content
$mail->isHTML(true);
$mail->Subject = 'Enquiry from' . $enquirydata['Fnombre'];
$mail->Body = $emailbody;
$mail->AltBody = strip_tags($emailbody);
$mail->send();
header('Location:index.php');
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
感谢您的宝贵时间。
【问题讨论】:
-
首先调试您的表单提交,然后然后担心发送它。你忽略了PHP's docs on handling file uploads safely(所以你的代码不安全),你也没有看过the file upload example provided with PHPMailer。您可能还需要更改您在 mailtrap 上的密码,因为您似乎已经公开发布了密码。