Transform email header string to header array & replace it at various situations.

This commit is contained in:
Philipp Holzer 2020-09-19 20:14:55 +02:00
parent 766d92aaa0
commit ac1d2cf38f
No known key found for this signature in database
GPG key ID: 9A28B7D4FF5667BD
7 changed files with 75 additions and 28 deletions

View file

@ -87,12 +87,15 @@ function notification($params)
}
$nickname = $user["nickname"];
// Creates a new email builder for the notification email
$emailBuilder = DI::emailer()->newNotifyMail();
// with $params['show_in_notification_page'] == false, the notification isn't inserted into
// the database, and an email is sent if applicable.
// default, if not specified: true
$show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
$additional_mail_header = "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
$emailBuilder->setHeader('X-Friendica-Account', '<' . $nickname . '@' . $hostname . '>');
if (array_key_exists('item', $params)) {
$title = $params['item']['title'];
@ -521,13 +524,14 @@ function notification($params)
'receiver-uid' => $params['uid'], 'parent-item' => 0];
DBA::insert('notify-threads', $fields);
$additional_mail_header .= "Message-ID: " . $message_id . "\n";
$emailBuilder->setHeader('Message-ID', $message_id);
$log_msg = "include/enotify: No previous notification found for this parent:\n" .
" parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
Logger::log($log_msg, Logger::DEBUG);
} else {
// If not, just "follow" the thread.
$additional_mail_header .= "References: " . $message_id . "\nIn-Reply-To: " . $message_id . "\n";
$emailBuilder->setHeader('References', $message_id);
$emailBuilder->setHeader('In-Reply-To', $message_id);
Logger::log("There's already a notification for this parent.", Logger::DEBUG);
}
}
@ -546,7 +550,6 @@ function notification($params)
'title' => $title,
'body' => $body,
'subject' => $subject,
'headers' => $additional_mail_header,
];
Hook::callAll('enotify_mail', $datarray);
@ -565,13 +568,13 @@ function notification($params)
// If a photo is present, add it to the email
if (!empty($datarray['source_photo'])) {
$builder->withPhoto(
$emailBuilder->withPhoto(
$datarray['source_photo'],
$datarray['source_link'] ?? $sitelink,
$datarray['source_name'] ?? $sitename);
}
$email = $builder->build();
$email = $emailBuilder->build();
// use the Emailer class to send the message
return DI::emailer()->send($email);

View file

@ -83,11 +83,18 @@ interface IEmail extends JsonSerializable
function getMessage(bool $plain = false);
/**
* Gets any additional mail header
* Gets the additional mail header array
*
* @return string[][]
*/
function getAdditionalMailHeader();
/**
* Gets the additional mail header as string - EOL separated
*
* @return string
*/
function getAdditionalMailHeader();
function getAdditionalMailHeaderString();
/**
* Returns the current email with a new recipient

View file

@ -47,14 +47,14 @@ class Email implements IEmail
/** @var string */
private $msgText;
/** @var string */
private $additionalMailHeader = '';
/** @var string[][] */
private $additionalMailHeader;
/** @var int|null */
private $toUid = null;
private $toUid;
public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress,
string $subject, string $msgHtml, string $msgText,
string $additionalMailHeader = '', int $toUid = null)
array $additionalMailHeader = [], int $toUid = null)
{
$this->fromName = $fromName;
$this->fromAddress = $fromAddress;
@ -127,6 +127,25 @@ class Email implements IEmail
return $this->additionalMailHeader;
}
/**
* {@inheritDoc}
*/
public function getAdditionalMailHeaderString()
{
$headerString = '';
foreach ($this->additionalMailHeader as $name => $values) {
if (is_array($values)) {
foreach ($values as $value) {
$headerString .= $name . ': ' . $value . '\n';
}
} else {
$headerString .= $name . ': ' . $values . '\n';
}
}
return $headerString;
}
/**
* {@inheritDoc}
*/

View file

@ -49,7 +49,7 @@ abstract class MailBuilder
/** @var LoggerInterface */
protected $logger;
/** @var string */
/** @var string[][] */
protected $headers;
/** @var string */
@ -76,13 +76,14 @@ abstract class MailBuilder
$hostname = substr($hostname, 0, strpos($hostname, ':'));
}
$this->headers = "";
$this->headers .= "Precedence: list\n";
$this->headers .= "X-Friendica-Host: " . $hostname . "\n";
$this->headers .= "X-Friendica-Platform: " . FRIENDICA_PLATFORM . "\n";
$this->headers .= "X-Friendica-Version: " . FRIENDICA_VERSION . "\n";
$this->headers .= "List-ID: <notification." . $hostname . ">\n";
$this->headers .= "List-Archive: <" . $baseUrl->get() . "/notifications/system>\n";
$this->headers = [
'Precedence' => ['list'],
'X-Friendica-Host' => [$hostname],
'X-Friendica-Platform' => [FRIENDICA_PLATFORM],
'X-Friendica-Version' => [FRIENDICA_VERSION],
'List-ID' => ['<notification.' . $hostname . '>'],
'List-Archive' => ['<' . $baseUrl->get() . '/notifications/system>'],
];
}
/**
@ -159,15 +160,32 @@ abstract class MailBuilder
}
/**
* Adds new headers to the default headers
* Adds a value to a header
*
* @param string $headers New headers
* @param string $name The header name
* @param string $value The value of the header to add
*
* @return static
*/
public function addHeaders(string $headers)
public function addHeader(string $name, string $value)
{
$this->headers .= $headers;
$this->headers[$name][] = $value;
return $this;
}
/**
* Sets a value to a header (overwrites existing values)
*
* @param string $name The header name
* @param string $value The value to set
*
* @return static
*/
public function setHeader(string $name, string $value)
{
$this->headers[$name] = [];
$this->headers[$name][] = $value;
return $this;
}

View file

@ -151,7 +151,7 @@ class Emailer
. rand(10000, 99999);
// generate a multipart/alternative message header
$messageHeader = $email->getAdditionalMailHeader() .
$messageHeader = $email->getAdditionalMailHeaderString() .
"From: $fromName <{$fromAddress}>\n" .
"Reply-To: $fromName <{$replyTo}>\n" .
"MIME-Version: 1.0\n" .

View file

@ -61,7 +61,7 @@ class MailBuilderTest extends MockedTest
$this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
$this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
$this->defaultHeaders = "";
$this->defaultHeaders = [];
}
public function assertEmail(IEmail $email, array $asserts)

View file

@ -41,7 +41,7 @@ class SystemMailBuilderTest extends MockedTest
/** @var BaseURL */
private $baseUrl;
/** @var string */
/** @var string[] */
private $defaultHeaders;
public function setUp()
@ -60,7 +60,7 @@ class SystemMailBuilderTest extends MockedTest
$this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
$this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
$this->defaultHeaders = "";
$this->defaultHeaders = [];
}
/**