aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/core/src/Mail
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-16 21:09:05 +0200
committermarvin-borner@live.com2018-04-16 21:09:05 +0200
commitcf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch)
tree86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/core/src/Mail
parent619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff)
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/core/src/Mail')
-rwxr-xr-xmain/app/sprinkles/core/src/Mail/EmailRecipient.php136
-rwxr-xr-xmain/app/sprinkles/core/src/Mail/MailMessage.php186
-rwxr-xr-xmain/app/sprinkles/core/src/Mail/Mailer.php204
-rwxr-xr-xmain/app/sprinkles/core/src/Mail/StaticMailMessage.php78
-rwxr-xr-xmain/app/sprinkles/core/src/Mail/TwigMailMessage.php93
5 files changed, 697 insertions, 0 deletions
diff --git a/main/app/sprinkles/core/src/Mail/EmailRecipient.php b/main/app/sprinkles/core/src/Mail/EmailRecipient.php
new file mode 100755
index 0000000..0b9381a
--- /dev/null
+++ b/main/app/sprinkles/core/src/Mail/EmailRecipient.php
@@ -0,0 +1,136 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Mail;
+
+/**
+ * EmailRecipient Class
+ *
+ * A class representing a recipient for a MailMessage, with associated parameters.
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+class EmailRecipient
+{
+
+ /**
+ * @var string The email address for this recipient.
+ */
+ protected $email;
+
+ /**
+ * @var string The name for this recipient.
+ */
+ protected $name;
+
+ /**
+ * @var array Any additional parameters (name => value) to use when rendering an email template for this recipient.
+ */
+ protected $params = [];
+
+ /**
+ * @var array A list of CCs for this recipient. Each CC is an associative array with `email` and `name` properties.
+ */
+ protected $cc = [];
+
+ /**
+ * @var array A list of BCCs for this recipient. Each BCC is an associative array with `email` and `name` properties.
+ */
+ protected $bcc = [];
+
+ /**
+ * Create a new EmailRecipient instance.
+ *
+ * @param string $email The primary recipient email address.
+ * @param string $name The primary recipient name.
+ * @param array $params An array of template parameters to render the email message with for this particular recipient.
+ */
+ public function __construct($email, $name = "", $params = [])
+ {
+ $this->email = $email;
+ $this->name = $name;
+ $this->params = $params;
+ }
+
+ /**
+ * Add a CC for this primary recipient.
+ *
+ * @param string $email The CC recipient email address.
+ * @param string $name The CC recipient name.
+ */
+ public function cc($email, $name = "")
+ {
+ $this->cc[] = [
+ "email" => $email,
+ "name" => $name
+ ];
+ }
+
+ /**
+ * Add a BCC for this primary recipient.
+ *
+ * @param string $email The BCC recipient email address.
+ * @param string $name The BCC recipient name.
+ */
+ public function bcc($email, $name = "")
+ {
+ $this->bcc[] = [
+ "email" => $email,
+ "name" => $name
+ ];
+ }
+
+ /**
+ * Get the primary recipient email address.
+ *
+ * @return string the primary recipient email address.
+ */
+ public function getEmail()
+ {
+ return $this->email;
+ }
+
+ /**
+ * Get the primary recipient name.
+ *
+ * @return string the primary recipient name.
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Get the parameters to use when rendering the template this recipient.
+ *
+ * @return array The parameters (name => value) to use when rendering an email template for this recipient.
+ */
+ public function getParams()
+ {
+ return $this->params;
+ }
+
+ /**
+ * Get the list of CCs for this recipient.
+ *
+ * @return array A list of CCs for this recipient. Each CC is an associative array with `email` and `name` properties.
+ */
+ public function getCCs()
+ {
+ return $this->cc;
+ }
+
+ /**
+ * Get the list of BCCs for this recipient.
+ *
+ * @return array A list of BCCs for this recipient. Each BCC is an associative array with `email` and `name` properties.
+ */
+ public function getBCCs()
+ {
+ return $this->bcc;
+ }
+}
diff --git a/main/app/sprinkles/core/src/Mail/MailMessage.php b/main/app/sprinkles/core/src/Mail/MailMessage.php
new file mode 100755
index 0000000..29bcf15
--- /dev/null
+++ b/main/app/sprinkles/core/src/Mail/MailMessage.php
@@ -0,0 +1,186 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Mail;
+
+/**
+ * MailMessage Class
+ *
+ * Represents a basic mail message, containing a static subject and body.
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+abstract class MailMessage
+{
+ /**
+ * @var string The current sender email address.
+ */
+ protected $fromEmail = "";
+
+ /**
+ * @var string The current sender name.
+ */
+ protected $fromName = null;
+
+ /**
+ * @var EmailRecipient[] A list of recipients for this message.
+ */
+ protected $recipients = [];
+
+ /**
+ * @var string The current reply-to email.
+ */
+ protected $replyEmail = null;
+
+ /**
+ * @var string The current reply-to name.
+ */
+ protected $replyName = null;
+
+ /**
+ * Gets the fully rendered text of the message body.
+ *
+ * @return string
+ */
+ abstract public function renderBody($params = []);
+
+ /**
+ * Gets the fully rendered text of the message subject.
+ *
+ * @return string
+ */
+ abstract public function renderSubject($params = []);
+
+ /**
+ * Add an email recipient.
+ *
+ * @param EmailRecipient $recipient
+ */
+ public function addEmailRecipient(EmailRecipient $recipient)
+ {
+ $this->recipients[] = $recipient;
+ return $this;
+ }
+
+ /**
+ * Clears out all recipients for this message.
+ */
+ public function clearRecipients()
+ {
+ $this->recipients = array();
+ }
+
+ /**
+ * Set sender information for this message.
+ *
+ * This is a shortcut for calling setFromEmail, setFromName, setReplyEmail, and setReplyName.
+ * @param string $fromInfo An array containing 'email', 'name', 'reply_email', and 'reply_name'.
+ */
+ public function from($fromInfo = [])
+ {
+ $this->setFromEmail(isset($fromInfo['email']) ? $fromInfo['email'] : "");
+ $this->setFromName(isset($fromInfo['name']) ? $fromInfo['name'] : null);
+ $this->setReplyEmail(isset($fromInfo['reply_email']) ? $fromInfo['reply_email'] : null);
+ $this->setReplyName(isset($fromInfo['reply_name']) ? $fromInfo['reply_name'] : null);
+
+ return $this;
+ }
+
+ /**
+ * Get the sender email address.
+ *
+ * @return string
+ */
+ public function getFromEmail()
+ {
+ return $this->fromEmail;
+ }
+
+ /**
+ * Get the sender name. Defaults to the email address if name is not set.
+ *
+ * @return string
+ */
+ public function getFromName()
+ {
+ return isset($this->fromName) ? $this->fromName : $this->getFromEmail();
+ }
+
+ /**
+ * Get the list of recipients for this message.
+ *
+ * @return EmailRecipient[]
+ */
+ public function getRecipients()
+ {
+ return $this->recipients;
+ }
+
+ /**
+ * Get the 'reply-to' address for this message. Defaults to the sender email.
+ *
+ * @return string
+ */
+ public function getReplyEmail()
+ {
+ return isset($this->replyEmail) ? $this->replyEmail : $this->getFromEmail();
+ }
+
+ /**
+ * Get the 'reply-to' name for this message. Defaults to the sender name.
+ *
+ * @return string
+ */
+ public function getReplyName()
+ {
+ return isset($this->replyName) ? $this->replyName : $this->getFromName();
+ }
+
+ /**
+ * Set the sender email address.
+ *
+ * @param string $fromEmail
+ */
+ public function setFromEmail($fromEmail)
+ {
+ $this->fromEmail = $fromEmail;
+ return $this;
+ }
+
+ /**
+ * Set the sender name.
+ *
+ * @param string $fromName
+ */
+ public function setFromName($fromName)
+ {
+ $this->fromName = $fromName;
+ return $this;
+ }
+
+ /**
+ * Set the sender 'reply-to' address.
+ *
+ * @param string $replyEmail
+ */
+ public function setReplyEmail($replyEmail)
+ {
+ $this->replyEmail = $replyEmail;
+ return $this;
+ }
+
+ /**
+ * Set the sender 'reply-to' name.
+ *
+ * @param string $replyName
+ */
+ public function setReplyName($replyName)
+ {
+ $this->replyName = $replyName;
+ return $this;
+ }
+}
diff --git a/main/app/sprinkles/core/src/Mail/Mailer.php b/main/app/sprinkles/core/src/Mail/Mailer.php
new file mode 100755
index 0000000..5b346b4
--- /dev/null
+++ b/main/app/sprinkles/core/src/Mail/Mailer.php
@@ -0,0 +1,204 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Mail;
+
+use Monolog\Logger;
+
+/**
+ * Mailer Class
+ *
+ * A basic wrapper for sending template-based emails.
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+class Mailer
+{
+ /**
+ * @var Logger
+ */
+ protected $logger;
+
+ /**
+ * @var \PHPMailer
+ */
+ protected $phpMailer;
+
+ /**
+ * Create a new Mailer instance.
+ *
+ * @param Logger $logger A Monolog logger, used to dump debugging info for SMTP server transactions.
+ * @param mixed[] $config An array of configuration parameters for phpMailer.
+ * @throws \phpmailerException Wrong mailer config value given.
+ */
+ public function __construct($logger, $config = [])
+ {
+ $this->logger = $logger;
+
+ // 'true' tells PHPMailer to use exceptions instead of error codes
+ $this->phpMailer = new \PHPMailer(true);
+
+ // Configuration options
+ if (isset($config['mailer'])) {
+ if (!in_array($config['mailer'], ['smtp', 'mail', 'qmail', 'sendmail'])) {
+ throw new \phpmailerException("'mailer' must be one of 'smtp', 'mail', 'qmail', or 'sendmail'.");
+ }
+
+ if ($config['mailer'] == 'smtp') {
+ $this->phpMailer->isSMTP(true);
+ $this->phpMailer->Host = $config['host'];
+ $this->phpMailer->Port = $config['port'];
+ $this->phpMailer->SMTPAuth = $config['auth'];
+ $this->phpMailer->SMTPSecure = $config['secure'];
+ $this->phpMailer->Username = $config['username'];
+ $this->phpMailer->Password = $config['password'];
+ $this->phpMailer->SMTPDebug = $config['smtp_debug'];
+
+ if (isset($config['smtp_options'])) {
+ $this->phpMailer->SMTPOptions = $config['smtp_options'];
+ }
+ }
+
+ // Set any additional message-specific options
+ // TODO: enforce which options can be set through this subarray
+ if (isset($config['message_options'])) {
+ $this->setOptions($config['message_options']);
+ }
+ }
+
+ // Pass logger into phpMailer object
+ $this->phpMailer->Debugoutput = function($message, $level) {
+ $this->logger->debug($message);
+ };
+ }
+
+ /**
+ * Get the underlying PHPMailer object.
+ *
+ * @return \PHPMailer
+ */
+ public function getPhpMailer()
+ {
+ return $this->phpMailer;
+ }
+
+ /**
+ * Send a MailMessage message.
+ *
+ * Sends a single email to all recipients, as well as their CCs and BCCs.
+ * Since it is a single-header message, recipient-specific template data will not be included.
+ * @param MailMessage $message
+ * @param bool $clearRecipients Set to true to clear the list of recipients in the message after calling send(). This helps avoid accidentally sending a message multiple times.
+ * @throws \phpmailerException The message could not be sent.
+ */
+ public function send(MailMessage $message, $clearRecipients = true)
+ {
+ $this->phpMailer->From = $message->getFromEmail();
+ $this->phpMailer->FromName = $message->getFromName();
+ $this->phpMailer->addReplyTo($message->getReplyEmail(), $message->getReplyName());
+
+ // Add all email recipients, as well as their CCs and BCCs
+ foreach ($message->getRecipients() as $recipient) {
+ $this->phpMailer->addAddress($recipient->getEmail(), $recipient->getName());
+
+ // Add any CCs and BCCs
+ if ($recipient->getCCs()) {
+ foreach($recipient->getCCs() as $cc) {
+ $this->phpMailer->addCC($cc['email'], $cc['name']);
+ }
+ }
+
+ if ($recipient->getBCCs()) {
+ foreach($recipient->getBCCs() as $bcc) {
+ $this->phpMailer->addBCC($bcc['email'], $bcc['name']);
+ }
+ }
+ }
+
+ $this->phpMailer->Subject = $message->renderSubject();
+ $this->phpMailer->Body = $message->renderBody();
+
+ // Try to send the mail. Will throw an exception on failure.
+ $this->phpMailer->send();
+
+ // Clear recipients from the PHPMailer object for this iteration,
+ // so that we can use the same object for other emails.
+ $this->phpMailer->clearAllRecipients();
+
+ // Clear out the MailMessage's internal recipient list
+ if ($clearRecipients) {
+ $message->clearRecipients();
+ }
+ }
+
+ /**
+ * Send a MailMessage message, sending a separate email to each recipient.
+ *
+ * If the message object supports message templates, this will render the template with the corresponding placeholder values for each recipient.
+ * @param MailMessage $message
+ * @param bool $clearRecipients Set to true to clear the list of recipients in the message after calling send(). This helps avoid accidentally sending a message multiple times.
+ * @throws \phpmailerException The message could not be sent.
+ */
+ public function sendDistinct(MailMessage $message, $clearRecipients = true)
+ {
+ $this->phpMailer->From = $message->getFromEmail();
+ $this->phpMailer->FromName = $message->getFromName();
+ $this->phpMailer->addReplyTo($message->getReplyEmail(), $message->getReplyName());
+
+ // Loop through email recipients, sending customized content to each one
+ foreach ($message->getRecipients() as $recipient) {
+ $this->phpMailer->addAddress($recipient->getEmail(), $recipient->getName());
+
+ // Add any CCs and BCCs
+ if ($recipient->getCCs()) {
+ foreach($recipient->getCCs() as $cc) {
+ $this->phpMailer->addCC($cc['email'], $cc['name']);
+ }
+ }
+
+ if ($recipient->getBCCs()) {
+ foreach($recipient->getBCCs() as $bcc) {
+ $this->phpMailer->addBCC($bcc['email'], $bcc['name']);
+ }
+ }
+
+ $this->phpMailer->Subject = $message->renderSubject($recipient->getParams());
+ $this->phpMailer->Body = $message->renderBody($recipient->getParams());
+
+ // Try to send the mail. Will throw an exception on failure.
+ $this->phpMailer->send();
+
+ // Clear recipients from the PHPMailer object for this iteration,
+ // so that we can send a separate email to the next recipient.
+ $this->phpMailer->clearAllRecipients();
+ }
+
+ // Clear out the MailMessage's internal recipient list
+ if ($clearRecipients) {
+ $message->clearRecipients();
+ }
+ }
+
+ /**
+ * Set option(s) on the underlying phpMailer object.
+ *
+ * @param mixed[] $options
+ * @return Mailer
+ */
+ public function setOptions($options)
+ {
+ if (isset($options['isHtml'])) {
+ $this->phpMailer->isHTML($options['isHtml']);
+ }
+
+ foreach ($options as $name => $value) {
+ $this->phpMailer->set($name, $value);
+ }
+
+ return $this;
+ }
+}
diff --git a/main/app/sprinkles/core/src/Mail/StaticMailMessage.php b/main/app/sprinkles/core/src/Mail/StaticMailMessage.php
new file mode 100755
index 0000000..098bbfc
--- /dev/null
+++ b/main/app/sprinkles/core/src/Mail/StaticMailMessage.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Mail;
+
+/**
+ * StaticMailMessage Class
+ *
+ * Represents a basic mail message, containing a static subject and body.
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+class StaticMailMessage extends MailMessage
+{
+ /**
+ * @var string The default body for this message.
+ */
+ protected $body;
+
+ /**
+ * @var string The default subject for this message.
+ */
+ protected $subject;
+
+ /**
+ * Create a new MailMessage instance.
+ *
+ * @param string $subject
+ * @param string $body
+ */
+ public function __construct($subject = "", $body = "")
+ {
+ $this->subject = $subject;
+ $this->body = $body;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function renderBody($params = [])
+ {
+ return $this->body;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function renderSubject($params = [])
+ {
+ return $this->subject;
+ }
+
+ /**
+ * Set the text of the message subject.
+ *
+ * @param string $subject
+ */
+ public function setSubject($subject)
+ {
+ $this->subject = $subject;
+ return $this;
+ }
+
+ /**
+ * Set the text of the message body.
+ *
+ * @param string $body
+ */
+ public function setBody($body)
+ {
+ $this->body = $body;
+ return $this;
+ }
+}
diff --git a/main/app/sprinkles/core/src/Mail/TwigMailMessage.php b/main/app/sprinkles/core/src/Mail/TwigMailMessage.php
new file mode 100755
index 0000000..aa65240
--- /dev/null
+++ b/main/app/sprinkles/core/src/Mail/TwigMailMessage.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Mail;
+
+/**
+ * MailMessage Class
+ *
+ * Represents a basic mail message, containing a static subject and body.
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+class TwigMailMessage extends MailMessage
+{
+ /**
+ * @var mixed[] A list of Twig placeholder values to use when rendering this message.
+ */
+ protected $params;
+
+ /**
+ * @var Twig_Template The Twig template object, to source the content for this message.
+ */
+ protected $template;
+
+ /**
+ * @var \Slim\Views\Twig The view object, used to render mail templates.
+ */
+ protected $view;
+
+ /**
+ * Create a new TwigMailMessage instance.
+ *
+ * @param Slim\Views\Twig $view The Twig view object used to render mail templates.
+ * @param string $filename optional Set the Twig template to use for this message.
+ */
+ public function __construct($view, $filename = null)
+ {
+ $this->view = $view;
+
+ $twig = $this->view->getEnvironment();
+ // Must manually merge in global variables for block rendering
+ // TODO: should we keep this separate from the local parameters?
+ $this->params = $twig->getGlobals();
+
+ if ($filename !== null) {
+ $this->template = $twig->loadTemplate($filename);
+ }
+ }
+
+ /**
+ * Merge in any additional global Twig variables to use when rendering this message.
+ *
+ * @param mixed[] $params
+ */
+ public function addParams($params = [])
+ {
+ $this->params = array_replace_recursive($this->params, $params);
+ return $this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function renderSubject($params = [])
+ {
+ $params = array_replace_recursive($this->params, $params);
+ return $this->template->renderBlock('subject', $params);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function renderBody($params = [])
+ {
+ $params = array_replace_recursive($this->params, $params);
+ return $this->template->renderBlock('body', $params);
+ }
+
+ /**
+ * Sets the Twig template object for this message.
+ *
+ * @param Twig_Template $template The Twig template object, to source the content for this message.
+ */
+ public function setTemplate($template)
+ {
+ $this->template = $template;
+ return $this;
+ }
+}