Skip to content

Commit

Permalink
Add Content-Location attachment header support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Butler authored and mathroc committed Oct 3, 2024
1 parent 039de17 commit 9286aa2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class PHPMailer
const ICAL_METHOD_COUNTER = 'COUNTER';
const ICAL_METHOD_DECLINECOUNTER = 'DECLINECOUNTER';

const CONTENT_ID = 'Content-ID';
const CONTENT_LOCATION = 'Content-Location';

/**
* Email priority.
* Options: null (default), 1 = High, 3 = Normal, 5 = low.
Expand Down Expand Up @@ -3292,6 +3295,7 @@ public function addAttachment(
5 => false, //isStringAttachment
6 => $disposition,
7 => $name,
8 => static::CONTENT_ID,
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down Expand Up @@ -3358,6 +3362,7 @@ protected function attachAll($disposition_type, $boundary)
$type = $attachment[4];
$disposition = $attachment[6];
$cid = $attachment[7];
$cidType = $attachment[8];
if ('inline' === $disposition && array_key_exists($cid, $cidUniq)) {
continue;
}
Expand All @@ -3384,9 +3389,17 @@ protected function attachAll($disposition_type, $boundary)
$mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, static::$LE);
}

//Only set Content-IDs on inline attachments
if ((string) $cid !== '' && $disposition === 'inline') {
$mime[] = 'Content-ID: <' . $this->encodeHeader($this->secureHeader($cid)) . '>' . static::$LE;
//Only set Content-ID/Content-Location headers on inline attachments
if (
(string) $cid !== ''
&& $disposition === 'inline'
&& in_array($cidType, [static::CONTENT_ID, static::CONTENT_LOCATION], true)
) {
$encodedCid = $this->encodeHeader($this->secureHeader($cid));
if ($cidType === static::CONTENT_ID) {
$encodedCid = '<' . $encodedCid . '>';
}
$mime[] = $cidType . ': ' . $encodedCid . static::$LE;
}

//Allow for bypassing the Content-Disposition header
Expand Down Expand Up @@ -3782,6 +3795,7 @@ public function addStringAttachment(
5 => true, //isStringAttachment
6 => $disposition,
7 => 0,
8 => '',
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down Expand Up @@ -3813,6 +3827,7 @@ public function addStringAttachment(
* @param string $type File MIME type (by default mapped from the `$path` filename's extension)
* @param string $disposition Disposition to use: `inline` (default) or `attachment`
* (unlikely you want this – {@see `addAttachment()`} instead)
* @param string $cidType static::CONTENT_ID (default) or static::CONTENT_LOCATION
*
* @return bool True on successfully adding an attachment
* @throws Exception
Expand All @@ -3824,7 +3839,8 @@ public function addEmbeddedImage(
$name = '',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'inline'
$disposition = 'inline',
$cidType = self::CONTENT_ID
) {
try {
if (!static::fileIsAccessible($path)) {
Expand Down Expand Up @@ -3855,6 +3871,7 @@ public function addEmbeddedImage(
5 => false, //isStringAttachment
6 => $disposition,
7 => $cid,
8 => $cidType,
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down Expand Up @@ -3883,6 +3900,7 @@ public function addEmbeddedImage(
* @param string $encoding File encoding (see $Encoding), defaults to 'base64'
* @param string $type MIME type - will be used in preference to any automatically derived type
* @param string $disposition Disposition to use
* @param string $cidType static::CONTENT_ID (default) or static::CONTENT_LOCATION
*
* @throws Exception
*
Expand All @@ -3894,7 +3912,8 @@ public function addStringEmbeddedImage(
$name = '',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'inline'
$disposition = 'inline',
$cidType = self::CONTENT_ID
) {
try {
//If a MIME type is not specified, try to work it out from the name
Expand All @@ -3916,6 +3935,7 @@ public function addStringEmbeddedImage(
5 => true, //isStringAttachment
6 => $disposition,
7 => $cid,
8 => $cidType,
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down
1 change: 1 addition & 0 deletions test/PHPMailer/AddEmbeddedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testAddEmbeddedImage()
5 => false,
6 => 'inline',
7 => 'my-attach',
8 => 'Content-ID',
];

$this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="' .
Expand Down
1 change: 1 addition & 0 deletions test/PHPMailer/AddStringAttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testAddPlainStringAttachment()
5 => true,
6 => 'attachment',
7 => 0,
8 => '',
];

$this->Mail->Body = 'Here is the text body';
Expand Down
1 change: 1 addition & 0 deletions test/PHPMailer/AddStringEmbeddedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testHtmlStringEmbedNoName()
5 => true,
6 => 'inline',
7 => $cid,
8 => 'Content-ID',
];

$this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
Expand Down

0 comments on commit 9286aa2

Please sign in to comment.