}
$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'];
'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);
}
}
'title' => $title,
'body' => $body,
'subject' => $subject,
- 'headers' => $additional_mail_header,
];
Hook::callAll('enotify_mail', $datarray);
// 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);
function getMessage(bool $plain = false);
/**
- * Gets any additional mail header
+ * Gets the additional mail header array
*
- * @return string
+ * @return string[][]
*/
function getAdditionalMailHeader();
+ /**
+ * Gets the additional mail header as string - EOL separated
+ *
+ * @return string
+ */
+ function getAdditionalMailHeaderString();
+
/**
* Returns the current email with a new recipient
*
/** @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;
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}
*/
/** @var LoggerInterface */
protected $logger;
- /** @var string */
+ /** @var string[][] */
protected $headers;
/** @var string */
$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>'],
+ ];
}
/**
}
/**
- * Adds new headers to the default headers
+ * Adds a value to a header
+ *
+ * @param string $name The header name
+ * @param string $value The value of the header to add
+ *
+ * @return static
+ */
+ public function addHeader(string $name, string $value)
+ {
+ $this->headers[$name][] = $value;
+
+ return $this;
+ }
+
+ /**
+ * Sets a value to a header (overwrites existing values)
*
- * @param string $headers New headers
+ * @param string $name The header name
+ * @param string $value The value to set
*
* @return static
*/
- public function addHeaders(string $headers)
+ public function setHeader(string $name, string $value)
{
- $this->headers .= $headers;
+ $this->headers[$name] = [];
+ $this->headers[$name][] = $value;
return $this;
}
. 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" .
$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)
/** @var BaseURL */
private $baseUrl;
- /** @var string */
+ /** @var string[] */
private $defaultHeaders;
public function setUp()
$this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
$this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
- $this->defaultHeaders = "";
+ $this->defaultHeaders = [];
}
/**