Ragnar
Ragnar

Reputation: 171

PHPMailer, prroblems with attachment files

I am sending a PDF document with PHPMailer and it works perfectly, but I want to add an Input File to my form to send the 2 Attachments, I mean, to receive the PDF and the file that I upload in Input File. For some reason I am not getting the Input File.

When I send the PDF it works but when I try to send anothe attachment by the File Input in my form doesnt work.

The errors I receive are:

*Line 150 Undefined index: foto // there is the code $ file = $ _FILES ['foto'] ['tmp_name'];

*Line 150 Trying to access array offset on value of type null.

*Line 187 Undefined variable: file // there is the code $ mail-> addAttachment ($ file, $ _ FILES ['foto'] ['name']);

*Line 187 Undefined index: foto

*Line 187 Trying to access array offset on value of type null

Someone give me a hand with this problem please!

Data> I use Emailtrap to receive emails. Input File of my form has as name = 'foto', My form starts like this:

<form class="nobottommargin" method="post" id="bolsa" enctype="multipart/form-data" action="formpdf.php" >...

And my formpdf.php file is like this:

$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}";
}

}

Thanks for your time.

Upvotes: 1

Views: 317

Answers (1)

khukho
khukho

Reputation: 476

It looks like your form doesn't have a name and you are accessing with the name of foto. Add name="foto" to your html form and please try again.

Upvotes: 1

Related Questions