Jump to content
ianhaney

phpmailer attachment help

Recommended Posts

I am new to phpmailer and still trying to learn and understand it, I can do the basics in it if want to just use it to send a email but now want to use it to send attachments but the attachments are optional and only want to be able to send jpg and png attachments but am trying to concentrate on one thing at a time but getting frustrated with it. I am currently trying to get it so that the attachments are not required so if the user sends the form with no attachments, the form sends all ok and the same as if the user sends attachments, below is my current code but is not working, am getting unexpected errors as either ; or }

 

$attachmentNames = [];
    
    //Attach multiple files one by one
    for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));        
        $filename = $_FILES['userfile']['name'][$ct];
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
            $attachmentNames[] = $_FILES['userfile']['name'][$ct];
        } else {
            $msg .= 'Failed to move file to ' . $uploadfile;
        }
        if ($mail->addAttachment {
            $msg .= 'Failed to attach file'
        }
    }

    $mail->Subject = 'New Repair Booking Made At' . ' ' .date('d-m-Y H:i:s');
            
    $mail->Body    = "A new repair has been booked in. This repair booking was made at " . $date = date('d-m-Y H:i:s') . "\r\n\r\n" . "The repair information is below" . "\r\n\r\n" . "Repair Date/Time: " . $_POST["date"] . "\r\n" . "Device: " . $_POST["selectdevice"] . "\r\n" . "Brand: " . $_POST["selectbrand"] . "\r\n" . "Name: " . $_POST["name"] . "\r\n" . "Email: " . $_POST["email"] . "\r\n" . "Phone Number: " . $_POST["phone"] . "\r\n" . "Repair Description/Issue: " . $_POST["repairdescription"] . "\r\n\r\n" . "Attached Filename(s): " . implode(', ', $attachmentNames);

 

Share this post


Link to post
Share on other sites

Just solved the above issue with the code below

 

//Attach multiple files one by one
    for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
        $filename = $_FILES['userfile']['name'][$ct];
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
            $attachmentNames[] = $_FILES['userfile']['name'][$ct];
        } else {
            $msg .= 'Failed to move file to ' . $uploadfile;
        }
        if (file_exists($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
        }
    }

 

Share this post


Link to post
Share on other sites

Just noticed with the above code, the form works and submits if there is no attachment or if I do upload a attachment but I get a error log with the following error

PHP Warning:  file_exists() expects exactly 1 parameter, 2 given

How should the code be as getting confused on it now

Share this post


Link to post
Share on other sites

Sorry just managed to solve it with the following code and not got any errors

 

//Attach multiple files one by one
    for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
        $filename = $_FILES['userfile']['name'][$ct];
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            if(isset($uploadfile))
            $mail->addAttachment($uploadfile, $filename);
            $attachmentNames[] = $_FILES['userfile']['name'][$ct];
        } else {
            $msg .= 'Failed to move file to ' . $uploadfile;
        }
    }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.