-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mailer.php
191 lines (165 loc) · 5.23 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* @link https://sprout.barrelstrengthdesign.com/
* @copyright Copyright (c) Barrel Strength Design LLC
* @license http://sprout.barrelstrengthdesign.com/license
*/
namespace barrelstrength\sproutbaseemail\base;
use barrelstrength\sproutbaseemail\models\SimpleRecipient;
use barrelstrength\sproutbaseemail\models\SimpleRecipientList;
use barrelstrength\sproutbaseemail\web\twig\EmailTemplateSandboxExtension;
use Craft;
use craft\base\Component;
use craft\mail\Message;
use Exception;
use Throwable;
use yii\base\Model;
/**
* Class Mailer
*
*
* @property SimpleRecipient[]|array $onTheFlyRecipients
* @property array $lists
* @property string $name
* @property string $description
* @property string $actionForPrepareModal
* @property string $prepareModalHtml
* @property SimpleRecipientList $recipientList
* @property string $recipientsHtml
* @property null|string $cpSettingsUrl
*/
abstract class Mailer extends Component
{
use RecipientsTrait;
use ModalWorkflowTrait;
/**
* The settings for this mailer
*
* @var Model
*/
protected $settings;
/**
* Returns the Mailer Title when used in string context
*
* @return string
*/
public function __toString()
{
return self::displayName();
}
/**
* Returns a short description of this mailer
*
* @return string
* @example The Sprout Email Mailer uses the Craft API to send emails
*
*/
abstract public function getDescription(): string;
/**
* Returns whether or not the mailer has registered routes to accomplish tasks within Sprout Email
*
* @return bool
*/
public function hasCpSection(): bool
{
return false;
}
/**
* Returns the URL for this Mailer's CP Settings
*
* @return null|string
*/
public function getCpSettingsUrl()
{
return null;
}
/**
* Returns the settings model for this mailer
*
* @return Model
*/
public function getSettings(): Model
{
$currentPluginHandle = Craft::$app->request->getSegment(1);
$plugin = Craft::$app->plugins->getPlugin($currentPluginHandle);
$settings = null;
if ($plugin) {
$settings = $plugin->getSettings();
}
return $settings;
}
/**
* Returns a rendered html string to use for capturing settings input
*
* @param array $settings
*
* @return string|Model
*/
public function getSettingsHtml(array $settings = [])
{
return '';
}
/**
* Prepares the NotificationEmail Element and returns a Message model.
*
* @param EmailElement $email
*
* @return Message
* @throws Throwable
* @throws \yii\base\Exception
*/
public function getMessage(EmailElement $email): Message
{
$object = $email->getEventObject();
$message = new Message();
// Render Email Entry fields that have dynamic values
$this->renderObjectTemplateSafely($email, 'subjectLine', $object);
$this->renderObjectTemplateSafely($email, 'fromName', $object);
$this->renderObjectTemplateSafely($email, 'fromEmail', $object);
$this->renderObjectTemplateSafely($email, 'replyToEmail', $object);
$this->renderObjectTemplateSafely($email, 'defaultBody', $object);
$message->setSubject($email->subjectLine);
$message->setFrom([$email->fromEmail => $email->fromName]);
$message->setReplyTo($email->replyToEmail);
// Our templates take a few steps to process
$textBody = '';
$htmlBody = '';
// Get the initial rendering of the templates
try {
$textBody = $email->getEmailTemplates()->getTextBody();
$htmlBody = $email->getEmailTemplates()->getHtmlBody();
} catch (Exception $e) {
$email->addError('template', $e->getMessage());
}
if (empty($textBody)) {
$email->addError('body', Craft::t('sprout-base-email', 'Text template is blank.'));
}
if (empty($htmlBody)) {
$email->addError('htmlBody', Craft::t('sprout-base-email', 'HTML template is blank.'));
}
$message->setTextBody($textBody);
$message->setHtmlBody($htmlBody);
// Make sure we use the HTML and Text after they are processed the second time
$email->getEmailTemplates()->setTextBody($textBody);
$email->getEmailTemplates()->setHtmlBody($htmlBody);
return $message;
}
/**
* Render a specific attribute on the EmailElement model and add an error to
* the model if something goes wrong.
*
* @param EmailElement $email
* @param $attribute
* @param $object
*
* @throws Throwable
*/
private function renderObjectTemplateSafely(EmailElement $email, $attribute, $object)
{
try {
$email->{$attribute} = Craft::$app->getView()->renderObjectTemplate($email->{$attribute}, $object);
} catch (Exception $e) {
$email->addError($email->{$attribute}, $e->getMessage());
}
}
}