mirror of
https://github.com/friendica/friendica
synced 2024-12-23 11:20:15 +00:00
Merge pull request #8232 from nupplaphil/task/notify_email_builder
Introduce NotifyEmailBuilder
This commit is contained in:
commit
e42b843505
7 changed files with 285 additions and 122 deletions
|
@ -15,7 +15,6 @@ use Friendica\Model\ItemContent;
|
|||
use Friendica\Model\Notify;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Model\UserItem;
|
||||
use Friendica\Object\Email;
|
||||
use Friendica\Protocol\Activity;
|
||||
|
||||
/**
|
||||
|
@ -31,8 +30,6 @@ use Friendica\Protocol\Activity;
|
|||
*/
|
||||
function notification($params)
|
||||
{
|
||||
$a = DI::app();
|
||||
|
||||
// Temporary logging for finding the origin
|
||||
if (!isset($params['uid'])) {
|
||||
Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
|
||||
|
@ -55,25 +52,14 @@ function notification($params)
|
|||
// from here on everything is in the recipients language
|
||||
$l10n = DI::l10n()->withLang($params['language']);
|
||||
|
||||
$banner = $l10n->t('Friendica Notification');
|
||||
$product = FRIENDICA_PLATFORM;
|
||||
$siteurl = DI::baseUrl()->get(true);
|
||||
$thanks = $l10n->t('Thank You,');
|
||||
$sitename = DI::config()->get('config', 'sitename');
|
||||
if (DI::config()->get('config', 'admin_name')) {
|
||||
$site_admin = $l10n->t('%1$s, %2$s Administrator', DI::config()->get('config', 'admin_name'), $sitename);
|
||||
} else {
|
||||
$site_admin = $l10n->t('%s Administrator', $sitename);
|
||||
}
|
||||
|
||||
$sender_name = $sitename;
|
||||
$hostname = DI::baseUrl()->getHostname();
|
||||
if (strpos($hostname, ':')) {
|
||||
$hostname = substr($hostname, 0, strpos($hostname, ':'));
|
||||
}
|
||||
|
||||
$sender_email = DI::emailer()->getSiteEmailAddress();
|
||||
|
||||
$user = User::getById($params['uid'], ['nickname', 'page-flags']);
|
||||
|
||||
// There is no need to create notifications for forum accounts
|
||||
|
@ -87,14 +73,7 @@ function notification($params)
|
|||
// default, if not specified: true
|
||||
$show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
|
||||
|
||||
$additional_mail_header = "";
|
||||
$additional_mail_header .= "Precedence: list\n";
|
||||
$additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
|
||||
$additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
|
||||
$additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
|
||||
$additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
|
||||
$additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
|
||||
$additional_mail_header .= "List-Archive: <".DI::baseUrl()."/notifications/system>\n";
|
||||
$additional_mail_header = "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
|
||||
|
||||
if (array_key_exists('item', $params)) {
|
||||
$title = $params['item']['title'];
|
||||
|
@ -503,74 +482,46 @@ function notification($params)
|
|||
}
|
||||
}
|
||||
|
||||
$textversion = BBCode::toPlaintext($body);
|
||||
$htmlversion = BBCode::convert($body);
|
||||
|
||||
$datarray = [];
|
||||
$datarray['banner'] = $banner;
|
||||
$datarray['product'] = $product;
|
||||
$datarray['preamble'] = $preamble;
|
||||
$datarray['sitename'] = $sitename;
|
||||
$datarray['siteurl'] = $siteurl;
|
||||
$datarray['type'] = $params['type'];
|
||||
$datarray['parent'] = $parent_id;
|
||||
$datarray['source_name'] = $params['source_name'] ?? '';
|
||||
$datarray['source_link'] = $params['source_link'] ?? '';
|
||||
$datarray['source_photo'] = $params['source_photo'] ?? '';
|
||||
$datarray['uid'] = $params['uid'];
|
||||
$datarray['hsitelink'] = $hsitelink;
|
||||
$datarray['tsitelink'] = $tsitelink;
|
||||
$datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
|
||||
$datarray['titemlink'] = $itemlink;
|
||||
$datarray['thanks'] = $thanks;
|
||||
$datarray['site_admin'] = $site_admin;
|
||||
$datarray['title'] = stripslashes($title);
|
||||
$datarray['htmlversion'] = $htmlversion;
|
||||
$datarray['textversion'] = $textversion;
|
||||
$datarray['subject'] = $subject;
|
||||
$datarray['headers'] = $additional_mail_header;
|
||||
$datarray = [
|
||||
'preamble' => $preamble,
|
||||
'type' => $params['type'],
|
||||
'parent' => $parent_id,
|
||||
'source_name' => $params['source_name'] ?? null,
|
||||
'source_link' => $params['source_link'] ?? null,
|
||||
'source_photo' => $params['source_photo'] ?? null,
|
||||
'uid' => $params['uid'],
|
||||
'hsitelink' => $hsitelink,
|
||||
'tsitelink' => $tsitelink,
|
||||
'itemlink' => $itemlink,
|
||||
'title' => $title,
|
||||
'body' => $body,
|
||||
'subject' => $subject,
|
||||
'headers' => $additional_mail_header,
|
||||
];
|
||||
|
||||
Hook::callAll('enotify_mail', $datarray);
|
||||
|
||||
// check whether sending post content in email notifications is allowed
|
||||
$content_allowed = (!DI::config()->get('system', 'enotify_no_content'));
|
||||
$builder = DI::emailer()
|
||||
->newNotifyMail()
|
||||
->addHeaders($datarray['headers'])
|
||||
->withRecipient($params['to_email'])
|
||||
->forUser([
|
||||
'uid' => $datarray['uid'],
|
||||
'language' => $params['language'],
|
||||
])
|
||||
->withNotification($datarray['subject'], $datarray['preamble'], $datarray['title'], $datarray['body'])
|
||||
->withSiteLink($datarray['tsitelink'], $datarray['hsitelink'])
|
||||
->withItemLink($datarray['itemlink']);
|
||||
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
|
||||
$email_html_body = Renderer::replaceMacros($tpl, [
|
||||
'$banner' => $datarray['banner'],
|
||||
'$product' => $datarray['product'],
|
||||
'$preamble' => str_replace("\n", "<br>\n", $datarray['preamble']),
|
||||
'$sitename' => $datarray['sitename'],
|
||||
'$siteurl' => $datarray['siteurl'],
|
||||
'$source_name' => $datarray['source_name'],
|
||||
'$source_link' => $datarray['source_link'],
|
||||
'$source_photo' => $datarray['source_photo'],
|
||||
'$hsitelink' => $datarray['hsitelink'],
|
||||
'$hitemlink' => $datarray['hitemlink'],
|
||||
'$thanks' => $datarray['thanks'],
|
||||
'$site_admin' => $datarray['site_admin'],
|
||||
'$title' => $datarray['title'],
|
||||
'$htmlversion' => $datarray['htmlversion'],
|
||||
'$content_allowed' => $content_allowed,
|
||||
]);
|
||||
// If a photo is present, add it to the email
|
||||
if (!empty($datarray['source_photo'])) {
|
||||
$builder->withPhoto(
|
||||
$datarray['source_photo'],
|
||||
$datarray['source_link'] ?? $sitelink,
|
||||
$datarray['source_name'] ?? $sitename);
|
||||
}
|
||||
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/notify/text.tpl');
|
||||
$email_text_body = Renderer::replaceMacros($tpl, [
|
||||
'$preamble' => $datarray['preamble'],
|
||||
'$tsitelink' => $datarray['tsitelink'],
|
||||
'$titemlink' => $datarray['titemlink'],
|
||||
'$thanks' => $datarray['thanks'],
|
||||
'$site_admin' => $datarray['site_admin'],
|
||||
'$title' => $datarray['title'],
|
||||
'$textversion' => $datarray['textversion'],
|
||||
'$content_allowed' => $content_allowed,
|
||||
]);
|
||||
|
||||
$email = new Email($sender_name, $sender_email, $sender_email, $params['to_email'],
|
||||
$datarray['subject'], $email_html_body, $email_text_body,
|
||||
$datarray['headers'], $params['uid']);
|
||||
$email = $builder->build();
|
||||
|
||||
// use the Emailer class to send the message
|
||||
return DI::emailer()->send($email);
|
||||
|
|
|
@ -139,6 +139,20 @@ abstract class MailBuilder
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new headers to the default headers
|
||||
*
|
||||
* @param string $headers New headers
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function addHeaders(string $headers)
|
||||
{
|
||||
$this->headers .= $headers;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a email based on the given attributes
|
||||
*
|
||||
|
|
205
src/Util/EMailer/NotifyMailBuilder.php
Normal file
205
src/Util/EMailer/NotifyMailBuilder.php
Normal file
|
@ -0,0 +1,205 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Util\EMailer;
|
||||
|
||||
use Exception;
|
||||
use Friendica\App\BaseURL;
|
||||
use Friendica\Content\Text\BBCode;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Builder for notification emails (notification, source, links, ...)
|
||||
*/
|
||||
class NotifyMailBuilder extends MailBuilder
|
||||
{
|
||||
/** @var string */
|
||||
protected $subject;
|
||||
/** @var string */
|
||||
protected $preamble;
|
||||
/** @var string */
|
||||
protected $body;
|
||||
|
||||
/** @var string */
|
||||
protected $siteAdmin;
|
||||
|
||||
/** @var bool */
|
||||
private $contentAllowed = false;
|
||||
/** @var string */
|
||||
private $title = '';
|
||||
/** @var array Details to print a photo:
|
||||
* - image
|
||||
* - link
|
||||
* - name
|
||||
*/
|
||||
private $photo = [
|
||||
'image' => null,
|
||||
'link' => null,
|
||||
'name' => null,
|
||||
];
|
||||
/** @var array HTML/Plain version of the Site Link:
|
||||
* - html
|
||||
* - text
|
||||
*/
|
||||
private $siteLink = [
|
||||
'html' => '',
|
||||
'text' => '',
|
||||
];
|
||||
/** @var string The item link */
|
||||
private $itemLink = '';
|
||||
|
||||
public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger, string $siteEmailAddress, string $siteName)
|
||||
{
|
||||
parent::__construct($l10n, $baseUrl, $config, $logger);
|
||||
|
||||
if ($this->config->get('config', 'admin_name')) {
|
||||
$this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
|
||||
} else {
|
||||
$this->siteAdmin = $l10n->t('%s Administrator', $siteName);
|
||||
}
|
||||
|
||||
// Set the system wide site address/name as sender (default for system mails)
|
||||
$this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
|
||||
|
||||
// check whether sending post content in email notifications is allowed
|
||||
$this->contentAllowed = !$this->config->get('system', 'enotify_no_content', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a notification (in fact a more detailed message)
|
||||
*
|
||||
* @param string $subject
|
||||
* @param string $preamble
|
||||
* @param string $title
|
||||
* @param string|null $body
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withNotification(string $subject, string $preamble, string $title, string $body = null)
|
||||
{
|
||||
if (!isset($body)) {
|
||||
$body = $preamble;
|
||||
}
|
||||
|
||||
$this->title = stripslashes($title);
|
||||
$this->subject = $subject;
|
||||
$this->preamble = $preamble;
|
||||
$this->body = $body;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a photo of the source of the notify
|
||||
*
|
||||
* @param string $image The image link to the photo
|
||||
* @param string $link The link to the source
|
||||
* @param string $name The name of the source
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withPhoto(string $image, string $link, string $name)
|
||||
{
|
||||
$this->photo = [
|
||||
'image' => $image ?? '',
|
||||
'link' => $link ?? '',
|
||||
'name' => $name ?? '',
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a sitelink to the notification
|
||||
*
|
||||
* @param string $text The text version of the site link
|
||||
* @param string $html The html version of the site link
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withSiteLink(string $text, string $html = '')
|
||||
{
|
||||
$this->siteLink = [
|
||||
'text' => $text,
|
||||
'html' => $html,
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a link to the item of the notification
|
||||
*
|
||||
* @param string $link The text version of the item link
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withItemLink(string $link)
|
||||
{
|
||||
$this->itemLink = $link;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSubject()
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws InternalServerErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getHtmlMessage()
|
||||
{
|
||||
$htmlVersion = BBCode::convert($this->body);
|
||||
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
|
||||
return Renderer::replaceMacros($tpl, [
|
||||
'$preamble' => str_replace("\n", "<br>\n", $this->preamble),
|
||||
'$source_name' => $this->photo['name'],
|
||||
'$source_link' => $this->photo['link'],
|
||||
'$source_photo' => $this->photo['image'],
|
||||
'$hsitelink' => $this->siteLink['html'],
|
||||
'$hitemlink' => sprintf('<a href="%s">%s</a>', $this->itemLink, $this->itemLink),
|
||||
'$thanks' => $this->l10n->t('thanks'),
|
||||
'$site_admin' => $this->siteAdmin,
|
||||
'$title' => $this->title,
|
||||
'$htmlversion' => $htmlVersion,
|
||||
'$content_allowed' => $this->contentAllowed,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getPlaintextMessage()
|
||||
{
|
||||
$textVersion = BBCode::toPlaintext($this->body);
|
||||
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/notify/text.tpl');
|
||||
return Renderer::replaceMacros($tpl, [
|
||||
'$preamble' => $this->preamble,
|
||||
'$tsitelink' => $this->siteLink['text'],
|
||||
'$titemlink' => $this->itemLink,
|
||||
'$thanks' => $this->l10n->t('thanks'),
|
||||
'$site_admin' => $this->siteAdmin,
|
||||
'$title' => $this->title,
|
||||
'$textversion' => $textVersion,
|
||||
'$content_allowed' => $this->contentAllowed,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -38,9 +38,7 @@ class SystemMailBuilder extends MailBuilder
|
|||
}
|
||||
|
||||
// Set the system wide site address/name as sender (default for system mails)
|
||||
$this->senderName = $siteName;
|
||||
$this->senderAddress = $siteEmailAddress;
|
||||
$this->senderNoReply = $siteEmailAddress;
|
||||
$this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,6 +12,7 @@ use Friendica\Core\PConfig\IPConfig;
|
|||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Object\EMail\IEmail;
|
||||
use Friendica\Protocol\Email;
|
||||
use Friendica\Util\EMailer\NotifyMailBuilder;
|
||||
use Friendica\Util\EMailer\SystemMailBuilder;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
@ -89,6 +90,17 @@ class Emailer
|
|||
$this->getSiteEmailAddress(), $this->getSiteEmailName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new mail for notifications
|
||||
*
|
||||
* @return NotifyMailBuilder
|
||||
*/
|
||||
public function newNotifyMail()
|
||||
{
|
||||
return new NotifyMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
|
||||
$this->getSiteEmailAddress(), $this->getSiteEmailName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a multipart/alternative message with Text and HTML versions
|
||||
*
|
||||
|
|
|
@ -1,37 +1,20 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional //EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>{{$banner}}</title>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
||||
</head>
|
||||
<body>
|
||||
<table style="border:1px solid #ccc">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td colspan="2" style="background:#084769; color:#FFFFFF; font-weight:bold; font-family:'lucida grande', tahoma, verdana,arial, sans-serif; padding: 4px 8px; vertical-align: middle; font-size:16px; letter-spacing: -0.03em; text-align: left;">
|
||||
<img style="width:32px;height:32px; float:left;" src='{{$siteurl}}/images/friendica-32.png'>
|
||||
<div style="padding:7px; margin-left: 5px; float:left; font-size:18px;letter-spacing:1px;">{{$product}}</div>
|
||||
<div style="clear: both;"></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr><td colspan="2" style="padding-top:22px;">{{$preamble nofilter}}</td></tr>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr><td colspan="2" style="padding-top:22px;">{{$preamble nofilter}}</td></tr>
|
||||
|
||||
{{if $content_allowed}}
|
||||
{{if $source_photo}}
|
||||
<tr>
|
||||
<td style="padding-left:22px;padding-top:22px;width:60px;" valign="top" rowspan=3><a href="{{$source_link}}"><img style="border:0px;width:48px;height:48px;" src="{{$source_photo}}"></a></td>
|
||||
<td style="padding-top:22px;"><a href="{{$source_link}}">{{$source_name}}</a></td>
|
||||
</tr>
|
||||
{{/if}}
|
||||
<tr><td style="font-weight:bold;padding-bottom:5px;">{{$title}}</td></tr>
|
||||
<tr><td style="padding-right:22px;">{{$htmlversion nofilter}}</td></tr>
|
||||
<tr>
|
||||
<td style="padding-left:22px;padding-top:22px;width:60px;" valign="top" rowspan=3><a href="{{$source_link}}"><img style="border:0px;width:48px;height:48px;" src="{{$source_photo}}"></a></td>
|
||||
<td style="padding-top:22px;"><a href="{{$source_link}}">{{$source_name}}</a></td>
|
||||
</tr>
|
||||
{{/if}}
|
||||
<tr><td style="font-weight:bold;padding-bottom:5px;">{{$title}}</td></tr>
|
||||
<tr><td style="padding-right:22px;">{{$htmlversion nofilter}}</td></tr>
|
||||
{{/if}}
|
||||
<tr><td colspan="2" style="padding-top:11px;">{{$hsitelink nofilter}}</td></tr>
|
||||
<tr><td colspan="2" style="padding-bottom:11px;">{{$hitemlink nofilter}}</td></tr>
|
||||
<tr><td></td><td>{{$thanks}}</td></tr>
|
||||
<tr><td></td><td>{{$site_admin}}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
<tr><td colspan="2" style="padding-top:11px;">{{$hsitelink nofilter}}</td></tr>
|
||||
<tr><td colspan="2" style="padding-bottom:11px;">{{$hitemlink nofilter}}</td></tr>
|
||||
<tr><td></td><td>{{$thanks}}</td></tr>
|
||||
<tr><td></td><td>{{$site_admin}}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
|
@ -2,4 +2,4 @@
|
|||
<tr><td style="padding-right:22px;">{{$htmlversion nofilter}}</td></tr>
|
||||
<tr><td>{{$thanks}}</td></tr>
|
||||
<tr><td>{{$site_admin}}</td></tr>
|
||||
</table>
|
||||
</table>
|
||||
|
|
Loading…
Reference in a new issue