Repository navigation
Expand file tree
/
Copy pathProcessResend.module.php
More file actions
1100 lines (1023 loc) · 38.2 KB
/
Copy pathProcessResend.module.php
File metadata and controls
1100 lines (1023 loc) · 38.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace ProcessWire;
/**
* ProcessResend
*
* Admin UI for the Resend module: dashboard, sent and received emails,
* domains, contacts, segments, topics, templates, broadcasts, webhooks,
* logs and a test send screen.
*
* Copyright (c) 2026 Maxim Semenov <maxim@smnv.org>
* License: MIT
*/
class ProcessResend extends Process implements Module {
public static function getModuleInfo() {
return [
'title' => 'Resend Admin',
'summary' => 'Admin interface for the Resend email platform.',
'version' => 102,
'author' => 'Maxim Semenov',
'icon' => 'envelope-o',
'requires' => ['Resend'],
'permission' => 'resend-admin',
'permissions' => [
'resend-admin' => 'Access the Resend admin interface',
],
'page' => [
'name' => 'resend',
'parent' => 'setup',
'title' => 'Resend',
],
];
}
/** @var Resend */
protected $resend;
public function init() {
parent::init();
$this->resend = $this->wire()->modules->get('Resend');
}
/*
* ------------------------------------------------------------------
* Rendering
* ------------------------------------------------------------------
*/
/**
* Module directory path as ProcessWire sees it.
*
* Not __DIR__: that resolves symlinks to the real filesystem path, which
* falls outside the allowed wireRenderFile() paths when the module
* directory is symlinked into site/modules.
*
* @return string
*/
protected function modulePath() {
$path = $this->wire()->config->paths->get($this->className());
return $path ? $path : __DIR__ . '/';
}
/**
* Render a view file from the views directory.
*
* @param string $view View name without prefix/extension, e.g. 'dashboard'
* @param array $vars Variables extracted into view scope
* @return string
*/
protected function render($view, array $vars = []) {
$vars['process'] = $this;
$vars['resend'] = $this->resend;
$vars['adminUrl'] = $this->wire()->page->url;
$vars['csrf'] = $this->wire()->session->CSRF;
$file = $this->modulePath() . 'views/resend.' . $view . '.php';
if(!is_file($file)) return "View not found: $view";
return wireRenderFile($file, $vars);
}
/**
* Build a URL within this process page.
*
* @param string $segment
* @param array $params
* @return string
*/
public function url($segment = '', array $params = []) {
$url = $this->wire()->page->url . ($segment ? trim($segment, '/') . '/' : '');
if(count($params)) $url .= '?' . http_build_query($params);
return $url;
}
/**
* Check API result; on error, show a notice and return false.
*
* @param array $result
* @return bool True if OK
*/
protected function ok($result) {
if($this->resend->isError($result)) {
$e = $result['error'];
$this->error("Resend API: {$e['name']} — {$e['message']} ({$e['statusCode']})");
// Key was downgraded on the Resend side: drop the stale cached
// permission so restricted mode kicks in on the next request
if($e['name'] === 'restricted_api_key') $this->resend->refreshApiKeyPermission();
return false;
}
return true;
}
/**
* Extract list data from a Resend list response.
*
* @param array $result
* @return array
*/
public function listData($result) {
if($this->resend->isError($result)) return [];
if(isset($result['data']) && is_array($result['data'])) return $result['data'];
return [];
}
/**
* Validate CSRF for POST actions.
*/
protected function requireValidToken() {
if(!$this->wire()->session->CSRF->hasValidToken()) {
throw new WireException('CSRF token validation failed');
}
}
/**
* Whether the configured key is restricted to sending only.
*
* @return bool
*/
protected function restricted() {
return $this->resend->apiKeyPermission() === 'sending_access';
}
/**
* Redirect to the dashboard when the key cannot use management endpoints.
*/
protected function requireFullAccess() {
if(!$this->restricted()) return;
$this->warning($this->_('This screen requires a full access API key. The configured key is restricted to sending emails.'));
$this->wire()->session->redirect($this->url());
}
/**
* Common cursor pagination params from query string.
*
* @return array
*/
protected function paginationParams() {
$input = $this->wire()->input;
$params = ['limit' => 25];
$after = $input->get->text('after');
$before = $input->get->text('before');
if(strlen($after)) $params['after'] = $after;
if(strlen($before)) $params['before'] = $before;
return $params;
}
/*
* ------------------------------------------------------------------
* Screens
* ------------------------------------------------------------------
*/
public function ___execute() {
$this->headline($this->_('Resend Dashboard'));
if($this->wire()->input->post->text('action') === 'recheck') {
$this->requireValidToken();
$permission = $this->resend->refreshApiKeyPermission();
if($permission === 'full_access') {
$this->message($this->_('API key permission re-checked: full access. All sections unlocked.'));
} elseif($permission === 'sending_access') {
$this->message($this->_('API key permission re-checked: the key is still sending-only.'));
} else {
$this->warning($this->_('Could not determine the key permission (invalid key or network error).'));
}
$this->wire()->session->redirect($this->url());
}
if(!strlen($this->resend->apiKey)) {
$this->warning($this->_('API key is not configured. Configure the Resend module first.'));
return $this->render('dashboard', [
'configured' => false,
'restricted' => false,
'domains' => [],
'emails' => [],
'statsDays' => [],
'statsSignalDays' => [],
'statsStatuses' => [],
'statsTotal' => 0,
]);
}
if($this->restricted()) {
// Sending-only key: management endpoints return 401, skip them
return $this->render('dashboard', [
'configured' => true,
'restricted' => true,
'domains' => [],
'emails' => [],
'statsDays' => [],
'statsSignalDays' => [],
'statsStatuses' => [],
'statsTotal' => 0,
]);
}
$domains = $this->resend->domains()->all();
// One list call feeds both the recent emails table and the charts
$emails = $this->resend->emails()->all(['limit' => 100]);
$this->ok($domains);
$emailData = $this->listData($emails);
return $this->render('dashboard', [
'configured' => true,
'restricted' => false,
'domains' => $this->listData($domains),
'emails' => array_slice($emailData, 0, 10),
'statsDays' => \Smnv\Resend\Stats::perDay($emailData),
'statsSignalDays' => \Smnv\Resend\Stats::perDaySignals($emailData),
'statsStatuses' => \Smnv\Resend\Stats::byStatus($emailData),
'statsTotal' => count($emailData),
]);
}
public function ___executeEmails() {
$this->requireFullAccess();
$this->headline($this->_('Sent Emails'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->emails()->all($this->paginationParams());
$this->ok($result);
return $this->render('emails', [
'emails' => $this->listData($result),
'hasMore' => isset($result['has_more']) ? (bool) $result['has_more'] : false,
'received' => false,
]);
}
public function ___executeReceived() {
$this->requireFullAccess();
$this->headline($this->_('Received Emails'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->emails()->receivedAll($this->paginationParams());
$this->ok($result);
return $this->render('emails', [
'emails' => $this->listData($result),
'hasMore' => isset($result['has_more']) ? (bool) $result['has_more'] : false,
'received' => true,
]);
}
public function ___executeEmail() {
$this->requireFullAccess();
$input = $this->wire()->input;
$id = $input->get->text('id');
$received = (bool) $input->get->int('received');
if(!strlen($id)) $this->wire()->session->redirect($this->url('emails'));
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'cancel') {
$result = $this->resend->emails()->cancel($id);
if($this->ok($result)) $this->message($this->_('Scheduled email cancelled.'));
} elseif($action === 'reschedule') {
$when = $input->post->text('scheduled_at');
$result = $this->resend->emails()->update($id, $when);
if($this->ok($result)) $this->message($this->_('Email rescheduled.'));
}
$this->wire()->session->redirect($this->url('email', ['id' => $id, 'received' => (int) $received]));
}
$this->headline($this->_('Email'));
$this->breadcrumb('../', $this->_('Resend'));
$this->breadcrumb('../emails/', $this->_('Sent Emails'));
$email = $received ? $this->resend->emails()->received($id) : $this->resend->emails()->get($id);
$this->ok($email);
$attachments = $received
? $this->resend->emails()->receivedAttachments($id)
: $this->resend->emails()->attachments($id);
return $this->render('email', [
'email' => $this->resend->isError($email) ? [] : $email,
'attachments' => $this->listData($attachments),
'received' => $received,
'id' => $id,
]);
}
public function ___executeDomains() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
$id = $input->post->text('id');
if($action === 'create') {
$name = $input->post->text('name');
$data = [];
$region = $input->post->text('region');
if(strlen($region)) $data['region'] = $region;
$result = $this->resend->domains()->create($name, $data);
if($this->ok($result)) $this->message(sprintf($this->_('Domain %s created. Add the DNS records, then verify.'), $name));
} elseif($action === 'verify' && strlen($id)) {
$result = $this->resend->domains()->verify($id);
if($this->ok($result)) $this->message($this->_('Verification triggered.'));
} elseif($action === 'delete' && strlen($id)) {
$result = $this->resend->domains()->delete($id);
if($this->ok($result)) $this->message($this->_('Domain deleted.'));
} elseif($action === 'tracking' && strlen($id)) {
$result = $this->resend->domains()->update($id, [
'open_tracking' => (bool) $input->post->int('open_tracking'),
'click_tracking' => (bool) $input->post->int('click_tracking'),
]);
if($this->ok($result)) $this->message($this->_('Tracking settings updated.'));
}
$this->wire()->session->redirect($this->url('domains'));
}
$this->headline($this->_('Domains'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->domains()->all();
$this->ok($result);
return $this->render('domains', ['domains' => $this->listData($result)]);
}
public function ___executeDomain() {
$this->requireFullAccess();
$input = $this->wire()->input;
$id = $input->get->text('id');
if(!strlen($id)) $this->wire()->session->redirect($this->url('domains'));
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'delete') {
$result = $this->resend->domains()->delete($id);
if($this->ok($result)) {
$this->message($this->_('Domain deleted.'));
$this->wire()->session->redirect($this->url('domains'));
}
}
$this->wire()->session->redirect($this->url('domain', ['id' => $id]));
}
$this->headline($this->_('Domain'));
$this->breadcrumb('../', $this->_('Resend'));
$this->breadcrumb('../domains/', $this->_('Domains'));
$domain = $this->resend->domains()->get($id);
$this->ok($domain);
return $this->render('domain', [
'domain' => $this->resend->isError($domain) ? [] : $domain,
]);
}
public function ___executeContacts() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'create') {
$data = ['email' => $input->post->email('email')];
$firstName = $input->post->text('first_name');
$lastName = $input->post->text('last_name');
if(strlen($firstName)) $data['first_name'] = $firstName;
if(strlen($lastName)) $data['last_name'] = $lastName;
$result = $this->resend->contacts()->create($data);
if($this->ok($result)) {
$segmentId = $input->post->text('segment_id');
if(strlen($segmentId) && isset($result['id'])) {
$this->resend->contacts()->addToSegment($result['id'], $segmentId);
}
$this->message($this->_('Contact created.'));
}
} elseif($action === 'delete') {
$result = $this->resend->contacts()->delete($input->post->text('id'));
if($this->ok($result)) $this->message($this->_('Contact deleted.'));
} elseif($action === 'unsubscribe') {
$result = $this->resend->contacts()->update($input->post->text('id'), ['unsubscribed' => true]);
if($this->ok($result)) $this->message($this->_('Contact unsubscribed.'));
} elseif($action === 'resubscribe') {
$result = $this->resend->contacts()->update($input->post->text('id'), ['unsubscribed' => false]);
if($this->ok($result)) $this->message($this->_('Contact resubscribed.'));
}
$this->wire()->session->redirect($this->url('contacts', array_filter([
'segment' => $input->get->text('segment'),
])));
}
$this->headline($this->_('Contacts'));
$this->breadcrumb('../', $this->_('Resend'));
$segmentId = $input->get->text('segment');
$params = $this->paginationParams();
$result = strlen($segmentId)
? $this->resend->segments()->contacts($segmentId, $params)
: $this->resend->contacts()->all($params);
$this->ok($result);
$segments = $this->resend->segments()->all();
return $this->render('contacts', [
'contacts' => $this->listData($result),
'hasMore' => isset($result['has_more']) ? (bool) $result['has_more'] : false,
'segments' => $this->listData($segments),
'segmentId' => $segmentId,
]);
}
public function ___executeSegments() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'create') {
$result = $this->resend->segments()->create($input->post->text('name'));
if($this->ok($result)) $this->message($this->_('Segment created.'));
} elseif($action === 'delete') {
$result = $this->resend->segments()->delete($input->post->text('id'));
if($this->ok($result)) $this->message($this->_('Segment deleted.'));
}
$this->wire()->session->redirect($this->url('segments'));
}
$this->headline($this->_('Segments'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->segments()->all();
$this->ok($result);
return $this->render('segments', ['segments' => $this->listData($result)]);
}
public function ___executeProperties() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'create') {
$type = $input->post->text('type');
$data = [
'key' => $input->post->text('key'),
'type' => in_array($type, $this->propertyTypes()) ? $type : 'string',
];
$fallback = $input->post->text('fallback_value');
if(strlen($fallback)) $data['fallback_value'] = $fallback;
$result = $this->resend->contactProperties()->create($data);
if($this->ok($result)) $this->message($this->_('Contact property created.'));
} elseif($action === 'delete') {
$result = $this->resend->contactProperties()->delete($input->post->text('id'));
if($this->ok($result)) $this->message($this->_('Contact property deleted.'));
}
$this->wire()->session->redirect($this->url('properties'));
}
$this->headline($this->_('Contact Properties'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->contactProperties()->all();
$this->ok($result);
return $this->render('properties', [
'properties' => $this->listData($result),
'types' => $this->propertyTypes(),
]);
}
public function ___executeTopics() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'create') {
$data = [
'name' => $input->post->text('name'),
'default_subscription' => $input->post->text('default_subscription') === 'opt-out' ? 'opt-out' : 'opt-in',
];
$description = $input->post->text('description');
if(strlen($description)) $data['description'] = $description;
$result = $this->resend->topics()->create($data);
if($this->ok($result)) $this->message($this->_('Topic created.'));
} elseif($action === 'delete') {
$result = $this->resend->topics()->delete($input->post->text('id'));
if($this->ok($result)) $this->message($this->_('Topic deleted.'));
}
$this->wire()->session->redirect($this->url('topics'));
}
$this->headline($this->_('Topics'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->topics()->all();
$this->ok($result);
return $this->render('topics', ['topics' => $this->listData($result)]);
}
public function ___executeTemplates() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
$id = $input->post->text('id');
if($action === 'create') {
// Raw value: the textarea() sanitizer strips tags, destroying the HTML body
$html = $input->post('html');
$data = [
'name' => $input->post->text('name'),
'html' => is_string($html) ? $html : '',
];
foreach(['alias', 'subject', 'from'] as $key) {
$value = $input->post->text($key);
if(strlen($value)) $data[$key] = $value;
}
$result = $this->resend->templates()->create($data);
if($this->ok($result)) $this->message($this->_('Template created as a draft. Publish it to make it sendable.'));
} elseif($action === 'publish' && strlen($id)) {
$result = $this->resend->templates()->publish($id);
if($this->ok($result)) $this->message($this->_('Template published.'));
} elseif($action === 'duplicate' && strlen($id)) {
$result = $this->resend->templates()->duplicate($id);
if($this->ok($result)) $this->message($this->_('Template duplicated.'));
} elseif($action === 'delete' && strlen($id)) {
$result = $this->resend->templates()->delete($id);
if($this->ok($result)) $this->message($this->_('Template deleted.'));
}
$this->wire()->session->redirect($this->url('templates'));
}
$this->headline($this->_('Templates'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->templates()->all($this->paginationParams());
$this->ok($result);
return $this->render('templates', [
'templates' => $this->listData($result),
'hasMore' => isset($result['has_more']) ? (bool) $result['has_more'] : false,
]);
}
public function ___executeBroadcasts() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
$id = $input->post->text('id');
if($action === 'create') {
// Raw value: the textarea() sanitizer strips tags, destroying the HTML body
$html = $input->post('html');
$data = [
'name' => $input->post->text('name'),
'segment_id' => $input->post->text('segment_id'),
'from' => $input->post->text('from'),
'subject' => $input->post->text('subject'),
'html' => is_string($html) ? $html : '',
];
$result = $this->resend->broadcasts()->create($data);
if($this->ok($result)) {
$this->message($this->_('Broadcast created as draft.'));
if(isset($result['id']) && strlen((string) $result['id'])) {
$this->wire()->session->redirect($this->url('broadcast', ['id' => (string) $result['id']]));
}
}
} elseif($action === 'send' && strlen($id)) {
$scheduledAt = $input->post->text('scheduled_at');
$result = $this->resend->broadcasts()->send($id, $scheduledAt);
if($this->ok($result)) {
$this->message(strlen($scheduledAt) ? $this->_('Broadcast scheduled.') : $this->_('Broadcast is being sent.'));
}
} elseif($action === 'delete' && strlen($id)) {
$result = $this->resend->broadcasts()->delete($id);
if($this->ok($result)) $this->message($this->_('Broadcast deleted.'));
}
$this->wire()->session->redirect($this->url('broadcasts'));
}
$this->headline($this->_('Broadcasts'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->broadcasts()->all($this->paginationParams());
$this->ok($result);
$segments = $this->resend->segments()->all();
return $this->render('broadcasts', [
'broadcasts' => $this->listData($result),
'segments' => $this->listData($segments),
'hasMore' => isset($result['has_more']) ? (bool) $result['has_more'] : false,
]);
}
public function ___executeBroadcast() {
$this->requireFullAccess();
$input = $this->wire()->input;
$id = $input->get->text('id');
if(!strlen($id)) $this->wire()->session->redirect($this->url('broadcasts'));
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'update') {
// Raw value: the textarea() sanitizer strips tags, destroying the HTML body
$html = $input->post('html');
$data = [
'name' => $input->post->text('name'),
'segment_id' => $input->post->text('segment_id'),
'from' => $input->post->text('from'),
'subject' => $input->post->text('subject'),
'html' => is_string($html) ? $html : '',
];
$result = $this->resend->broadcasts()->update($id, $data);
if($this->ok($result)) $this->message($this->_('Broadcast draft updated.'));
} elseif($action === 'send') {
$scheduledAt = $input->post->text('scheduled_at');
$result = $this->resend->broadcasts()->send($id, $scheduledAt);
if($this->ok($result)) {
$this->message(strlen($scheduledAt) ? $this->_('Broadcast scheduled.') : $this->_('Broadcast is being sent.'));
}
} elseif($action === 'delete') {
$result = $this->resend->broadcasts()->delete($id);
if($this->ok($result)) {
$this->message($this->_('Broadcast deleted.'));
$this->wire()->session->redirect($this->url('broadcasts'));
}
}
$this->wire()->session->redirect($this->url('broadcast', ['id' => $id]));
}
$this->headline($this->_('Broadcast'));
$this->breadcrumb('../', $this->_('Resend'));
$this->breadcrumb('../broadcasts/', $this->_('Broadcasts'));
$broadcast = $this->resend->broadcasts()->get($id);
$this->ok($broadcast);
return $this->render('broadcast', [
'broadcast' => $this->resend->isError($broadcast) ? [] : $broadcast,
'segments' => $this->listData($this->resend->segments()->all()),
'id' => $id,
]);
}
public function ___executeWebhooks() {
$this->requireFullAccess();
$input = $this->wire()->input;
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
$id = $input->post->text('id');
if($action === 'create') {
$events = $input->post->array('events');
$events = array_values(array_intersect($events, $this->webhookEventTypes()));
$result = $this->resend->webhooks()->create($input->post->url('endpoint_url'), $events);
if($this->ok($result)) $this->message($this->_('Webhook created.'));
} elseif($action === 'delete' && strlen($id)) {
$result = $this->resend->webhooks()->delete($id);
if($this->ok($result)) $this->message($this->_('Webhook deleted.'));
} elseif($action === 'toggle' && strlen($id)) {
$result = $this->resend->webhooks()->update($id, [
'status' => $input->post->text('status') === 'disabled' ? 'enabled' : 'disabled',
]);
if($this->ok($result)) $this->message($this->_('Webhook status updated.'));
}
$this->wire()->session->redirect($this->url('webhooks'));
}
$this->headline($this->_('Webhooks'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->webhooks()->all();
$this->ok($result);
return $this->render('webhooks', [
'webhooks' => $this->listData($result),
'eventTypes' => $this->webhookEventTypes(),
]);
}
public function ___executeEvents() {
// Local data from ResendWebhooks; available regardless of key permission
$this->headline($this->_('Webhook Events'));
$this->breadcrumb('../', $this->_('Resend'));
$modules = $this->wire()->modules;
$events = [];
$receiverInstalled = $modules->isInstalled('ResendWebhooks');
$beforeId = $this->wire()->input->get->int('before');
if($receiverInstalled) {
/** @var ResendWebhooks $webhooks */
$webhooks = $modules->get('ResendWebhooks');
$events = $webhooks->latestEvents(25, $beforeId);
}
return $this->render('events', [
'events' => $events,
'receiverInstalled' => $receiverInstalled,
'secretConfigured' => strlen((string) $this->resend->webhookSigningSecret) > 0,
'hasMore' => count($events) === 25,
]);
}
public function ___executeContact() {
$this->requireFullAccess();
$input = $this->wire()->input;
$id = $input->get->text('id');
if(!strlen($id)) $this->wire()->session->redirect($this->url('contacts'));
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'update') {
$result = $this->resend->contacts()->update($id, [
'first_name' => $input->post->text('first_name'),
'last_name' => $input->post->text('last_name'),
]);
if($this->ok($result)) $this->message($this->_('Contact updated.'));
} elseif($action === 'addsegment') {
$result = $this->resend->contacts()->addToSegment($id, $input->post->text('segment_id'));
if($this->ok($result)) $this->message($this->_('Added to segment.'));
} elseif($action === 'removesegment') {
$result = $this->resend->contacts()->removeFromSegment($id, $input->post->text('segment_id'));
if($this->ok($result)) $this->message($this->_('Removed from segment.'));
} elseif($action === 'topic') {
$subscription = $input->post->text('subscription') === 'opt-out' ? 'opt-out' : 'opt-in';
$result = $this->resend->contacts()->updateTopics($id, [[
'id' => $input->post->text('topic_id'),
'subscription' => $subscription,
]]);
if($this->ok($result)) $this->message($this->_('Topic subscription updated.'));
} elseif($action === 'unsubscribe' || $action === 'resubscribe') {
$result = $this->resend->contacts()->update($id, ['unsubscribed' => $action === 'unsubscribe']);
if($this->ok($result)) $this->message($this->_('Subscription updated.'));
}
$this->wire()->session->redirect($this->url('contact', ['id' => $id]));
}
$this->headline($this->_('Contact'));
$this->breadcrumb('../', $this->_('Resend'));
$this->breadcrumb('../contacts/', $this->_('Contacts'));
$contact = $this->resend->contacts()->get($id);
$this->ok($contact);
return $this->render('contact', [
'contact' => $this->resend->isError($contact) ? [] : $contact,
'memberSegments' => $this->listData($this->resend->contacts()->segments($id)),
'topics' => $this->listData($this->resend->contacts()->topics($id)),
'segments' => $this->listData($this->resend->segments()->all()),
'id' => $id,
]);
}
public function ___executeTemplate() {
$this->requireFullAccess();
$input = $this->wire()->input;
$id = $input->get->text('id');
if(!strlen($id)) $this->wire()->session->redirect($this->url('templates'));
if($input->post->text('action')) {
$this->requireValidToken();
$action = $input->post->text('action');
if($action === 'update') {
// Raw value: the textarea() sanitizer strips tags, destroying the HTML body
$html = $input->post('html');
$data = [
'name' => $input->post->text('name'),
'subject' => $input->post->text('subject'),
'html' => is_string($html) ? $html : '',
];
$alias = $input->post->text('alias');
if(strlen($alias)) $data['alias'] = $alias;
$result = $this->resend->templates()->update($id, $data);
if($this->ok($result)) $this->message($this->_('Template updated. Publish to make the changes sendable.'));
} elseif($action === 'publish') {
$result = $this->resend->templates()->publish($id);
if($this->ok($result)) $this->message($this->_('Template published.'));
}
$this->wire()->session->redirect($this->url('template', ['id' => $id]));
}
$this->headline($this->_('Template'));
$this->breadcrumb('../', $this->_('Resend'));
$this->breadcrumb('../templates/', $this->_('Templates'));
$template = $this->resend->templates()->get($id);
$this->ok($template);
return $this->render('template', [
'template' => $this->resend->isError($template) ? [] : $template,
'id' => $id,
]);
}
public function ___executeLogs() {
$this->requireFullAccess();
$this->headline($this->_('API Logs'));
$this->breadcrumb('../', $this->_('Resend'));
$result = $this->resend->logs()->all($this->paginationParams());
$this->ok($result);
return $this->render('logs', [
'logs' => $this->listData($result),
'hasMore' => isset($result['has_more']) ? (bool) $result['has_more'] : false,
]);
}
public function ___executeTest() {
$input = $this->wire()->input;
$sent = false;
$fromConfigured = strlen((string) $this->resend->fromEmail) > 0;
if($input->post->text('action') === 'send') {
$this->requireValidToken();
if(!$fromConfigured) {
$this->warning($this->_('Set a Default From Email in Resend settings before sending a test email.'));
$this->wire()->session->redirect($this->url('test'));
}
$to = $input->post->email('to');
// Raw values: text() truncates at 255 chars, textarea() strips tags from the HTML body
$subject = $this->wire()->sanitizer->text($input->post('subject'), ['maxLength' => 998, 'stripTags' => false]);
$body = $input->post('body');
if(!is_string($body)) $body = '';
$scheduledAt = $input->post->text('scheduled_at');
if(strlen($to) && strlen($subject)) {
/** @var Resend $m */
$m = $this->wire()->modules->get('Resend');
$m->to($to)->subject($subject)->bodyHTML($body ?: '<p>Test email from ProcessWire via Resend.</p>');
if(strlen($scheduledAt)) $m->scheduledAt($scheduledAt);
$m->addTag('source', 'processwire-test');
$count = $m->send();
if($count) {
$this->message(sprintf($this->_('Test email sent. Resend id: %s'), $m->getLastEmailId()));
$sent = true;
} else {
$this->error($this->_('Send failed. Check the resend log.'));
}
} else {
$this->error($this->_('Recipient and subject are required.'));
}
}
$this->headline($this->_('Test Send'));
$this->breadcrumb('../', $this->_('Resend'));
return $this->render('test', [
'sent' => $sent,
'restricted' => $this->restricted(),
'fromConfigured' => $fromConfigured,
'fromEmail' => $this->resend->fromEmail,
'fromName' => $this->resend->fromName,
]);
}
/**
* Supported webhook event types.
*
* @return array
*/
public function webhookEventTypes() {
return \Smnv\Resend\Webhooks::EVENT_TYPES;
}
/**
* Supported contact property types.
*
* @return array
*/
public function propertyTypes() {
return \Smnv\Resend\ContactProperties::TYPES;
}
/**
* Inspect public DNS records for Resend domain setup guidance.
*
* @param string $domainName
* @param array $records
* @return array
*/
public function domainDnsAudit($domainName, array $records) {
if(!function_exists('dns_get_record')) return [];
$out = [];
foreach($records as $record) {
$type = strtoupper((string) ($record['type'] ?? ''));
$name = (string) ($record['name'] ?? '');
$value = (string) ($record['value'] ?? '');
$host = $this->domainDnsHost($domainName, $name);
$item = [
'type' => $type,
'name' => $name,
'host' => $host,
'value' => $value,
'priority' => isset($record['priority']) ? (string) $record['priority'] : '',
'status' => 'missing',
'message' => 'Add this record at your DNS provider.',
'current' => [],
'suggested' => '',
];
if($type === 'TXT') {
$item = $this->auditTxtRecord($item);
} elseif($type === 'MX') {
$item = $this->auditMxRecord($item);
} elseif($type === 'CNAME') {
$item = $this->auditCnameRecord($item);
}
$out[] = $item;
}
return $out;
}
/**
* @param string $domainName
* @param string $recordName
* @return string
*/
protected function domainDnsHost($domainName, $recordName) {
$domainName = trim((string) $domainName, ". \t\r\n");
$recordName = trim((string) $recordName, ". \t\r\n");
if(!strlen($recordName) || $recordName === '@') return $domainName;
if(strtolower($recordName) === strtolower($domainName)) return $domainName;
if(substr(strtolower($recordName), -strlen('.' . strtolower($domainName))) === '.' . strtolower($domainName)) return $recordName;
return $recordName . '.' . $domainName;
}
/**
* @param array $item
* @return array
*/
protected function auditTxtRecord(array $item) {
$records = @dns_get_record($item['host'], DNS_TXT);
$records = is_array($records) ? $records : [];
$txts = [];
foreach($records as $record) {
if(isset($record['txt'])) $txts[] = (string) $record['txt'];
}
$item['current'] = $txts;
$required = trim($item['value']);
if(stripos($required, 'v=spf1') === 0) {
$existingSpf = '';
foreach($txts as $txt) {
if(stripos(trim($txt), 'v=spf1') === 0) {
$existingSpf = trim($txt);
break;
}
}
if(!strlen($existingSpf)) {
$item['message'] = 'Add this SPF TXT record at this host.';
} elseif($this->dnsValueEquals($existingSpf, $required)) {
$item['status'] = 'found';
$item['message'] = 'SPF is already ready for Resend.';
} else {
$item['status'] = 'merge';
$item['message'] = 'Existing SPF found. Replace it with one combined SPF record so your current mail setup and Resend both work.';
$item['suggested'] = $this->mergeSpf($existingSpf, $required);
}
return $item;
}
foreach($txts as $txt) {
if($this->dnsValueEquals($txt, $required)) {
$item['status'] = 'found';
$item['message'] = 'This TXT record is already present.';
return $item;
}
}
$item['message'] = count($txts) ? 'TXT records exist here, but the Resend value is not present yet.' : 'Add this TXT record.';
return $item;
}
/**
* @param array $item
* @return array
*/
protected function auditMxRecord(array $item) {
$records = @dns_get_record($item['host'], DNS_MX);
$records = is_array($records) ? $records : [];
$item['current'] = $records;
$requiredTarget = $this->normalizeDnsValue($item['value']);
$requiredPriority = strlen($item['priority']) ? (int) $item['priority'] : null;
foreach($records as $record) {
$target = $this->normalizeDnsValue((string) ($record['target'] ?? ''));
$priority = isset($record['pri']) ? (int) $record['pri'] : null;
if($target === $requiredTarget && ($requiredPriority === null || $priority === $requiredPriority)) {
$item['status'] = 'found';
$item['message'] = 'This MX record is already present.';
return $item;
}
}
if(count($records)) {
$item['status'] = 'coexist';
$item['message'] = 'Existing MX records found. Keep them and add the Resend MX record too; the priority keeps routing predictable.';
} else {
$item['message'] = 'Add this MX record.';
}
return $item;
}
/**
* @param array $item
* @return array
*/
protected function auditCnameRecord(array $item) {
$records = @dns_get_record($item['host'], DNS_CNAME);
$records = is_array($records) ? $records : [];
$item['current'] = $records;
$required = $this->normalizeDnsValue($item['value']);
foreach($records as $record) {
$target = $this->normalizeDnsValue((string) ($record['target'] ?? ''));
if($target === $required) {
$item['status'] = 'found';