Skip to content

Commit

Permalink
Log and archive ALL outgoing mail
Browse files Browse the repository at this point in the history
  • Loading branch information
vphantom committed May 11, 2017
1 parent 59b7080 commit 65f7763
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions src/Pyrite/Sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Sendmail
*
* Send e-mails based on the Email class and templating events.
* Send e-mails based on the Email class and templating events, keeping a full
* copy archived.
*
* PHP version 5
*
Expand Down Expand Up @@ -60,16 +61,18 @@ public static function install()
$db->exec(
"
CREATE TABLE IF NOT EXISTS 'emails' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender INTEGER NOT NULL DEFAULT '0',
isSent BOOL NOT NULL DEFAULT '0',
modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
recipients VARCHAR(255) NOT NULL DEFAULT '',
ccs VARCHAR(255) NOT NULL DEFAULT '',
bccs VARCHAR(255) NOT NULL DEFAULT '',
subject TEXT NOT NULL DEFAULT '',
html TEXT NOT NULL DEFAULT '',
files BLOB,
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender INTEGER NOT NULL DEFAULT '0',
isSent BOOL NOT NULL DEFAULT '0',
modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
contextType VARCHAR(64) DEFAULT NULL,
contextId INTEGER DEFAULT NULL,
recipients VARCHAR(255) NOT NULL DEFAULT '',
ccs VARCHAR(255) NOT NULL DEFAULT '',
bccs VARCHAR(255) NOT NULL DEFAULT '',
subject TEXT NOT NULL DEFAULT '',
html TEXT NOT NULL DEFAULT '',
files BLOB,
FOREIGN KEY(sender) REFERENCES users(id)
)
"
Expand Down Expand Up @@ -193,6 +196,9 @@ public static function setOutboxEmail($id, $to, $cc, $bcc, $subject, $html, $fil
$cols['files'] = json_encode($files);
};

$cols['contextType'] = $PPHP['contextType'];
$cols['contextId'] = $PPHP['contextId'];

if ($id) {
if (is_array($files)) {
$cols['files'] = json_encode($files);
Expand Down Expand Up @@ -238,18 +244,25 @@ public static function deleteOutboxEmail($id)
/**
* Send an e-mail from the user's outbox
*
* @param int $id The e-mail ID
* Note that $tampered is TRUE by default to match v1.0.0 API where only
* messages spooled in manual outboxes were then sent with this function.
*
* @param int $id The e-mail ID
* @param bool|null $tampered Did it go through manual outbox?
*
* @return bool Whether it succeeded
*/
public static function sendOutboxEmail($id)
public static function sendOutboxEmail($id, $tampered = true)
{
global $PPHP;
$db = $PPHP['db'];

$cc = null;
$bcc = null;

if (!$id) {
return false;
};
$email = self::getOutboxEmail($id);
if (!$email) {
return false;
Expand All @@ -260,14 +273,18 @@ public static function sendOutboxEmail($id)
$bcc = self::_usersToRecipients($email['bccs']);
if (self::_sendmail($to, $cc, $bcc, $email['subject'], $email['html'], $email['files'])) {
$db->update('emails', array('isSent' => true), 'WHERE id=?', array($id));
trigger(
'log',
array(
'action' => 'sent',
'objectType' => 'email',
'objectId' => $id
)
$logData = array(
'action' => 'emailed',
'newValue' => $id
);
if (!$tampered) {
$logData['userId'] = 0;
};
if ($email['contextType'] !== null && $email['contextId'] !== null) {
$logData['objectType'] = $email['contextType'];
$logData['objectId'] = $email['contextId'];
};
trigger('log', $logData);
trigger('outbox_changed');
return true;
};
Expand Down Expand Up @@ -379,13 +396,11 @@ public static function send($to, $cc, $bcc, $template, $args = array(), $files =
$bcc = array($bcc);
};

$email = self::setOutboxEmail(null, $to, $cc, $bcc, $blocks['subject'], $blocks['html'], $files);

if (pass('can', 'edit', 'email') && !$nodelay) {
return self::setOutboxEmail(null, $to, $cc, $bcc, $blocks['subject'], $blocks['html'], $files);
} else {
$to = self::_usersToRecipients($to);
$cc = self::_usersToRecipients($cc);
$bcc = self::_usersToRecipients($bcc);
return self::_sendmail($to, $cc, $bcc, $blocks['subject'], $blocks['html'], $files);
return $email;
};
return self::sendOutboxEmail($email, false);
}
}

0 comments on commit 65f7763

Please sign in to comment.