MailtemplateDispatch finished

This commit is contained in:
Frank Schubert
2025-01-14 22:41:14 +01:00
parent c55747054c
commit 5393160273
5 changed files with 314 additions and 29 deletions

View File

@@ -82,7 +82,7 @@ $pagination_entity_name = "Emailaussendungen";
<?php if($dispatch->send_finish): ?> <?php if($dispatch->send_finish): ?>
<i class="fas fa-fw fa-check text-success" title="Aussendung abgeschlossen"></i> <i class="fas fa-fw fa-check text-success" title="Aussendung abgeschlossen"></i>
<?php elseif($dispatch->send_lock): ?> <?php elseif($dispatch->send_lock): ?>
<i class="far fa-fw fa-evelope fa-spin-pulse text-primary" title="Versand läuft"></i> <i class="far fa-fw fa-envelope fa-spin-pulse text-primary" title="Versand läuft"></i>
<?php elseif($dispatch->tosend_date && !$dispatch->send_lock): ?> <?php elseif($dispatch->tosend_date && !$dispatch->send_lock): ?>
<i class="far fa-fw fa-clock" title="Warte auf Versand"></i> <i class="far fa-fw fa-clock" title="Warte auf Versand"></i>
<?php endif; ?> <?php endif; ?>

View File

@@ -10,11 +10,18 @@ class MailtemplateDispatch extends mfBaseModel {
public function sendToRecipients($test_to_email = false) { public function sendToRecipients($test_to_email = false) {
$emails = [];
if(!$test_to_email) { if(!$test_to_email) {
$emails = $this->getRecipients(); $emails = $this->getRecipients();
if(!$emails) return true; if(!$emails) {
if(!$this->tosend_date || $this->tosend_date > date('U')) return false; $this->log->debug(__METHOD__.": No recipient emails (".$this->id.")");
if($this->send_lock) return false; return true;
}
if(!$this->tosend_date || $this->tosend_date > date('U')) {
$this->log->debug(__METHOD__.": No tosend date or in future (".$this->id.")");
return true;
}
} }
if($test_to_email) { if($test_to_email) {
@@ -28,9 +35,14 @@ class MailtemplateDispatch extends mfBaseModel {
$template = $this->getProperty("mailtemplate"); $template = $this->getProperty("mailtemplate");
if(!$template) return false; if(!$template) return false;
$body = "<html><head></head><body>"; if($template->body_html) {
$body .= $template->renderBody(); $body = "<html><head></head><body>";
$body .= "</body></html>\n"; // explicitly add new line, or some mail clients wont show attachment $body .= $template->renderBody();
$body .= "</body></html>\n"; // explicitly add new line, or some mail clients wont show attachment
} else {
$body = $template->renderBody();
}
$subject = $this->subject; $subject = $this->subject;
@@ -38,7 +50,7 @@ class MailtemplateDispatch extends mfBaseModel {
$from_name = $this->sender_name; $from_name = $this->sender_name;
$attachments = []; $attachments = [];
foreach($this->getProperty("mailtemplate")->files as $file) { foreach($template->files as $file) {
$att = []; $att = [];
$att["path"] = MFUPLOAD_FILE_SAVE_PATH."/".$file->file->subfolder."/".$file->file->store_filename; $att["path"] = MFUPLOAD_FILE_SAVE_PATH."/".$file->file->subfolder."/".$file->file->store_filename;
$att["filename"] = $file->filename; $att["filename"] = $file->filename;
@@ -50,23 +62,19 @@ class MailtemplateDispatch extends mfBaseModel {
$to = $email; $to = $email;
// check for NotificationLog // check for NotificationLog
/*$mnlog = MaintenanceNotificationLog::getFirst(["maintenancenotification_id" => $this->id, "email" => $to]); $mtdlog = MailtemplateDispatchLog::getFirst(["mailtemplatedispatch_id" => $this->id, "email" => $to]);
if($mnlog) { if($mtdlog) {
// was sent already // was sent already
continue; continue;
}*/ }
/*fwrite(STDERR, "Subject: $subject\n\n");
fwrite(STDERR, "$body\n\n");
exit;*/
$email = new Emailnotification(); $email = new Emailnotification();
$email->setSubject($subject); $email->setSubject($subject);
if($this->getProperty("mailtemplate")->body_html) { if($template->body_html) {
$email->setHtmlBody($body); $email->setHtmlBody($body);
} else { } else {
$email->setBody($body); $email->setBody($body);
} }
$email->setFrom($from_email, $from_name); $email->setFrom($from_email, $from_name);
$email->setTo($to); $email->setTo($to);
$email->setHeader("X-".ucfirst(MFAPPNAME)."-mtdid", $this->id); $email->setHeader("X-".ucfirst(MFAPPNAME)."-mtdid", $this->id);
@@ -82,24 +90,16 @@ class MailtemplateDispatch extends mfBaseModel {
return true; return true;
} }
/*$mnlog = MaintenanceNotificationLog::create([ $mtdlog = MailtemplateDispatchLog::create([
"maintenancenotification_id" => $this->id, "mailtemplatedispatch_id" => $this->id,
"email" => $to, "email" => $to,
"sent" => date("U"), "sent" => date("U"),
]); ]);
$mnlog->save(); $mtdlog->save();
*/
sleep(1); // wait a second
} }
//$this->sent = date("U");
//$this->save();
} }
public function getRecipients($list_recipients = false, $return_recipients = false) { public function getRecipients($list_recipients = false, $return_recipients = false) {
@@ -463,6 +463,19 @@ class MailtemplateDispatch extends mfBaseModel {
} }
} }
if(array_key_exists("send_finish", $filter)) {
$send_finish = $filter['send_finish'];
if($send_finish === null) {
$where .= " AND send_finish IS NULL";
}
}
if(array_key_exists("send_lock", $filter)) {
$send_lock = $filter['send_lock'];
if($send_lock === null) {
$where .= " AND send_lock IS NULL";
}
}
if(array_key_exists("add-where", $filter)) { if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where']; $where .= " ".$filter['add-where'];

View File

@@ -0,0 +1,183 @@
<?php
class MailtemplateDispatchLog extends mfBaseModel {
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new MailtemplateDispatchLog();
$table_fields = [
"mailtemplatedispatch_id", "email", "sent",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("MailtemplateDispatchLog", "*", "1 = 1 ORDER BY `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new MailtemplateDispatchLog($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MailtemplateDispatchLog
WHERE $where
ORDER BY `create` LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new MailtemplateDispatchLog($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM MailtemplateDispatchLog
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "`create` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MailtemplateDispatchLog
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new MailtemplateDispatchLog($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("mailtemplatedispatch_id", $filter)) {
$mailtemplatedispatch_id = $filter['mailtemplatedispatch_id'];
if(is_numeric($mailtemplatedispatch_id)) {
$where .= " AND mailtemplatedispatch_id=$mailtemplatedispatch_id";
}
}
if(array_key_exists("email", $filter)) {
$email = FronkDB::singleton()->escape($filter["email"]);
if($email) {
$where .= " AND email='$email'";
}
}
if(array_key_exists("sent", $filter)) {
$sent = $filter['sent'];
if($sent === true) {
$where .= " AND `sent` > 0";
} elseif($sent === false || $sent === null) {
$where .= " AND (`sent` IS NULL OR `sent` = 0)";
} elseif(is_numeric($sent)) {
$where .= " AND `sent`=$sent";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class CreateMailtemplateDispatchLog extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$log = $this->table("MailtemplateDispatchLog");
$log->addColumn("mailtemplatedispatch_id", "integer", ["null" => false]);
$log->addColumn("email", "string", ["null" => false]);
$log->addColumn("sent", "integer", ["null" => false, "default" => 0]);
$log->addColumn("create_by", "integer", ["null" => false]);
$log->addColumn("edit_by", "integer", ["null" => false]);
$log->addColumn("create", "integer", ["null" => false]);
$log->addColumn("edit", "integer", ["null" => false]);
$log->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("MailtemplateDispatchLog")->drop()->save;
}
if($this->getEnvironment() == "addressdb") {
}
}
}

View File

@@ -0,0 +1,51 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../config/config.php");
define('FRONKDB_SQLDEBUG', false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR . "/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
define("INTERNAL_USER_ID", $me->id);
define("INTERNAL_USER_USERNAME", $me->username);
define("MFBASE_BYPASS_LOGIN", true);
/*$dispatch = new MailtemplateDispatch(1);
$emails = $dispatch->getRecipients(true);
echo "empfänger emails: ".count($emails)."\n";
//$dispatch->sendToRecipients("fronk@fronk.at");
*/
foreach(MailtemplateDispatch::search(["send_finish" => null, "send_lock" => null]) as $dispatch) {
// check tosend date
if(!$dispatch->tosend_date || $dispatch->tosend_date > date('U')) continue;
// get notification again from DB in case send_lock was set
$dispatch = new MailtemplateDispatch($dispatch->id);
if($dispatch->send_lock) continue;
// first of all set send lock and start date
$mypid = getmypid();
if(!$dispatch->send_start) {
$dispatch->send_start = date("U");
}
$dispatch->send_lock = ($mypid) ? $mypid : 1;
$dispatch->save();
$dispatch->sendToRecipients();
// get Dispatch from DB again and update send lock and finish date
$dispatch = new MailtemplateDispatch($dispatch->id);
$dispatch->send_finish = date('U');
$dispatch->send_lock = null;
$dispatch->save();
}