[TASK] Updates serendipity_event_mailer: Prepend body option, force sending mails, fixed striptag label

This commit is contained in:
Garvin Hicking 2019-12-03 14:25:03 +01:00
parent d7b4e93292
commit 15bc67ce8d
5 changed files with 170 additions and 105 deletions

View File

@ -0,0 +1,8 @@
1.60:
-----
* Add new field to prepend a mail body message for each mail
* Adds checkbox to allow force sending e-mails even when a blog entry is published already
* Fixes missing "Keep stripped tags" description

View File

@ -16,6 +16,9 @@
@define('PLUGIN_EVENT_MAILER_ISTOSENDIT', 'Diesen Eintrag per E-Mail versenden'); @define('PLUGIN_EVENT_MAILER_ISTOSENDIT', 'Diesen Eintrag per E-Mail versenden');
@define('PLUGIN_EVENT_MAILER_SENDTOALL', 'An alle Redakteure schicken'); @define('PLUGIN_EVENT_MAILER_SENDTOALL', 'An alle Redakteure schicken');
@define('PLUGIN_EVENT_MAILER_STRIPTAGS', 'Bilder und Hyperlinks beibehalten, wenn HTML entfernt wird?'); @define('PLUGIN_EVENT_MAILER_KEEPSTRIPTAGS', 'Bilder und Hyperlinks beibehalten, wenn HTML entfernt wird?');
@define('PLUGIN_EVENT_MAILER_STRIPTAGSDESC', 'Gilt nur, wenn HTML entfernt wird. Falls aktiviert, werden Bilder und Hyperlinks in der Mail enthalten bleiben (in eckigen Klammern). Falls deaktiviert, werden alle Bilder und Hyperlinks auch entfernt.'); @define('PLUGIN_EVENT_MAILER_KEEPSTRIPTAGSDESC', 'Gilt nur, wenn HTML entfernt wird. Falls aktiviert, werden Bilder und Hyperlinks in der Mail enthalten bleiben (in eckigen Klammern). Falls deaktiviert, werden alle Bilder und Hyperlinks auch entfernt.');
@define('PLUGIN_EVENT_MAILER_FORCESEND', 'E-Mail-Versand erzwingen');
@define('PLUGIN_EVENT_MAILER_FORCESEND_DESC', 'Standardmäßig werden E-Mails nur beim erstmaligen Veröffentlichen eines Artikels versendet.');
@define('PLUGIN_EVENT_MAILER_MAILTEXT', 'Optionaler Text am Anfang der E-Mail (z.B. eine Grußbotschaft oder Begründung, warum der Blog-Artikel per E-Mail geschickt wird)');

View File

@ -16,6 +16,9 @@
@define('PLUGIN_EVENT_MAILER_ISTOSENDIT', 'Diesen Eintrag per E-Mail versenden'); @define('PLUGIN_EVENT_MAILER_ISTOSENDIT', 'Diesen Eintrag per E-Mail versenden');
@define('PLUGIN_EVENT_MAILER_SENDTOALL', 'An alle Redakteure schicken'); @define('PLUGIN_EVENT_MAILER_SENDTOALL', 'An alle Redakteure schicken');
@define('PLUGIN_EVENT_MAILER_STRIPTAGS', 'Bilder und Hyperlinks beibehalten, wenn HTML entfernt wird?'); @define('PLUGIN_EVENT_MAILER_KEEPSTRIPTAGS', 'Bilder und Hyperlinks beibehalten, wenn HTML entfernt wird?');
@define('PLUGIN_EVENT_MAILER_STRIPTAGSDESC', 'Gilt nur, wenn HTML entfernt wird. Falls aktiviert, werden Bilder und Hyperlinks in der Mail enthalten bleiben (in eckigen Klammern). Falls deaktiviert, werden alle Bilder und Hyperlinks auch entfernt.'); @define('PLUGIN_EVENT_MAILER_KEEPSTRIPTAGSDESC', 'Gilt nur, wenn HTML entfernt wird. Falls aktiviert, werden Bilder und Hyperlinks in der Mail enthalten bleiben (in eckigen Klammern). Falls deaktiviert, werden alle Bilder und Hyperlinks auch entfernt.');
@define('PLUGIN_EVENT_MAILER_FORCESEND', 'E-Mail-Versand erzwingen');
@define('PLUGIN_EVENT_MAILER_FORCESEND_DESC', 'Standardmäßig werden E-Mails nur beim erstmaligen Veröffentlichen eines Artikels versendet.');
@define('PLUGIN_EVENT_MAILER_MAILTEXT', 'Optionaler Text am Anfang der E-Mail (z.B. eine Grußbotschaft oder Begründung, warum der Blog-Artikel per E-Mail geschickt wird)');

View File

@ -21,6 +21,8 @@
@define('PLUGIN_EVENT_MAILER_SENDING', 'Sending'); @define('PLUGIN_EVENT_MAILER_SENDING', 'Sending');
@define('PLUGIN_EVENT_MAILER_ISTOSENDIT', 'Send this entry via E-Mail'); @define('PLUGIN_EVENT_MAILER_ISTOSENDIT', 'Send this entry via E-Mail');
@define('PLUGIN_EVENT_MAILER_SENDTOALL', 'Send to all authors'); @define('PLUGIN_EVENT_MAILER_SENDTOALL', 'Send to all authors');
@define('PLUGIN_EVENT_MAILER_STRIPTAGS', 'Keep images and hyperlinks when removing html?'); @define('PLUGIN_EVENT_MAILER_KEEPSTRIPTAGS', 'Keep images and hyperlinks when removing html?');
@define('PLUGIN_EVENT_MAILER_STRIPTAGSDESC', 'Only applies when removing HTML-tags from the mail. If enabled, images and hyperlinks will be put inside the text, when disabled those placeholders will also be removed.'); @define('PLUGIN_EVENT_MAILER_KEEPSTRIPTAGSDESC', 'Only applies when removing HTML-tags from the mail. If enabled, images and hyperlinks will be put inside the text, when disabled those placeholders will also be removed.');
@define('PLUGIN_EVENT_MAILER_FORCESEND', 'Forces sending an E-Mail on save');
@define('PLUGIN_EVENT_MAILER_FORCESEND_DESC', 'By default, E-Mails are only sent when publishing an entry for the first time');
@define('PLUGIN_EVENT_MAILER_MAILTEXT', 'Optional custom text to prepend to the E-Mail (like a greeting or explaining why this entry is being mailed)');

View File

@ -19,7 +19,7 @@ class serendipity_event_mailer extends serendipity_event
$propbag->add('description', PLUGIN_EVENT_MAILER_DESC); $propbag->add('description', PLUGIN_EVENT_MAILER_DESC);
$propbag->add('stackable', false); $propbag->add('stackable', false);
$propbag->add('author', 'Sebastian Nohn, Kristian Koehntopp, Garvin Hicking'); $propbag->add('author', 'Sebastian Nohn, Kristian Koehntopp, Garvin Hicking');
$propbag->add('version', '1.54'); $propbag->add('version', '1.60');
$propbag->add('requirements', array( $propbag->add('requirements', array(
'serendipity' => '1.6', 'serendipity' => '1.6',
'smarty' => '2.6.7', 'smarty' => '2.6.7',
@ -28,10 +28,11 @@ class serendipity_event_mailer extends serendipity_event
$propbag->add('event_hooks', array( $propbag->add('event_hooks', array(
'backend_publish' => true, 'backend_publish' => true,
'backend_display' => true, 'backend_display' => true,
'backend_save' => true,
)); ));
$propbag->add('groups', array('FRONTEND_ENTRY_RELATED')); $propbag->add('groups', array('FRONTEND_ENTRY_RELATED'));
$config = array('what', 'mailto', 'sendtoall', 'includelink', 'striptags', 'convertp', 'keepstriptags'); $config = array('what', 'mailto', 'sendtoall', 'includelink', 'striptags', 'keepstriptags', 'convertp', 'mailerbody');
$propbag->add('configuration', $config); $propbag->add('configuration', $config);
} }
@ -128,6 +129,13 @@ class serendipity_event_mailer extends serendipity_event
$propbag->add('default', 'false'); $propbag->add('default', 'false');
break; break;
case 'mailerbody':
$propbag->add('type', 'text');
$propbag->add('name', PLUGIN_EVENT_MAILER_MAILTEXT);
$propbag->add('description', '');
$propbag->add('default', '');
break;
default: default:
return false; return false;
} }
@ -139,6 +147,110 @@ class serendipity_event_mailer extends serendipity_event
$title = $this->title; $title = $this->title;
} }
function sendMail($eventData)
{
global $serendipity;
$mails = explode(' ', str_replace(',', '', $this->get_config('mailto')));
$to = array();
foreach($mails AS $mailto) {
$mailto = trim($mailto);
if (!empty($mailto)) {
$to[] = $mailto;
}
}
$this->performConfig($to);
if (is_array($this->data['cat'])) {
$selected = array();
if (is_array($eventData['categories'])) {
foreach($eventData['categories'] AS $idx => $cid) {
$selected[$cid] = true;
}
}
foreach($this->data['cat'] AS $cid => $cat) {
$mailto = trim($this->get_config('category_' . $cid));
if (!empty($mailto) && isset($selected[$cid])) {
$tos = explode(' ', str_replace(',', '', $mailto));
foreach($tos AS $mailtopart) {
$to[] = trim($mailtopart);
}
}
}
}
if ($serendipity['POST']['properties']['sendentry_all']) {
$mails = serendipity_db_query("SELECT DISTINCT email FROM {$serendipity['dbPrefix']}authors");
foreach($mails AS $mail) {
$to[] = trim($mail['email']);
}
}
$mail = array(
'subject' => $eventData['title'],
'body' => $eventData['body'] . $eventData['extended'],
// 'from' => $serendipity['blogTitle'] . ' - ' . $eventData['author'] . ' <' . $serendipity['serendipityEmail'] . '>'
'from' => $serendipity['serendipityEmail']
);
switch($this->get_config('what')) {
case 'all':
$mail['body'] = $eventData['body'] . $eventData['extended'];
break;
case 'body':
$mail['body'] = $eventData['body'];
break;
case 'extended':
$mail['body'] = $eventData['extended'];
break;
case 'none':
$mail['body'] = '';
break;
}
if (!empty($serendipity['POST']['properties']['mailerbody'])) {
$mail['body'] = $serendipity['POST']['properties']['mailerbody'] . "\n" . $mail['body'];
}
if (isset($serendipity['POST']['properties']['mailto'])) {
$mails = explode(' ', str_replace(',', '', $serendipity['POST']['properties']['mailto']));
foreach($mails as $mailto) {
$mailto = trim($mailto);
if (!in_array($mailto, $to)) {
$to[] = $mailto;
}
}
}
if (serendipity_db_bool($this->get_config('convertp', 'false'))) {
$mail['body'] = str_replace('</p>', "</p>\n", $mail['body']);
}
if (serendipity_db_bool($this->get_config('striptags', 'false'))) {
if (serendipity_db_bool($this->get_config('keepstriptags', 'true'))) {
$mail['body'] = preg_replace('@<a[^>]+href=["\']([^"\']*)["\'][^>]*>([^<]*)</a>@i', "$2 [$1]", $mail['body']);
$mail['body'] = preg_replace('@<img[^>]+src=["\']([^"\']*)["\'][^>]*>@i', "[" . IMAGE . ": $1]", $mail['body']);
} else {
$mail['body'] = preg_replace('@<a[^>]+href=["\']([^"\']*)["\'][^>]*>([^<]*)</a>@i', "", $mail['body']);
$mail['body'] = preg_replace('@<img[^>]+src=["\']([^"\']*)["\'][^>]*>@i', "", $mail['body']);
}
$mail['body'] = strip_tags($mail['body']);
}
if (serendipity_db_bool($this->get_config('includelink', 'false'))) {
$mail['body'] = serendipity_archiveURL($eventData['id'], $eventData['title'], 'baseURL', true, array('timestamp' => $eventData['timestamp'])) . "\n\n" . $mail['body'];
}
foreach($to AS $mailto) {
if (!empty($mailto)) {
echo serendipity_specialchars($mailto) . '...<br />';
serendipity_sendMail($mailto, $mail['subject'], $mail['body'], $mail['from']);
}
}
}
function event_hook($event, &$bag, &$eventData, $addData = null) function event_hook($event, &$bag, &$eventData, $addData = null)
{ {
global $serendipity; global $serendipity;
@ -162,7 +274,13 @@ class serendipity_event_mailer extends serendipity_event
$sendtoall = serendipity_db_bool($this->get_config('sendtoall')); $sendtoall = serendipity_db_bool($this->get_config('sendtoall'));
} }
?> if (isset($serendipity['POST']['properties']['mailerbody'])) {
$mailerbody = $serendipity['POST']['properties']['mailerbody'];
} else {
$mailerbody = $this->get_config('mailerbody');
}
?>
<fieldset class="entryproperties"> <fieldset class="entryproperties">
<span class="wrap_legend"><legend><?php echo PLUGIN_EVENT_MAILER_NAME; ?></legend></span> <span class="wrap_legend"><legend><?php echo PLUGIN_EVENT_MAILER_NAME; ?></legend></span>
@ -178,108 +296,40 @@ class serendipity_event_mailer extends serendipity_event
<input id="sendall" type="checkbox" value="true" name="serendipity[properties][sendentry_all]" <?php echo ($sendtoall ? 'checked="checked"': ''); ?>> <input id="sendall" type="checkbox" value="true" name="serendipity[properties][sendentry_all]" <?php echo ($sendtoall ? 'checked="checked"': ''); ?>>
<label title="<?php echo PLUGIN_EVENT_MAILER_SENDTOALL; ?>" for="sendall"><?php echo PLUGIN_EVENT_MAILER_SENDTOALL; ?></label> <label title="<?php echo PLUGIN_EVENT_MAILER_SENDTOALL; ?>" for="sendall"><?php echo PLUGIN_EVENT_MAILER_SENDTOALL; ?></label>
</div> </div>
<?php if (!serendipity_db_bool($eventData['isdraft'])) { // Only show this for entries that are published ?>
<div class="form_check">
<input id="forcesend" type="checkbox" value="true" name="serendipity[properties][forcesend]">
<label title="<?php echo PLUGIN_EVENT_MAILER_FORCESEND; ?>" for="forcesend"><?php echo PLUGIN_EVENT_MAILER_FORCESEND; ?></label>
<br />
<em><?php echo PLUGIN_EVENT_MAILER_FORCESEND_DESC; ?></em>
</div>
<?php } ?>
<div class="form_check">
<label for="mailerbody"><?= PLUGIN_EVENT_MAILER_MAILTEXT; ?></label>
<textarea id="mailerbody" rows="5" name="serendipity[properties][mailerbody]" ><?php echo serendipity_specialchars($mailerbody); ?></textarea>
</div>
</fieldset> </fieldset>
<?php <?php
break; break;
case 'backend_save':
if (serendipity_db_bool($eventData['isdraft'])) {
// Never send e-mails for drafts.
return true;
}
if (isset($serendipity['POST']['properties']['forcesend']) && $serendipity['POST']['properties']['forcesend']) {
$this->sendMail($eventData);
} else {
echo PLUGIN_EVENT_MAILER_NOTSENDDECISION . '<br />';
}
break;
case 'backend_publish': case 'backend_publish':
if (isset($serendipity['POST']['properties']) && !isset($serendipity['POST']['properties']['sendentry'])) { if (isset($serendipity['POST']['properties']) && !isset($serendipity['POST']['properties']['sendentry'])) {
echo PLUGIN_EVENT_MAILER_NOTSENDDECISION . '<br />'; echo PLUGIN_EVENT_MAILER_NOTSENDDECISION . '<br />';
} else { } else {
$mails = explode(' ', str_replace(',', '', $this->get_config('mailto'))); $this->sendMail($eventData);
$to = array();
foreach($mails AS $mailto) {
$mailto = trim($mailto);
if (!empty($mailto)) {
$to[] = $mailto;
}
}
$this->performConfig($to);
if (is_array($this->data['cat'])) {
$selected = array();
if (is_array($eventData['categories'])) {
foreach($eventData['categories'] AS $idx => $cid) {
$selected[$cid] = true;
}
}
foreach($this->data['cat'] AS $cid => $cat) {
$mailto = trim($this->get_config('category_' . $cid));
if (!empty($mailto) && isset($selected[$cid])) {
$tos = explode(' ', str_replace(',', '', $mailto));
foreach($tos AS $mailtopart) {
$to[] = trim($mailtopart);
}
}
}
}
if ($serendipity['POST']['properties']['sendentry_all']) {
$mails = serendipity_db_query("SELECT DISTINCT email FROM {$serendipity['dbPrefix']}authors");
foreach($mails AS $mail) {
$to[] = trim($mail['email']);
}
}
$mail = array(
'subject' => $eventData['title'],
'body' => $eventData['body'] . $eventData['extended'],
// 'from' => $serendipity['blogTitle'] . ' - ' . $eventData['author'] . ' <' . $serendipity['serendipityEmail'] . '>'
'from' => $serendipity['serendipityEmail']
);
switch($this->get_config('what')) {
case 'all':
$mail['body'] = $eventData['body'] . $eventData['extended'];
break;
case 'body':
$mail['body'] = $eventData['body'];
break;
case 'extended':
$mail['body'] = $eventData['extended'];
break;
case 'none':
$mail['body'] = '';
break;
}
if (isset($serendipity['POST']['properties']['mailto'])) {
$mails = explode(' ', str_replace(',', '', $serendipity['POST']['properties']['mailto']));
foreach($mails as $mailto) {
$mailto = trim($mailto);
if (!in_array($mailto, $to)) {
$to[] = $mailto;
}
}
}
if (serendipity_db_bool($this->get_config('convertp', 'false'))) {
$mail['body'] = str_replace('</p>', "</p>\n", $mail['body']);
}
if (serendipity_db_bool($this->get_config('striptags', 'false'))) {
if (serendipity_db_bool($this->get_config('keepstriptags', 'true'))) {
$mail['body'] = preg_replace('§<a[^>]+href=["\']([^"\']*)["\'][^>]*>([^<]*)</a>§i', "$2 [$1]", $mail['body']);
$mail['body'] = preg_replace('§<img[^>]+src=["\']([^"\']*)["\'][^>]*>§i', "[" . IMAGE . ": $1]", $mail['body']);
} else {
$mail['body'] = preg_replace('§<a[^>]+href=["\']([^"\']*)["\'][^>]*>([^<]*)</a>§i', "", $mail['body']);
$mail['body'] = preg_replace('§<img[^>]+src=["\']([^"\']*)["\'][^>]*>§i', "", $mail['body']);
}
$mail['body'] = strip_tags($mail['body']);
}
if (serendipity_db_bool($this->get_config('includelink', 'false'))) {
$mail['body'] = serendipity_archiveURL($eventData['id'], $eventData['title'], 'baseURL', true, array('timestamp' => $eventData['timestamp'])) . "\n\n" . $mail['body'];
}
foreach($to AS $mailto) {
if (!empty($mailto)) {
echo serendipity_specialchars($mailto) . '...<br />';
serendipity_sendMail($mailto, $mail['subject'], $mail['body'], $mail['from']);
}
}
} }
break; break;
@ -295,4 +345,3 @@ class serendipity_event_mailer extends serendipity_event
} }
/* vim: set sts=4 ts=4 expandtab : */ /* vim: set sts=4 ts=4 expandtab : */
?>