Generating compliant MIME data (i.e. the email itself) is thirsty work, especially for non-ascii character sets. Swift Mailer caches data where it can so that when sending the same email to lot of recipients things run a little faster. There are two caching strategies offered by default in Swift Mailer. One uses a PHP array to hold cached data. The other writes data to disk. It would be easy to extend Swift to support a new cache type if you have a reason to cache data somewhere other than on disk or in memory.
Caching to disk is what Swift Mailer does by default, but only if it can find a temporary directory (i.e. if sys_get_temp_dir() is available). You can override its default caching strategy however.
Caching to disk offers one major advantage over the array cache - Memory usage! If you use the array cache Swift Mailer will save all data in memory which is a big waste. When using the disk cache Swift Mailer will perform file-streaming operations for complex algorithms such as encoding a large attachment file. On a PHP installation with an 8MB memory limit sending attachments would be very prohibited if disk caching was not available.
The following chart shows the memory used when sending attachments ranging in size from 0MiB to 100MiB. The blue line represents the Disk cache, and the red line represents the Array cache.
The code used to generate the benchmark results is shown below. It generates binary files full of random data, attaches them and sends them whilst checking the peak memory used.
<?php require_once '../../lib/swift_required.php'; //Using sendmail, but SmtpTransport should be just the same $mailer = new Swift_Mailer(new Swift_SendmailTransport('/usr/sbin/sendmail -bs')); //Define a cache type $type = 'array'; //Use the correct cache type Swift_Preferences::getInstance()->setCacheType($type); //Create a CSV log file $log = fopen($type . '-results.csv', 'w'); //Write headings fwrite($log, '"Attachment Size (MiB)","Memory (MiB)"' . "\n"); $increment = 1; for ($i = 0; $i <= 100; $i += $increment) { //Non-linear scale if ($i >= 10) $increment = 10; //Create a file full of random binary `dd if=/dev/random of=attach.ext bs=1048576 count=$i`; //Create a message with the random binary attached $message = Swift_Message::newInstance() ->setSubject('Swift Mailer Benchmark') ->setTo(array('chris@w3style.co.uk' => 'Chris Corbyn')) ->setFrom(array('chris@w3style.co.uk' => 'Chris Corbyn')) ->setBody("Benchmark test") ->attach(Swift_Attachment::fromPath('attach.ext')) ; //Send it with Sendmail $mailer->send($message); //Record result $usage = round( memory_get_peak_usage() / (1024 * 1024), 2); fwrite($log, sprintf("\"%s\",\"%s\"\n", $i, $usage)); printf("MEM @ {$i} MiB = %s MiB\n", $usage); } //Close the CSV fclose($log);