Search code examples
phpphpmailer

PHPMailer send multiple attachments


HI im trying to send multiple attachment with php mailer but only one attachment being sent even both files are uploaded and path are correct below is code. in second iteration $mail->addAttachment return false.

if  (is_array( $email_attachment )) {

    foreach ($email_attachment as $attachment ) {

        $mail->addAttachment($attachment ); 

        var_dump($attachment);
        echo '<br/>';  

        var_dump($mail->addAttachment($attachment));
        echo '<br/>'; 
    }

     die();
}

dump result.

string(62) "D:\htdocs\express-english/wp-content/uploads/2016/10/23882.jpg" 
bool(true) 
string(63) " D:\htdocs\express-english/wp-content/uploads/2016/10/97778.jpg" 
bool(false)

Solution

  • Your 2nd string contains a space in front of it

    string(63) " D:\htdocs\express-english/wp-content/uploads/2016/10/97778.jpg" 
    

    you should trim that with

    $attachment = trim($attachment);
    

    before using $mail->addAttachment