PHPMailer Attachment Code

I am using PHPMailer to send files to users that they select with a checkbox form. They can select up to 5 files to be sent. The files are assigned to variables called $chart0, $chart1, $chart2, $chart3 and $chart4. Currently my code just attaches all 5 variables to the email like this:

[php]$mail->AddAttachment($chart0);
$mail->AddAttachment($chart1);
$mail->AddAttachment($chart2);
$mail->AddAttachment($chart3);
$mail->AddAttachment($chart4);[/php]

If the user only selects 3 items, for example, the email is still sent ok with the proper attachments, but the code displays a page that says “Could not access file: Could not access file:” for the 2 attachments that had no values.

What is the best way to have the code only attach the variables that have a filename associated with them?

Thanks for your help,

DLew

before you recieve the selected values assign them empty strings.

$chart0 = $chart1 = $chart2 = $chart3 = $chart4 = “”;

Runman,

Thanks for the suggestion. I actually got it working shortly before you posted this by using the “!empty” function with each variable and only attaching them if they have an actual value assigned to them.

Thanks,

DLew

Sponsor our Newsletter | Privacy Policy | Terms of Service