1. Go to this page and download the library: Download sfaut/zimbra library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$buffer = 'Contents that will be attached to a file';
$stream = fopen('/path/to/file.csv', 'r');
$attachments = [
['basename' => 'data-1.csv', 'file' => '/path/to/data.csv'],
['basename' => 'data-2.txt', 'buffer' => $buffer],
['basename' => 'data-3.csv', 'stream' => $stream],
];
$zimbra->send($addresses, $subject, $body, $attachments);
// ⚠️ YOU SHOULD NOT DO THIS
// The same file uploaded 3 times for 3 messages
$attachments = [
['basename' => 'decennial-data.csv', 'file' => '/path/to/decennial-data.csv'],
['basename' => 'another-data.csv', 'file' => '/path/to/another-data.csv'],
];
$zimbra->send($addresses_1, $subject, $body, $attachments); // 🙅🏻♂️
$zimbra->send($addresses_2, $subject, $body, $attachments); // 🙅🏻♂️
$zimbra->send($addresses_3, $subject, $body, $attachments); // 🙅🏻♂️
// 💡 YOU SHOULD DO THAT
// 1 upload for 3 messages
$attachments = [
['basename' => 'decennial-data.csv', 'file' => '/path/to/decennial-data.csv'],
['basename' => 'another-data.csv', 'file' => '/path/to/another-data.csv'],
];
// 🥂 That's the trick
// An attachment ID is retrieved and reused as necessary
$attachments = $zimbra->upload($attachments);
$zimbra->send($addresses_1, $subject, $body, $attachments);
$zimbra->send($addresses_2, $subject, $body, $attachments);
$zimbra->send($addresses_3, $subject, $body, $attachments);
{
part: <MIME part of the attachment in the message, eg. "2" or "2.1.1">
disposition: <Attachment method to the message : "attachment" or "inline">
type: <MIME type of the attachment file, eg. "text/plain", "text/csv">
size: <Attachement file size in bytes>
basename: <Attachement file name with extension, eg. "my-data.csv">
filename: <Attachment file name without extension, eg. "my-data">
extension: <Attachment extension without dot, eg. "csv">
stream: <Stream from temporary, only after Zimbra::download() call>
}
$message = $zimbra->search(['subject' => 'Annual result 2022'])[0]; // Get 1 message
$attachment = $zimbra->download($message)[0]; // Get 1 attachment
// Where you want to save your file
$destination_file = '/path/to/' . $attachment->basename;
// 1st method, with stream
// Memory efficient
$destination_stream = fopen($destination_file, 'w');
stream_copy_to_stream($attachment->stream, $destination_stream);
// 2nd method, with buffer
// Memory inefficient on huge files, but you can process $buffer
$buffer = stream_get_contents($attachment->stream);
file_put_contents($destination_file, $buffer);
$message = $zimbra->search(['subject' => 'Summer 2022 holidays photos'])[0];
// Your filter closure
// You need to keep only images attachments
$filter = fn ($attachment) => strpos($attachment->type, 'image/') === 0;
$attachments = $zimbra->download($message, $filter);
foreach ($attachments as $attachment) {
// Save the downloaded attachments / photos to a safe place
$stream_destination = fopen('/home/me/holidays/' . $attachment->basename, 'w');
stream_copy_to_stream($attachment->stream, $stream_destination);
}
use sfaut\Zimbra;
s.php';
// Starting attachment, you choose an all upper case reference
$starting_file = 'REPORT 2020-01-01.CSV';
// Mailbox source folder
$source_folder = '/Inbox/Reports';
// Locale target subdirectory, where all CSV attachments will be downloaded
$target_directory = '/path/to/mailbox/reports';
$zimbra = Zimbra::authenticate($host, $user, $password);
// Search messages in source folder that have at least one CSV attachment begining with "Report"
// You reduce unuseful messages retrieving
$messages = $zimbra->search(['in' => $source_folder, 'filename' => 'Report*', 'type' => 'text/csv']);
foreach ($messages as $message) {
// Download attachments, only what you need
$attachments = $zimbra->download($message, function ($attachment) {
if (strtoupper($attachment->extension) !== 'CSV') {
return false;
}
if (strtoupper($attachment->basename) < $starting_file) {
return false;
}
return true;
});
// Save CSV attachments in compressed files in safe place
foreach ($attachments as $attachment) {
$target_file = "{$target_directory}/{$message->subject} -- {$attachment->basename}.gz";
$target_stream = gzopen($target_file, 'w');
stream_copy_to_stream($attachment->stream, $target_stream);
}
}
exit(0);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.