-
Notifications
You must be signed in to change notification settings - Fork 641
/
Mailer.php
180 lines (153 loc) · 6.58 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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\mail;
use Craft;
use craft\elements\User;
use craft\enums\CmsEdition;
use craft\helpers\App;
use craft\helpers\Template;
use craft\web\View;
use Throwable;
use yii\base\InvalidConfigException;
use yii\helpers\Markdown;
use yii\mail\MailEvent;
/**
* The Mailer component provides APIs for sending email in Craft.
* An instance of the Mailer component is globally accessible in Craft via [[\craft\web\Application::mailer|`Craft::$app->mailer`]].
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class Mailer extends \yii\symfonymailer\Mailer
{
/**
* @event MailEvent The event that is triggered before a message is prepped to be sent.
* @since 3.6.5
*/
public const EVENT_BEFORE_PREP = 'beforePrep';
/**
* @var string|null The email template that should be used
*/
public ?string $template = null;
/**
* @var string|array|User|User[]|null The default sender’s email address, or their user model(s).
*/
public User|string|array|null $from = null;
/**
* @var string|array|User|User[]|null The default Reply-To email address, or their user model(s).
* @since 3.4.0
*/
public User|string|array|null $replyTo = null;
/**
* Composes a new email based on a given key.
*
* Craft has four predefined email keys: account_activation, verify_new_email, forgot_password and test_email.
* Plugins can register additional email keys using the
* [registerEmailMessages](http://craftcms.com/docs/plugins/hooks-reference#registerEmailMessages) hook, and
* by providing the corresponding language strings.
*
* ```php
* Craft::$app->mailer->composeFromKey('account_activation', [
* 'link' => $activationUrl
* ]);
* ```
*
* @param string $key The email key
* @param array $variables Any variables that should be passed to the email body template
* @return Message The new email message
* @throws InvalidConfigException if [[messageConfig]] or [[class]] is not configured to use [[Message]]
*/
public function composeFromKey(string $key, array $variables = []): Message
{
$message = $this->createMessage();
if (!$message instanceof Message) {
throw new InvalidConfigException('Mailer must be configured to create messages with craft\mail\Message (or a subclass).');
}
$message->key = $key;
$message->variables = $variables;
return $message;
}
/**
* @inheritdoc
*/
public function send($message): bool
{
// Fire a 'beforePrep' event
$this->trigger(self::EVENT_BEFORE_PREP, new MailEvent([
'message' => $message,
]));
$generalConfig = Craft::$app->getConfig()->getGeneral();
if ($message instanceof Message && $message->key !== null) {
if ($message->language === null) {
// Default to the current language
$message->language = Craft::$app->getRequest()->getIsSiteRequest()
? Craft::$app->language
: Craft::$app->getSites()->getPrimarySite()->language;
}
$systemMessage = Craft::$app->getSystemMessages()->getMessage($message->key, $message->language);
// Use the message language
$language = Craft::$app->language;
Craft::$app->language = $message->language;
$settings = App::mailSettings();
$variables = ($message->variables ?: []) + [
'emailKey' => $message->key,
'fromEmail' => App::parseEnv($settings->fromEmail),
'replyToEmail' => App::parseEnv($settings->replyToEmail),
'fromName' => App::parseEnv($settings->fromName),
'language' => $message->language,
];
// Temporarily disable lazy transform generation
$generateTransformsBeforePageLoad = $generalConfig->generateTransformsBeforePageLoad;
$generalConfig->generateTransformsBeforePageLoad = true;
// Render the subject and body text
$view = Craft::$app->getView();
$subject = $view->renderString($systemMessage->subject, $variables, View::TEMPLATE_MODE_SITE);
$textBody = $view->renderString($systemMessage->body, $variables, View::TEMPLATE_MODE_SITE);
$htmlBody = $view->renderString($systemMessage->body, $variables, View::TEMPLATE_MODE_SITE, true);
// Remove </> from around URLs, so they’re not interpreted as HTML tags
$textBody = preg_replace('/<(https?:\/\/.+?)>/', '$1', $textBody);
$message->setSubject($subject);
$message->setTextBody($textBody);
// Is there a custom HTML template set?
if (Craft::$app->edition->value >= CmsEdition::Pro->value && $this->template) {
$template = $this->template;
$templateMode = View::TEMPLATE_MODE_SITE;
} else {
// Default to the _special/email.html template
$template = '_special/email.twig';
$templateMode = View::TEMPLATE_MODE_CP;
}
try {
$message->setHtmlBody($view->renderTemplate($template, array_merge($variables, [
'body' => Template::raw(Markdown::process($htmlBody)),
]), $templateMode));
} catch (Throwable $e) {
// Just log it and don't worry about the HTML body
Craft::warning('Error rendering email template: ' . $e->getMessage(), __METHOD__);
Craft::$app->getErrorHandler()->logException($e);
}
// Set things back to normal
Craft::$app->language = $language;
$generalConfig->generateTransformsBeforePageLoad = $generateTransformsBeforePageLoad;
}
// Set the default sender if there isn't one already
if (!$message->getFrom()) {
$message->setFrom($this->from);
}
if ($this->replyTo && !$message->getReplyTo()) {
$message->setReplyTo($this->replyTo);
}
// Apply the testToEmailAddress config setting
$testToEmailAddress = $generalConfig->getTestToEmailAddress();
if (!empty($testToEmailAddress)) {
$message->setTo($testToEmailAddress);
$message->setCc([]);
$message->setBcc([]);
}
return parent::send($message);
}
}