Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Content-Location attachment header support #2065

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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 @@ -2989,6 +2992,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 @@ -3055,6 +3059,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 @@ -3081,9 +3086,13 @@ 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 @@ -3479,6 +3488,7 @@ public function addStringAttachment(
5 => true, // isStringAttachment
6 => $disposition,
7 => 0,
8 => '',
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down Expand Up @@ -3509,6 +3519,7 @@ public function addStringAttachment(
* @param string $encoding File encoding (see $Encoding)
* @param string $type File MIME type
* @param string $disposition Disposition to use
* @param string $cidType static::CONTENT_ID (default) or static::CONTENT_LOCATION
*
* @throws Exception
*
Expand All @@ -3520,7 +3531,8 @@ public function addEmbeddedImage(
$name = '',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'inline'
$disposition = 'inline',
$cidType = self::CONTENT_ID
) {
try {
if (!static::isPermittedPath($path) || !@is_file($path) || !is_readable($path)) {
Expand Down Expand Up @@ -3551,6 +3563,7 @@ public function addEmbeddedImage(
5 => false, // isStringAttachment
6 => $disposition,
7 => $cid,
8 => $cidType,
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down Expand Up @@ -3579,6 +3592,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 @@ -3590,7 +3604,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 @@ -3612,6 +3627,7 @@ public function addStringEmbeddedImage(
5 => true, // isStringAttachment
6 => $disposition,
7 => $cid,
8 => $cidType,
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
Expand Down
5 changes: 5 additions & 0 deletions src/SMTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ public function connect($host, $port = null, $timeout = 30, $options = [])

$this->smtp_conn = $this->getSMTPConnection($host, $port, $timeout, $options);

if ($this->smtp_conn === false) {
//Error info already set inside `getSMTPConnection()`
return false;
}

$this->edebug('Connection: opened', self::DEBUG_CONNECTION);
// SMTP server can take longer to respond, give longer timeout for first read
// Windows does not have support for this timeout function
Expand Down