forked from craftcms/contact-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.php
236 lines (201 loc) · 7.26 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace craft\contactform;
use Craft;
use craft\contactform\events\SendEvent;
use craft\contactform\models\Submission;
use craft\elements\User;
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use craft\mail\Message;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Markdown;
class Mailer extends Component
{
/**
* @event SubmissionEvent The event that is triggered before a message is sent
*/
const EVENT_BEFORE_SEND = 'beforeSend';
/**
* @event SubmissionEvent The event that is triggered after a message is sent
*/
const EVENT_AFTER_SEND = 'afterSend';
/**
* Sends an email submitted through a contact form.
*
* @param Submission $submission
* @param bool $runValidation Whether the section should be validated
* @throws InvalidConfigException if the plugin settings don't validate
* @return bool
*/
public function send(Submission $submission, bool $runValidation = true): bool
{
// Get the plugin settings and make sure they validate before doing anything
$settings = Plugin::getInstance()->getSettings();
if (!$settings->validate()) {
throw new InvalidConfigException('The Contact Form settings don’t validate.');
}
if ($runValidation && !$submission->validate()) {
Craft::info('Contact form submission not saved due to validation error.', __METHOD__);
return false;
}
$mailer = Craft::$app->getMailer();
// Prep the message
$fromEmail = $this->getFromEmail($mailer->from);
$fromName = $this->compileFromName($submission->fromName);
$subject = $this->compileSubject($submission->subject);
$textBody = $this->compileTextBody($submission);
$htmlBody = $this->compileHtmlBody($textBody);
// Flag for file attachment validation.
$validAttachments = true;
$message = (new Message())
->setFrom([$fromEmail => $fromName])
->setReplyTo([$submission->fromEmail => $submission->fromName])
->setSubject($subject)
->setTextBody($textBody)
->setHtmlBody($htmlBody);
if ($submission->attachment !== null) {
$allowedFileTypes = Craft::$app->getConfig()->getGeneral()->allowedFileExtensions;
foreach ($submission->attachment as $attachment) {
if (!$attachment) {
continue;
}
// Validate that the file is safe to send by e-mail
$extension = pathinfo($attachment->name, PATHINFO_EXTENSION);
if (!in_array(strtolower($extension), $allowedFileTypes)) {
$validAttachments = false;
}
$message->attach($attachment->tempName, [
'fileName' => $attachment->name,
'contentType' => FileHelper::getMimeType($attachment->tempName),
]);
}
}
// Grab any "to" emails set in the plugin settings.
$toEmails = Craft::parseEnv($settings->toEmail);
$toEmails = is_string($toEmails) ? StringHelper::split($toEmails) : $toEmails;
// Fire a 'beforeSend' event
$event = new SendEvent([
'submission' => $submission,
'message' => $message,
'toEmails' => $toEmails,
]);
$this->trigger(self::EVENT_BEFORE_SEND, $event);
if ($event->isSpam) {
Craft::warning('Contact form submission suspected to be spam.', __METHOD__);
return true;
}
if($validAttachments === false) {
Craft::error('Contact form submission contains a disallowed filetype.', __METHOD__);
return false;
}
foreach ($event->toEmails as $toEmail) {
$message->setTo($toEmail);
$mailer->send($message);
}
// Fire an 'afterSend' event
if ($this->hasEventHandlers(self::EVENT_AFTER_SEND)) {
$this->trigger(self::EVENT_AFTER_SEND, new SendEvent([
'submission' => $submission,
'message' => $message,
'toEmails' => $event->toEmails,
]));
}
return true;
}
/**
* Returns the "From" email value on the given mailer $from property object.
*
* @param string|array|User|User[]|null $from
* @return string
* @throws InvalidConfigException if it can’t be determined
*/
public function getFromEmail($from): string
{
if (is_string($from)) {
return $from;
}
if ($from instanceof User) {
return $from->email;
}
if (is_array($from)) {
$first = reset($from);
$key = key($from);
if (is_numeric($key)) {
return $this->getFromEmail($first);
}
return $key;
}
throw new InvalidConfigException('Can\'t determine "From" email from email config settings.');
}
/**
* Compiles the "From" name value from the submitted name.
*
* @param string|null $fromName
* @return string
*/
public function compileFromName(string $fromName = null): string
{
$settings = Plugin::getInstance()->getSettings();
return $settings->prependSender.($settings->prependSender && $fromName ? ' ' : '').$fromName;
}
/**
* Compiles the real email subject from the submitted subject.
*
* @param string|null $subject
* @return string
*/
public function compileSubject(string $subject = null): string
{
$settings = Plugin::getInstance()->getSettings();
return $settings->prependSubject.($settings->prependSubject && $subject ? ' - ' : '').$subject;
}
/**
* Compiles the real email textual body from the submitted message.
*
* @param Submission $submission
* @return string
*/
public function compileTextBody(Submission $submission): string
{
$fields = [];
if ($submission->fromName) {
$fields[Craft::t('contact-form', 'Name')] = $submission->fromName;
}
$fields[Craft::t('contact-form', 'Email')] = $submission->fromEmail;
if (is_array($submission->message)) {
$body = $submission->message['body'] ?? '';
$fields = array_merge($fields, $submission->message);
unset($fields['body']);
} else {
$body = (string)$submission->message;
}
$text = '';
foreach ($fields as $key => $value) {
$text .= ($text ? "\n" : '')."- **{$key}:** ";
if (is_array($value)) {
$text .= implode(', ', $value);
} else {
$text .= $value;
}
}
if ($body !== '') {
$body = preg_replace('/\R/u', "\n\n", $body);
$text .= "\n\n".$body;
}
return $text;
}
/**
* Compiles the real email HTML body from the compiled textual body.
*
* @param string $textBody
* @return string
*/
public function compileHtmlBody(string $textBody): string
{
$html = Html::encode($textBody);
$html = Markdown::process($html);
return $html;
}
}