BulkSubmissionEmail.php
6.2 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
if (!defined('ABSPATH'))
exit;
use NF_Exports_BulkSubmissionEmailParameters as BulkSubmissionEmailParameters;
/**
* Email exported submissions as an attachment
*/
class NF_Exports_BulkSubmissionEmail {
/**
* Bulk Submission Email Parameters
* @var BulkSubmissionEmailParameters
*/
protected $bulkSubmissionEmailParameters;
/**
* Comma delineated `To` addresses
* @var string
*/
protected $toAddresses;
/**
* `From` address
* @var string
*/
protected $fromAddress;
/**
* `Reply To` address
* @var string
*/
protected $replyTo;
/**
* `Subject`
* @var string
*/
protected $subject;
/**
* CSV string content, stored in array for multiple attachments
* @var array
*/
protected $content;
/**
*
* @var resource
*/
protected $tempFile;
/**
* Directory of final file location
* @var string
*/
protected $dir;
/**
* Temp file name at time of upload, before renaming
* @var string
*/
protected $basename;
/**
* Full file name with path as attached to email
* @var string
*/
protected $attachmentFilename;
/**
* Instantiated with BulkSubmissionEmailParameters and string CSV content
*
* @param BulkSubmissionEmailParameters $bulkSubmissionEmailParameters
* @param array $attachmentFilenames Array of string filenames ready for attachment
*/
public function __construct($bulkSubmissionEmailParameters, array $attachmentFilenames) {
$this->bulkSubmissionEmailParameters = $bulkSubmissionEmailParameters;
$this->attachmentFilename = $attachmentFilenames;
$this->setDefaults();
}
/**
* Set default properties
*/
protected function setDefaults() {
// set upload director to /uploads
$dir = wp_upload_dir();
$this->uploadDir = $dir['path'];
}
/**
* Generate email, attach content, submit email
*/
public function handle() {
$this->sanitizeAddressFields();
$headers = $this->getHeaders();
$attachments = $this->getAttachments();
$message = apply_filters('ninja_forms_action_email_message', $this->bulkSubmissionEmailParameters->getEmailSubject());
try {
$sent = wp_mail($this->toAddresses, strip_tags($this->bulkSubmissionEmailParameters->getEmailSubject()), $message, $headers, $attachments);
} catch (Exception $e) {
$sent = false;
}
}
/**
* Put every email address through a sanitizing method
*/
protected function sanitizeAddressFields() {
$incomingToAddresses = $this->bulkSubmissionEmailParameters->getEmailTo();
$emailAddresses = explode(',', $incomingToAddresses);
// Loop over our email addresses.
foreach ($emailAddresses as $email) {
$sanitized = $this->sanitizeEmail($email);
// Build our array of the email addresses.
$sanitizedArray[] = $sanitized;
}
$this->toAddresses = implode(',', $sanitizedArray);
// Sanitized our array of settings.
$this->fromAddress = $this->sanitizeEmail($this->bulkSubmissionEmailParameters->getEmailFrom());
$this->replyTo = $this->bulkSubmissionEmailParameters->getEmailReplyTo();
}
/**
* Sanitize a given email address
*
* @param string $incoming
* @return string
*/
protected function sanitizeEmail($incoming) {
// Trim values in case there is a value with spaces/tabs/etc to remove whitespace
$trimmed = trim($incoming);
if (empty($trimmed)) {
return '';
}
$matches = [];
if (false !== strpos($trimmed, '<') && false !== strpos($trimmed, '>')) {
preg_match('/(?:<)([^>]*)(?:>)/', $trimmed, $matches);
$return = $matches[1];
} else {
$return = $trimmed;
}
// skip if email is invalid
if (!is_email($return)) {
return '';
}
return $return;
}
/**
* Construct and return header array
*
* Note that variable headers are run through sanitize_header method
* @return array
*/
private function getHeaders() {
$contentHeaders = [];
$contentHeaders[] = 'Content-Type: text/html';
$contentHeaders[] = 'charset=UTF-8';
$contentHeaders[] = 'X-Ninja-Forms:ninja-forms'; // Flag for transactional email.
$contentHeaders[] = $this->formatAddress('from', $this->fromAddress);
$headers = array_merge($contentHeaders, $this->constructRecipientsHeader());
return $headers;
}
/**
* Sanitize header to prevent attacker is able to create new headers using charecter encoding.
*
* @param string $header
* @return void
*/
protected function sanitize_header($header){
return preg_replace( '=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $header );
}
/**
* Construct and return attachments
* @return array
*/
private function getAttachments() {
$attachments = $this->attachmentFilename;
return $attachments;
}
/**
* Format Reply-To, CC, and BCC address header
* @return array
*/
private function constructRecipientsHeader() {
$headers = [];
// Could include `cc` and `bcc` in future
$recipientParameters = array(
'Reply-to' => $this->bulkSubmissionEmailParameters->getEmailReplyTo(),
);
foreach ($recipientParameters as $type => $email) {
if (!$email) {
continue;
}
$headers[] = $this->formatAddress($type, $email);
}
return $headers;
}
/**
* Format address for header
*
* @param string $type
* @param string $email
* @param string $name
* @return string
*/
private function formatAddress($type, $email, $name = '') {
$formattedType = ucfirst($type);
if (!$name) {
$name = $email;
}
$recipient = "$formattedType: $name <$email>";
return $this->sanitize_header($recipient);
}
}