/** * @param Campaign $campaign * @param phpListMailer $mail * @param string $type */ private static function addAttachments($campaign, &$mail, $type) { if (Config::ALLOW_ATTACHMENTS) { $attachments = $campaign->getAttachments(); //if (empty($attachments)) // return; if ($type == "text") { $mail->append_text(s('This campaign contains attachments that can be viewed with a webbrowser:') . "\n"); } /** * @var Attachment $attachment */ foreach ($attachments as $attachment) { $file = Config::ATTACHMENT_REPOSITORY . '/' . $attachment->filename; switch ($type) { case "HTML": if (is_file($file) && filesize($file)) { $fp = fopen($file, "r"); if ($fp) { $contents = fread($fp, filesize($file)); fclose($fp); $mail->add_attachment($contents, basename($attachment->remotefile), $attachment->mimetype); } } elseif (is_file($attachment->remotefile) && filesize($attachment->remotefile)) { # handle local filesystem attachments $fp = fopen($attachment->remotefile, 'r'); if ($fp) { $contents = fread($fp, filesize($attachment->remotefile)); fclose($fp); $mail->add_attachment($contents, basename($attachment->remotefile), $attachment->mimetype); list($name, $ext) = explode('.', basename($attachment->remotefile)); # create a temporary file to make sure to use a unique file name to store with $newfile = tempnam(Config::ATTACHMENT_REPOSITORY, $name); $newfile .= "." . $ext; $newfile = basename($newfile); $fd = fopen(Config::ATTACHMENT_REPOSITORY . '/' . $newfile, 'w'); fwrite($fd, $contents); fclose($fd); # check that it was successful if (filesize(Config::ATTACHMENT_REPOSITORY . '/' . $newfile)) { $attachment->filename = $newfile; $attachment->update(); } else { # now this one could be sent many times, so send only once per run if (Config::get($attachment->remotefile . '_warned', false) === false) { phpList::log()->notice("Unable to make a copy of attachment {$attachment->remotefile} in repository"); $msg = sprintf('Error, when trying to send campaign %d the filesystem attachment %s could not be copied to the repository. Check for permissions.', $campaign->id, $attachment->remotefile); phplistMailer::sendMail(Config::get('report_address'), 'Mail list error', $msg, ''); Config::setRunningConfig($attachment->remotefile . '_warned', time()); } } } else { phpList::log()->notice("failed to open attachment {$attachment->remotefile} to add to campaign {$campaign->id}"); } } else { phpList::log()->notice("Attachment {$attachment->remotefile} does not exist"); $msg = "Error, when trying to send campaign {$campaign->id} the attachment {$attachment->remotefile} could not be found"; phpListMailer::sendMail(Config::get('report_address'), 'Mail list error', $msg, ''); } break; case "text": $viewurl = Config::get('public_scheme') . "://" . Config::get('website') . Config::PAGEROOT . '/dl.php?id=' . $attachment->id; $mail->append_text($attachment->description . "\n" . s('Location') . ": " . $viewurl . "\n"); break; } } } }