首页 > 解决方案 > PHPMailer,附件文件问题

问题描述

我正在使用 PHPMailer 发送一个 PDF 文档,它运行良好,但我想在我的表单中添加一个输入文件以发送 2 个附件,我的意思是,接收 PDF 和我在输入文件中上传的文件。出于某种原因,我没有得到输入文件。

当我发送 PDF 时它可以工作,但是当我尝试通过表单中的文件输入发送另一个附件时不起作用。

我收到的错误是:

*第 150 行未定义索引:foto // 有代码 $file = $ _FILES ['foto'] ['tmp_name'];

*第 150 行尝试访问 null 类型值的数组偏移量。

*第 187 行未定义变量:file // 有代码 $mail-> addAttachment ($file, $_FILES ['foto'] ['name']);

*第 187 行未定义索引:照片

*第 187 行尝试访问 null 类型值的数组偏移量

请有人帮我解决这个问题!

数据> 我使用 Emailtrap 接收电子邮件。我的表单的输入文件有 name = '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}";
}

}

谢谢你的时间。

标签: phpphpmailer

解决方案


看起来您的表单没有名称,并且您正在使用foto. 添加name="foto"到您的 html 表单,然后重试。


推荐阅读