PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.4.5.2
JetFormBuilder — Dynamic Blocks Form Builder v3.4.5.2
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / actions-v2 / send-email / send-email-action.php
jetformbuilder / modules / actions-v2 / send-email Last commit date
assets 1 year ago send-email-action.php 1 year ago send-email-hooks.php 1 year ago send-email.php 1 year ago
send-email-action.php
612 lines
1 <?php
2
3 namespace JFB_Modules\Actions_V2\Send_Email;
4
5 use Jet_Form_Builder\Actions\Action_Handler;
6 use Jet_Form_Builder\Actions\Types\Base;
7 use Jet_Form_Builder\Classes\Http\Http_Tools;
8 use Jet_Form_Builder\Classes\Tools;
9 use Jet_Form_Builder\Exceptions\Action_Exception;
10 use Jet_Form_Builder\Request\Request_Tools;
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Define Base_Type class
19 */
20 class Send_Email_Action extends Base {
21
22 private $content_type = '';
23 private $mail_to = array();
24 private $cc = array();
25 private $bcc = array();
26 private $reply_to = '';
27 private $from_email = '';
28 private $content = '';
29 private $subject = '';
30 private $from_name = '';
31 private $attachments = array();
32 private $headers = '';
33
34 public function get_name() {
35 return __( 'Send Email', 'jet-form-builder' );
36 }
37
38 public function get_id() {
39 return 'send_email';
40 }
41
42 public function self_script_name() {
43 return 'jetFormEmailData';
44 }
45
46 public function editor_labels() {
47 return array(
48 'mail_to' => __( 'Mail To:', 'jet-form-builder' ),
49 'custom_email' => __( 'Email Address:', 'jet-form-builder' ),
50 'from_field' => __( 'From Field:', 'jet-form-builder' ),
51 'reply_to' => __( 'Reply To:', 'jet-form-builder' ),
52 'reply_to_email' => __( 'Reply to Email Address:', 'jet-form-builder' ),
53 'reply_from_field' => __( 'Reply To Email From Field:', 'jet-form-builder' ),
54 'subject' => __( 'Subject:', 'jet-form-builder' ),
55 'from_name' => __( 'From Name:', 'jet-form-builder' ),
56 'from_address' => __( 'From Email Address:', 'jet-form-builder' ),
57 'content_type' => __( 'Content type:', 'jet-form-builder' ),
58 'disable_format' => __( 'Disable Auto-Formatting', 'jet-form-builder' ),
59 'content' => __( 'Content:', 'jet-form-builder' ),
60 'attachments' => __( 'Attachments:', 'jet-form-builder' ),
61 'add_attachment' => __( 'Add form field with attachment', 'jet-form-builder' ),
62 );
63 }
64
65 public function action_attributes() {
66 return array(
67 'mail_to' => array(
68 'default' => '',
69 ),
70 'custom_email' => array(
71 'default' => '',
72 ),
73 'from_field' => array(
74 'default' => '',
75 ),
76 'reply_to' => array(
77 'default' => '',
78 ),
79 'reply_to_email' => array(
80 'default' => '',
81 ),
82 'reply_from_field' => array(
83 'default' => '',
84 ),
85 'subject' => array(
86 'default' => '',
87 'path' => 'email/subject',
88 ),
89 'from_name' => array(
90 'default' => '',
91 'path' => 'email/from_name',
92 ),
93 'from_address' => array(
94 'default' => '',
95 'path' => 'email/from_address',
96 ),
97 'content_type' => array(
98 'default' => '',
99 'path' => 'email/content_type',
100 ),
101 'content' => array(
102 'default' => '',
103 'path' => 'email/content',
104 ),
105 );
106 }
107
108 /**
109 * Regsiter custom action data for the editor
110 *
111 * @return [type] [description]
112 */
113 public function action_data() {
114 return array(
115 'replyTo' => Tools::with_placeholder(
116 array(
117 array(
118 'value' => 'form',
119 'label' => __( 'Email from submitted form field', 'jet-form-builder' ),
120 ),
121 array(
122 'value' => 'custom',
123 'label' => __( 'Custom email', 'jet-form-builder' ),
124 ),
125 )
126 ),
127 'content_type' => Tools::with_placeholder(
128 array(
129 array(
130 'value' => 'text/plain',
131 'label' => __( 'Plain text', 'jet-form-builder' ),
132 ),
133 array(
134 'value' => 'text/html',
135 'label' => __( 'HTML', 'jet-form-builder' ),
136 ),
137 )
138 ),
139 'customMacros' => apply_filters( 'jet-form-builder/actions/send-email/custom-macros', false ),
140 );
141 }
142
143 /**
144 * @param array $request
145 * @param Action_Handler $handler
146 *
147 * @throws Action_Exception
148 */
149 public function do_action( array $request, Action_Handler $handler ) {
150 $message = apply_filters(
151 'jet-form-builder/send-email/message_content',
152 $this->settings['content'] ?? '',
153 $this
154 );
155 $this->set_content( $message );
156
157 $content_type = apply_filters(
158 'jet-form-builder/send-email/content-type',
159 $this->get_default_content_type(),
160 $this
161 );
162 $this->set_content_type( $content_type );
163
164 $from_name = apply_filters(
165 'jet-form-builder/send-email/from-name',
166 $this->get_default_from_name(),
167 $this
168 );
169 $this->set_from_name( $from_name );
170
171 $reply_to = apply_filters(
172 'jet-form-builder/send-email/reply-to',
173 $this->get_default_reply_to(),
174 $this
175 );
176 $this->set_reply_to( $reply_to );
177
178 $from_email = apply_filters(
179 'jet-form-builder/send-email/from-address',
180 $this->get_default_from_address(),
181 $this
182 );
183 $this->set_from_address( $from_email );
184
185 $this->set_mail_to( $this->get_default_mail_to() );
186 $this->set_cc( $this->get_default_cc() );
187 $this->set_bcc( $this->get_default_bcc() );
188 $this->set_subject( $this->get_default_subject() );
189 $this->set_attachments( $this->get_default_attachments() );
190
191 $headers = apply_filters(
192 'jet-form-builder/send-email/headers',
193 $this->get_default_headers(),
194 $this
195 );
196 $this->set_headers( $headers );
197
198 /**
199 * Hooks before the email is sent
200 */
201 do_action( 'jet-form-builder/send-email/send-before', $this );
202
203 if ( jet_form_builder()->has_module( 'dev' ) ) {
204 add_action(
205 'wp_mail_failed',
206 function ( \WP_Error $wp_error ) {
207 new Action_Exception( 'failed', $wp_error->get_error_message(), $wp_error->get_error_data() );
208 }
209 );
210 }
211
212 $this->validate();
213
214 $sent = wp_mail(
215 $this->get_mail_to(),
216 $this->get_subject(),
217 $this->get_content(),
218 $this->get_headers(),
219 $this->get_attachments()
220 );
221
222 if ( ! $sent ) {
223 throw new Action_Exception(
224 'failed',
225 array(
226 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
227 'to' => $this->get_mail_to(),
228 'subject' => $this->get_subject(),
229 'message' => $message,
230 'headers' => $this->get_headers(),
231 // phpcs:enable
232 )
233 );
234 }
235
236 /**
237 * Hooks after the email is sent
238 *
239 * @since 2.1
240 */
241 do_action( 'jet-form-builder/send-email/send-after', $this );
242 }
243
244 /**
245 * @throws Action_Exception
246 */
247 protected function validate() {
248 $this->validate_mail_to();
249 $this->validate_reply_to();
250 }
251
252 /**
253 * @throws Action_Exception
254 */
255 protected function validate_mail_to() {
256 foreach ( $this->mail_to as $value ) {
257 if ( ! $value || ! is_email( $value ) ) {
258 throw new Action_Exception( 'invalid_email' );
259 }
260 }
261 }
262
263 protected function validate_reply_to() {
264 $emails = explode( ',', $this->get_reply_to() );
265
266 foreach ( $emails as &$value ) {
267 $value = trim( $value );
268 }
269
270 $emails = array_filter( $emails, 'is_email' );
271
272 if ( count( $emails ) ) {
273 $this->set_reply_to( $emails );
274
275 return;
276 }
277
278 $this->set_reply_to( 'noreply@' . Http_Tools::get_site_host() );
279 }
280
281 public function get_default_mail_to() {
282 $mail_to = ! empty( $this->settings['mail_to'] ) ? $this->settings['mail_to'] : 'admin';
283
284 switch ( $mail_to ) {
285 case 'form':
286 $field = $this->settings['from_field'] ?? '';
287 $email = jet_fb_context()->get_value( $field );
288
289 return $email ?: '';
290 case 'custom':
291 return ! empty( $this->settings['custom_email'] ) ? $this->settings['custom_email'] : '';
292 case 'admin':
293 default:
294 return get_option( 'admin_email' );
295 }
296 }
297
298 public function get_default_cc() {
299 if ( empty( $this->settings['use_cc_bcc'] ) ) {
300 return array();
301 }
302
303 $mail_to = ! empty( $this->settings['cc_from'] ) ? $this->settings['cc_from'] : '';
304
305 switch ( $mail_to ) {
306 case 'form':
307 $field = $this->settings['cc_field'] ?? '';
308 $email = jet_fb_context()->get_value( $field );
309
310 return $email ?: '';
311 case 'custom':
312 return ! empty( $this->settings['cc_email'] ) ? $this->settings['cc_email'] : '';
313 case 'admin':
314 return get_option( 'admin_email' );
315 default:
316 return '';
317 }
318 }
319
320 public function get_default_bcc() {
321 if ( empty( $this->settings['use_cc_bcc'] ) ) {
322 return array();
323 }
324
325 $mail_to = ! empty( $this->settings['bcc_from'] ) ? $this->settings['bcc_from'] : '';
326
327 switch ( $mail_to ) {
328 case 'form':
329 $field = $this->settings['bcc_field'] ?? '';
330 $email = jet_fb_context()->get_value( $field );
331
332 return $email ?: '';
333 case 'custom':
334 return ! empty( $this->settings['bcc_email'] ) ? $this->settings['bcc_email'] : '';
335 case 'admin':
336 return get_option( 'admin_email' );
337 default:
338 return '';
339 }
340 }
341
342 public function get_default_reply_to(): string {
343 $reply_to = ! empty( $this->settings['reply_to'] ) ? $this->settings['reply_to'] : 'form';
344
345 switch ( $reply_to ) {
346 case 'form':
347 $field = $this->settings['reply_from_field'] ?? '';
348
349 return Tools::to_string( jet_fb_context()->get_value( $field ) );
350 case 'custom':
351 return ! empty( $this->settings['reply_to_email'] ) ? $this->settings['reply_to_email'] : '';
352 default:
353 return '';
354 }
355 }
356
357 public function get_default_subject(): string {
358 return empty( $this->settings['subject'] )
359 ? sprintf(
360 /* translators: %s - site url */
361 __( 'Form on %s Submitted', 'jet-form-builder' ),
362 home_url( '' )
363 )
364 : $this->settings['subject'];
365 }
366
367 public function get_default_content_type(): string {
368 $type = $this->settings['content_type'] ?? 'text/html';
369
370 return empty( $type ) ? 'text/html' : $type;
371 }
372
373 public function get_default_from_name(): string {
374 return empty( $this->settings['from_name'] )
375 ? Tools::get_site_name()
376 : $this->settings['from_name'];
377 }
378
379 public function get_default_from_address(): string {
380 return ! empty( $this->settings['from_address'] ) ? $this->settings['from_address'] : '';
381 }
382
383 public function get_default_attachments(): array {
384 $fields = $this->settings['attachments'] ?? array();
385 $attachments = array();
386
387 foreach ( $fields as $field ) {
388 $value = Request_Tools::get_file( $field );
389
390 if ( ! $value ) {
391 continue;
392 }
393
394 array_push(
395 $attachments,
396 ...explode( ',', $value->get_attachment_file() )
397 );
398 }
399
400 return $attachments;
401 }
402
403 public function update_headers() {
404 $this->set_headers( $this->get_default_headers() );
405 }
406
407 /**
408 * Get the email headers
409 *
410 * @since 2.1
411 */
412 public function get_default_headers(): string {
413 $headers = array(
414 "From: {$this->get_from_name()} <{$this->get_from_address()}>",
415 "Reply-To: {$this->get_reply_to()}",
416 "Content-Type: {$this->get_content_type()}; charset=utf-8",
417 );
418
419 $cc_emails = $this->get_cc();
420 $bcc_emails = $this->get_bcc();
421
422 foreach ( $cc_emails as $cc_email ) {
423 $headers[] = 'Cc: ' . $cc_email;
424 }
425 foreach ( $bcc_emails as $bcc_email ) {
426 $headers[] = 'Bcc: ' . $bcc_email;
427 }
428
429 return implode( "\r\n", $headers );
430 }
431
432 /**
433 * @param string|array $email
434 */
435 public function set_mail_to( $email ) {
436 if ( ! is_array( $email ) && ! is_string( $email ) ) {
437 return;
438 }
439
440 $this->mail_to = $this->parse_email( $email );
441 }
442
443 /**
444 * @param $email
445 *
446 * @return void
447 */
448 public function set_cc( $email ): void {
449 if ( ! is_array( $email ) && ! is_string( $email ) ) {
450 return;
451 }
452
453 $this->cc = $this->parse_email( $email );
454 }
455
456 /**
457 * @param $email
458 *
459 * @return void
460 */
461 public function set_bcc( $email ): void {
462 if ( ! is_array( $email ) && ! is_string( $email ) ) {
463 return;
464 }
465
466 $this->bcc = $this->parse_email( $email );
467 }
468
469 /**
470 * @param $email
471 *
472 * @return string[]
473 */
474 private function parse_email( $email ): array {
475 if ( ! is_array( $email ) ) {
476 $email = explode( ',', $email );
477 }
478
479 foreach ( $email as &$value ) {
480 $value = trim( $value );
481 }
482
483 return $email;
484 }
485
486 public function set_reply_to( $email ) {
487 if ( ! is_string( $email ) && ! is_array( $email ) ) {
488 return;
489 }
490
491 $this->reply_to = Tools::to_string( $email );
492 }
493
494 public function set_content( $content ) {
495 if ( ! is_string( $content ) ) {
496 return;
497 }
498
499 $this->content = $content;
500 }
501
502 public function set_subject( $subject ) {
503 if ( ! is_string( $subject ) ) {
504 return;
505 }
506
507 $this->subject = $subject;
508 }
509
510 public function set_content_type( string $type ) {
511 $this->content_type = $type;
512 }
513
514 public function set_from_name( string $name ) {
515 $this->from_name = $name;
516 }
517
518 public function set_from_address( string $email ) {
519 $this->from_email = $email;
520 }
521
522 /**
523 * @param array $attachments
524 */
525 public function set_attachments( array $attachments ) {
526 $this->attachments = $attachments;
527 }
528
529 public function set_headers( string $headers ) {
530 $this->headers = $headers;
531 }
532
533 public function get_attachments(): array {
534 return $this->attachments;
535 }
536
537 /**
538 * @return array
539 */
540 public function get_mail_to(): array {
541 return $this->mail_to;
542 }
543
544 /**
545 * @return array
546 */
547 public function get_cc(): array {
548 return $this->cc;
549 }
550
551 /**
552 * @return array
553 */
554 public function get_bcc(): array {
555 return $this->bcc;
556 }
557
558 /**
559 * @return string
560 */
561 public function get_subject(): string {
562 return $this->subject;
563 }
564
565 /**
566 * @return string
567 */
568 public function get_content(): string {
569 return $this->content;
570 }
571
572 /**
573 * Get the email from name
574 */
575 public function get_from_name(): string {
576 return $this->from_name;
577 }
578
579 /**
580 * Returns e-mail address to set into Reply-to email header
581 */
582 public function get_reply_to(): string {
583 return $this->reply_to;
584 }
585
586 /**
587 * Get the email from address
588 */
589 public function get_from_address(): string {
590 return $this->from_email;
591 }
592
593 public function is_html(): bool {
594 return 'text/html' === $this->get_content_type();
595 }
596
597 /**
598 * Get the email content type
599 */
600 public function get_content_type(): string {
601 return $this->content_type;
602 }
603
604 /**
605 * @return string
606 */
607 public function get_headers(): string {
608 return $this->headers;
609 }
610
611 }
612