Skip to content

Commit

Permalink
Try to help PHPMailer#1824
Browse files Browse the repository at this point in the history
$ php benchmark_reduce_mime_encoding.phps
...
Size	Method1	Method2	Method3	Method4
1.8kb	0.0646	0.1114	0.0561	0.1054
5.7kb	0.0757	0.1610	0.0624	0.1452
50kb	0.0846	0.1856	0.0623	0.1469
100kb	0.0989	0.1862	0.0740	0.1496
200kb	0.0636	0.1117	0.0563	0.1058
500kb	0.2195	0.3179	0.0834	0.1745

Method1: Send mails with creating a new PHPMailer instance.
Method2: Send mails with only one PHPMailer instance.
Method3: Send mails with creating a new PHPMailer instance and ReduceMIMEEncodeCacheStore.
Method4: Send mails with only one PHPMailer instance and ReduceMIMEEncodeCacheStore.
  • Loading branch information
changyy committed Sep 6, 2019
1 parent 2d1ab8e commit 3a94c03
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,17 @@ class PHPMailer
*/
protected $SingleToArray = [];

/**
* Whether to send the same message to each receiver
* with small changes in mail content.
* Use a lookup table to reduce attachment encoding.
*
* Storage for attachment encoding.
*
* @var array
*/
public $MIMECache = false;

/**
* Whether to generate VERP addresses on send.
* Only applicable when sending via SMTP.
Expand Down Expand Up @@ -3028,14 +3039,32 @@ protected function attachAll($disposition_type, $boundary)
protected function encodeFile($path, $encoding = self::ENCODING_BASE64)
{
try {
if (!static::isPermittedPath($path) || !file_exists($path)) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
$file_buffer = file_get_contents($path);
if (false === $file_buffer) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
$file_buffer = '';
if ($this->MIMECache !== false) {
$cache_key = $path . "\t" . $encoding;
if (array_key_exists($cache_key, $this->MIMECache)) {
$file_buffer = &$this->MIMECache[$cache_key];
} else {
if (!static::isPermittedPath($path) || !file_exists($path)) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
$file_buffer = file_get_contents($path);
if (false === $file_buffer) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
$file_buffer = $this->encodeString($file_buffer, $encoding);
$this->MIMECache[$cache_key] = $file_buffer;
}
} else {
if (!static::isPermittedPath($path) || !file_exists($path)) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
$file_buffer = file_get_contents($path);
if (false === $file_buffer) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
$file_buffer = $this->encodeString($file_buffer, $encoding);
}
$file_buffer = $this->encodeString($file_buffer, $encoding);

return $file_buffer;
} catch (Exception $exc) {
Expand Down

0 comments on commit 3a94c03

Please sign in to comment.