Repository navigation
Expand file tree
/
Copy pathResend.module.php
More file actions
1057 lines (942 loc) · 32.5 KB
/
Copy pathResend.module.php
File metadata and controls
1057 lines (942 loc) · 32.5 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;
use Smnv\Resend\Broadcasts;
use Smnv\Resend\Client;
use Smnv\Resend\ContactProperties;
use Smnv\Resend\Contacts;
use Smnv\Resend\Crypto;
use Smnv\Resend\Domains;
use Smnv\Resend\Emails;
use Smnv\Resend\Logs;
use Smnv\Resend\Segments;
use Smnv\Resend\Templates;
use Smnv\Resend\Topics;
use Smnv\Resend\Webhooks;
require_once __DIR__ . '/src/Crypto.php';
require_once __DIR__ . '/src/Client.php';
require_once __DIR__ . '/src/Resource.php';
require_once __DIR__ . '/src/Emails.php';
require_once __DIR__ . '/src/Domains.php';
require_once __DIR__ . '/src/Segments.php';
require_once __DIR__ . '/src/Contacts.php';
require_once __DIR__ . '/src/ContactProperties.php';
require_once __DIR__ . '/src/Topics.php';
require_once __DIR__ . '/src/Templates.php';
require_once __DIR__ . '/src/Broadcasts.php';
require_once __DIR__ . '/src/Webhooks.php';
require_once __DIR__ . '/src/Logs.php';
require_once __DIR__ . '/src/Stats.php';
require_once __DIR__ . '/src/McpProviderTrait.php';
/**
* Resend
*
* WireMail implementation and full API client for Resend (resend.com).
*
* Sending:
* $m = $mail->new(['module' => 'Resend']);
* $m->to('user@example.com')->subject('Hello')->bodyHTML('<p>Hi</p>')->send();
*
* Resend extras (fluent):
* $m->scheduledAt('in 1 hour');
* $m->addTag('category', 'welcome');
* $m->idempotencyKey('welcome-user-123');
* $m->useTemplate('tmpl_id', ['CTA' => 'Sign up']);
* $m->topicId('topic_id');
*
* API client (resource-based):
* $resend = $modules->get('Resend');
* $resend->domains()->all();
* $resend->contacts()->create(['email' => 'a@b.com']);
*
* All methods return decoded arrays. On error, ['error' => [...]] is returned
* and the last error is available via $resend->getLastError().
*
* Copyright (c) 2026 Maxim Semenov <maxim@smnv.org>
* License: MIT
*/
class Resend extends WireMail implements Module, ConfigurableModule {
use ResendMcpProviderTrait;
public static function getModuleInfo() {
return [
'title' => 'Resend',
'summary' => 'Send email via the Resend API. Full API client: emails, batch, scheduling, domains, contacts, segments, topics, templates, broadcasts, webhooks, logs.',
'version' => 102,
'author' => 'Maxim Semenov',
'href' => 'https://github.com/mxmsmnv/Resend',
'icon' => 'envelope-o',
'singular' => false,
'autoload' => false,
'mcpProvider' => true,
'requires' => [
'ProcessWire>=3.0.184',
'PHP>=8.1',
],
'installs' => ['ProcessResend', 'ResendWebhooks', 'WireMailResend'],
];
}
const LOG_NAME = 'resend';
/** Dedicated settings table; the API key is stored encrypted (see Crypto) */
const SETTINGS_TABLE = 'resend_settings';
/** Config defaults; also the whitelist of setting names */
const CONFIG_DEFAULTS = [
'apiKey' => '',
'webhookSigningSecret' => '',
'fromEmail' => '',
'fromName' => '',
'replyToEmail' => '',
'defaultTopicId' => '',
'defaultTags' => '',
'globalBcc' => '',
'redirectEmail' => '',
'httpRetries' => 0,
'disableSending' => 0,
'logSends' => 1,
'httpTimeout' => 30,
];
/** Settings stored encrypted at rest (see Crypto) */
const ENCRYPTED_SETTINGS = ['apiKey', 'webhookSigningSecret'];
/** @var array|null Per-request settings cache, shared across instances */
protected static $settings = null;
/** @var bool Whether the Modules::saveConfig hook was attached this request */
protected static $configHooked = false;
/** @var Client|null */
protected $client = null;
/** @var array Resource instances cache */
protected $resources = [];
/** Default Resend-specific send options; restored after each completed send */
const OPTION_DEFAULTS = [
'scheduledAt' => '',
'tags' => [],
'idempotencyKey' => '',
'template' => null,
'topicId' => '',
];
/** @var array Extra Resend-specific send options */
protected $resendOptions = self::OPTION_DEFAULTS;
/** @var string|null Last sent email id (single send) */
protected $lastEmailId = null;
/** @var int Recipient count before the dev redirect rewrites the payload */
protected $intendedRecipients = 0;
public function __construct() {
parent::__construct();
foreach(self::CONFIG_DEFAULTS as $key => $value) $this->set($key, $value);
}
public function init() {
// Settings live in a dedicated table, not in the modules table
foreach($this->loadSettings() as $key => $value) $this->set($key, $value);
if(!self::$configHooked) {
self::$configHooked = true;
// Route module config saves (admin config screen) into our table
$this->wire()->modules->addHookBefore('saveConfig', $this, 'hookModulesSaveConfig');
}
}
/*
* ------------------------------------------------------------------
* API client accessors
* ------------------------------------------------------------------
*/
/**
* @return Client
*/
public function client() {
if($this->client === null) {
$this->client = new Client($this, $this->apiKey, (int) $this->httpTimeout, (int) $this->httpRetries);
}
return $this->client;
}
/**
* @return Emails
*/
public function emails() {
return $this->resource(Emails::class);
}
/**
* @return Domains
*/
public function domains() {
return $this->resource(Domains::class);
}
/**
* @return Segments
*/
public function segments() {
return $this->resource(Segments::class);
}
/**
* @return Contacts
*/
public function contacts() {
return $this->resource(Contacts::class);
}
/**
* @return ContactProperties
*/
public function contactProperties() {
return $this->resource(ContactProperties::class);
}
/**
* @return Topics
*/
public function topics() {
return $this->resource(Topics::class);
}
/**
* @return Templates
*/
public function templates() {
return $this->resource(Templates::class);
}
/**
* @return Broadcasts
*/
public function broadcasts() {
return $this->resource(Broadcasts::class);
}
/**
* @return Webhooks
*/
public function webhooks() {
return $this->resource(Webhooks::class);
}
/**
* @return Logs
*/
public function logs() {
return $this->resource(Logs::class);
}
/**
* @param string $class
* @return object
*/
protected function resource($class) {
if(!isset($this->resources[$class])) {
$this->resources[$class] = new $class($this->client());
}
return $this->resources[$class];
}
/**
* Whether a response array is an error.
*
* @param mixed $result
* @return bool
*/
public function isError($result) {
return $this->client()->isError($result);
}
/**
* @return array|null
*/
public function getLastError() {
return $this->client()->getLastError();
}
/**
* @return array|null
*/
public function getLastResponse() {
return $this->client()->getLastResponse();
}
/**
* Verify a Resend (Svix) webhook signature.
*
* @param string $payload Raw request body
* @param array $headers Request headers
* @param string $signingSecret Webhook signing secret (whsec_...); defaults
* to the webhookSigningSecret module setting when omitted
* @return bool
*/
public function verifyWebhookSignature($payload, array $headers, $signingSecret = '') {
if(!strlen((string) $signingSecret)) $signingSecret = (string) $this->webhookSigningSecret;
return Webhooks::verifySignature($payload, $headers, $signingSecret);
}
/**
* Permission level of the configured API key.
*
* Detected with a probe request (GET /domains) and cached for an hour,
* keyed by a hash of the API key so swapping the key re-detects
* immediately. Indeterminate results (invalid key, network error) are
* not cached and return an empty string.
*
* @return string 'full_access', 'sending_access' or '' when unknown
*/
public function apiKeyPermission() {
if(!strlen($this->apiKey)) return '';
$cache = $this->wire()->cache;
$cacheKey = $this->permissionCacheKey();
$cached = $cache->get($cacheKey);
if(is_string($cached) && strlen($cached)) return $cached;
$result = $this->domains()->all();
if(!$this->isError($result)) {
$permission = 'full_access';
} elseif($result['error']['statusCode'] === 401 && $result['error']['name'] === 'restricted_api_key') {
$permission = 'sending_access';
} else {
return '';
}
$cache->save($cacheKey, $permission, 3600);
return $permission;
}
/**
* Drop the cached permission and re-detect. Needed when the key's
* permission is changed on the Resend side while the key itself stays
* the same, so the hash-keyed cache would otherwise serve the old level.
*
* @return string New permission ('full_access', 'sending_access' or '')
*/
public function refreshApiKeyPermission() {
if(!strlen($this->apiKey)) return '';
$this->wire()->cache->delete($this->permissionCacheKey());
return $this->apiKeyPermission();
}
/**
* @return string
*/
protected function permissionCacheKey() {
return 'resend-key-permission-' . substr(sha1($this->apiKey), 0, 16);
}
/*
* ------------------------------------------------------------------
* WireMail implementation
* ------------------------------------------------------------------
*/
/**
* Send the email via Resend API.
*
* Resend-specific options (schedule, tags, idempotency key, template, topic)
* are reset after a completed send. On failure they are kept, so a retry
* reuses the same idempotency key.
*
* @return int Number of emails accepted by Resend
*/
public function ___send() {
if(!strlen($this->apiKey)) {
$this->logError('API key is not configured');
return 0;
}
$payload = $this->buildPayload();
if($payload === false) return 0;
if((int) $this->disableSending) {
$this->logMessage('Sending disabled (test mode). Payload: ' . $this->jsonForLog($this->payloadForLog($payload)));
$this->resendOptions = self::OPTION_DEFAULTS;
return $this->intendedRecipients;
}
$idempotencyKey = $this->resendOptions['idempotencyKey'];
if(!strlen($idempotencyKey) && (int) $this->httpRetries > 0) {
// With retries enabled a network failure after the server processed
// the request could double-send; an auto key makes retries safe
$idempotencyKey = 'pw-auto-' . bin2hex(random_bytes(16));
}
$result = $this->emails()->send($payload, $idempotencyKey);
if($this->isError($result)) {
$this->logError('Send failed: ' . $this->client()->errorToString($result['error']));
return 0;
}
$this->lastEmailId = isset($result['id']) ? $result['id'] : null;
$this->resendOptions = self::OPTION_DEFAULTS;
if((int) $this->logSends) {
$to = implode(', ', (array) $payload['to']);
$redirected = isset($payload['headers']['X-Original-To']) ? " (redirected from {$payload['headers']['X-Original-To']})" : '';
$this->logMessage("Sent id={$this->lastEmailId} to={$to}{$redirected} subject={$payload['subject']}");
}
return $this->intendedRecipients;
}
/**
* Build the /emails payload from WireMail properties and Resend options.
*
* @return array|false
*/
protected function buildPayload() {
$mail = $this->mail;
// From
$fromEmail = strlen($mail['from']) ? $mail['from'] : $this->fromEmail;
$fromName = strlen($mail['fromName']) ? $mail['fromName'] : $this->fromName;
if(!strlen($fromEmail)) {
$this->logError('No from address (set in module config or via from())');
return false;
}
$from = $this->formatAddress($fromEmail, $fromName);
// To (with optional names)
$to = [];
foreach($mail['to'] as $email) {
$name = isset($mail['toName'][$email]) ? $mail['toName'][$email] : '';
$to[] = $this->formatAddress($email, $name);
}
if(!count($to)) {
$this->logError('No recipients');
return false;
}
$payload = [
'from' => $from,
'to' => $to,
'subject' => $this->sanitizeLine($mail['subject']),
];
// Template mode excludes html/text
if(is_array($this->resendOptions['template'])) {
$payload['template'] = $this->resendOptions['template'];
} else {
if(strlen($mail['bodyHTML'])) $payload['html'] = $mail['bodyHTML'];
if(strlen($mail['body'])) $payload['text'] = $mail['body'];
if(!isset($payload['html']) && !isset($payload['text'])) {
$this->logError('No body or bodyHTML set');
return false;
}
}
// CC / BCC via headers set by WireMail::header('cc', ...)
$cc = $this->extractAddressHeader('cc');
$bcc = $this->extractAddressHeader('bcc');
if(count($cc)) $payload['cc'] = $cc;
if(count($bcc)) $payload['bcc'] = $bcc;
// Reply-To
$replyTo = strlen($mail['replyTo']) ? $mail['replyTo'] : $this->replyToEmail;
if(strlen($replyTo)) {
$replyToName = isset($mail['replyToName']) && strlen($mail['replyToName']) ? $mail['replyToName'] : '';
if(strpos($replyTo, '<') !== false) {
// Already in "Name <email>" format
$payload['reply_to'] = $this->sanitizeLine($replyTo);
} else {
$payload['reply_to'] = $this->formatAddress($replyTo, $replyToName);
}
}
// Custom headers (excluding cc/bcc handled above)
$headers = [];
foreach($mail['header'] as $key => $value) {
$lower = strtolower($key);
if($lower === 'cc' || $lower === 'bcc') continue;
$headers[$this->sanitizeLine($key)] = $this->sanitizeLine($value);
}
if(count($headers)) $payload['headers'] = $headers;
// Attachments
if(count($mail['attachments'])) {
$attachments = [];
foreach($mail['attachments'] as $filename => $filepath) {
if(!is_file($filepath)) continue;
$attachments[] = [
'filename' => $this->sanitizeLine(is_string($filename) && strlen($filename) ? $filename : basename($filepath)),
'content' => base64_encode(file_get_contents($filepath)),
];
}
if(count($attachments)) $payload['attachments'] = $attachments;
}
// Resend extras
if(strlen($this->resendOptions['scheduledAt'])) {
$payload['scheduled_at'] = $this->resendOptions['scheduledAt'];
}
// Config default tags first; per-send tags with the same name win
$tags = $this->defaultTagsArray();
foreach($this->resendOptions['tags'] as $tag) $tags[$tag['name']] = $tag;
if(count($tags)) $payload['tags'] = array_values($tags);
$topicId = strlen($this->resendOptions['topicId']) ? $this->resendOptions['topicId'] : $this->defaultTopicId;
if(strlen($topicId)) {
$payload['topic_id'] = $topicId;
}
$this->intendedRecipients = count($to);
if(strlen($this->redirectEmail)) {
// Dev redirect: deliver to one address, record the originals in headers
$headers = isset($payload['headers']) ? $payload['headers'] : [];
$headers['X-Original-To'] = $this->sanitizeLine(implode(', ', (array) $payload['to']));
if(isset($payload['cc'])) {
$headers['X-Original-Cc'] = $this->sanitizeLine(implode(', ', $payload['cc']));
unset($payload['cc']);
}
if(isset($payload['bcc'])) {
$headers['X-Original-Bcc'] = $this->sanitizeLine(implode(', ', $payload['bcc']));
unset($payload['bcc']);
}
$payload['headers'] = $headers;
$payload['to'] = [$this->sanitizeLine($this->redirectEmail)];
} elseif(strlen($this->globalBcc)) {
$bcc = isset($payload['bcc']) ? $payload['bcc'] : [];
foreach(explode(',', $this->globalBcc) as $address) {
$address = $this->sanitizeLine($address);
if(strlen($address) && !in_array($address, $bcc)) $bcc[] = $address;
}
if(count($bcc)) $payload['bcc'] = $bcc;
}
return $payload;
}
/**
* Parse the defaultTags setting ("name:value, name:value") into tag arrays
* keyed by name.
*
* @return array
*/
protected function defaultTagsArray() {
$out = [];
foreach(preg_split('/[,\n]/', (string) $this->defaultTags) as $pair) {
$pair = trim($pair);
if(!strlen($pair)) continue;
$parts = explode(':', $pair, 2);
$name = $this->sanitizeTag(trim($parts[0]));
if(!strlen($name)) continue;
$value = $this->sanitizeTag(isset($parts[1]) ? trim($parts[1]) : '');
$out[$name] = ['name' => $name, 'value' => strlen($value) ? $value : '1'];
}
return $out;
}
/**
* Format an address as "Name <email>" with CR/LF stripped.
*
* @param string $email
* @param string $name
* @return string
*/
protected function formatAddress($email, $name = '') {
$email = $this->sanitizeLine($email);
$name = $this->sanitizeLine($name);
// Strip angle brackets from names to keep the address parseable
$name = str_replace(['<', '>'], '', $name);
return strlen($name) ? "$name <$email>" : $email;
}
/**
* Strip CR/LF from a single-line value (header injection prevention).
*
* @param string $value
* @return string
*/
protected function sanitizeLine($value) {
return trim(str_replace(["\r", "\n"], ' ', (string) $value));
}
/**
* Extract cc/bcc addresses from WireMail headers.
*
* @param string $type 'cc' or 'bcc'
* @return array
*/
protected function extractAddressHeader($type) {
$out = [];
foreach($this->mail['header'] as $key => $value) {
if(strtolower($key) !== $type) continue;
// Split on commas outside double quotes: '"Doe, John" <a@b.com>' stays intact
foreach(preg_split('/,(?=(?:[^"]*"[^"]*")*[^"]*$)/', $value) as $address) {
$address = $this->sanitizeLine($address);
if(strlen($address)) $out[] = $address;
}
}
return $out;
}
/*
* ------------------------------------------------------------------
* Fluent Resend extras
* ------------------------------------------------------------------
*/
/**
* Schedule the email. Natural language ('in 1 hour') or ISO 8601.
*
* @param string $when
* @return $this
*/
public function scheduledAt($when) {
$this->resendOptions['scheduledAt'] = $this->sanitizeLine($when);
return $this;
}
/**
* Add a tag (key/value, ASCII letters, numbers, _ and - only).
*
* @param string $name
* @param string $value
* @return $this
*/
public function addTag($name, $value) {
$this->resendOptions['tags'][] = [
'name' => $this->sanitizeTag($name),
'value' => $this->sanitizeTag($value),
];
return $this;
}
/**
* Set an idempotency key to prevent duplicate sends.
*
* @param string $key
* @return $this
*/
public function idempotencyKey($key) {
$this->resendOptions['idempotencyKey'] = (string) $key;
return $this;
}
/**
* Send using a published Resend template. Excludes html/text body.
*
* @param string $id Template id or alias
* @param array $variables Key/value template variables
* @return $this
*/
public function useTemplate($id, array $variables = []) {
$template = ['id' => (string) $id];
if(count($variables)) $template['variables'] = $variables;
$this->resendOptions['template'] = $template;
return $this;
}
/**
* Set the topic id for subscription-aware sending.
*
* @param string $topicId
* @return $this
*/
public function topicId($topicId) {
$this->resendOptions['topicId'] = (string) $topicId;
return $this;
}
/**
* Get id of the last sent email.
*
* @return string|null
*/
public function getLastEmailId() {
return $this->lastEmailId;
}
protected function sanitizeTag($value) {
return substr(preg_replace('/[^a-zA-Z0-9_-]/', '_', (string) $value), 0, 256);
}
/*
* ------------------------------------------------------------------
* Logging
* ------------------------------------------------------------------
*/
/**
* Payload copy safe for logging: attachment contents replaced with their
* size, html/text bodies truncated.
*
* @param array $payload
* @return array
*/
protected function payloadForLog(array $payload) {
if(isset($payload['attachments'])) {
foreach($payload['attachments'] as $i => $attachment) {
$payload['attachments'][$i]['content'] = '[' . strlen($attachment['content']) . ' bytes base64]';
}
}
foreach(['html', 'text'] as $key) {
if(isset($payload[$key]) && strlen($payload[$key]) > 1024) {
$payload[$key] = substr($payload[$key], 0, 1024) . '... [truncated]';
}
}
return $payload;
}
protected function jsonForLog(array $data) {
$json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
return $json === false ? '[unencodable payload]' : $json;
}
protected function logMessage($message) {
$this->wire()->log->save(self::LOG_NAME, $this->sanitizeLine($message));
}
protected function logError($message) {
$message = $this->sanitizeLine($message);
$this->wire()->log->save(self::LOG_NAME, 'ERROR: ' . $message);
$this->error($message, Notice::log);
}
/*
* ------------------------------------------------------------------
* Settings storage (dedicated table, encrypted API key)
* ------------------------------------------------------------------
*/
public function ___install() {
$this->createSettingsTable();
}
public function ___uninstall() {
$this->wire()->database->exec('DROP TABLE IF EXISTS `' . self::SETTINGS_TABLE . '`');
try {
$this->wire()->cache->delete('resend-key-permission-*');
} catch(\Exception $e) {
// Cache entries expire on their own within an hour
}
}
public function ___upgrade($fromVersion, $toVersion) {
$this->createSettingsTable();
$this->loadSettings(); // triggers legacy config migration when the table is empty
$modules = $this->wire()->modules;
if(!$modules->isInstalled('ResendWebhooks')) {
try {
$modules->install('ResendWebhooks');
} catch(\Exception $e) {
// Available for manual install from the modules screen
}
}
if(!$modules->isInstalled('WireMailResend')) {
try {
$modules->install('WireMailResend');
} catch(\Exception $e) {
// Available for manual install from the modules screen
}
}
}
protected function createSettingsTable() {
$config = $this->wire()->config;
$engine = $config->dbEngine ?: 'InnoDB';
$charset = $config->dbCharset ?: 'utf8mb4';
$this->wire()->database->exec(
'CREATE TABLE IF NOT EXISTS `' . self::SETTINGS_TABLE . '` (' .
'`name` VARCHAR(128) NOT NULL, ' .
'`value` MEDIUMTEXT, ' .
'`modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, ' .
"PRIMARY KEY (`name`)) ENGINE=$engine DEFAULT CHARSET=$charset"
);
}
/**
* Site salt used for settings encryption. Per config.php guidance,
* tableSalt is meant for non-authentication hashing; userAuthSalt is the
* fallback for older installs without it. Never stored in the database.
*
* @return string
*/
protected function settingsSalt() {
$config = $this->wire()->config;
$salt = (string) $config->tableSalt;
return strlen($salt) ? $salt : (string) $config->userAuthSalt;
}
/**
* Load settings from the dedicated table (per-request cache). Creates the
* table and migrates legacy module config on first use after an upgrade.
*
* @return array name => value, apiKey decrypted
*/
protected function loadSettings() {
if(is_array(self::$settings)) return self::$settings;
$database = $this->wire()->database;
$rows = [];
try {
$query = $database->query('SELECT `name`, `value` FROM `' . self::SETTINGS_TABLE . '`');
$rows = $query->fetchAll(\PDO::FETCH_KEY_PAIR);
} catch(\Exception $e) {
// Table missing: pre-1.8 installation loading updated files
try {
$this->createSettingsTable();
} catch(\Exception $e2) {
return [];
}
}
if(!is_array($rows)) $rows = [];
if(!count($rows)) $rows = $this->migrateLegacyConfig();
$settings = array_intersect_key($rows, self::CONFIG_DEFAULTS);
foreach(self::ENCRYPTED_SETTINGS as $name) {
if(!isset($settings[$name]) || !strlen($settings[$name])) continue;
$decrypted = Crypto::decrypt($settings[$name], $this->settingsSalt());
if(!strlen($decrypted) && Crypto::isEncrypted($settings[$name])) {
$this->logError("Stored $name cannot be decrypted (site salt changed?). Re-enter it in the module config.");
}
$settings[$name] = $decrypted;
}
self::$settings = $settings;
return $settings;
}
/**
* One-time migration of legacy config from the modules table.
*
* @return array Stored rows (apiKey encrypted)
*/
protected function migrateLegacyConfig() {
$modules = $this->wire()->modules;
$legacy = $modules->getConfig('Resend');
if(!is_array($legacy)) return [];
$legacy = array_intersect_key($legacy, self::CONFIG_DEFAULTS);
if(!count($legacy)) return [];
$rows = $this->saveSettings($legacy);
// Clear the modules table copy; the empty-data save passes the hook through
$modules->saveConfig('Resend', []);
$this->wire()->log->save(self::LOG_NAME, 'Settings migrated to the ' . self::SETTINGS_TABLE . ' table; encrypted settings require tableSalt or userAuthSalt.');
return $rows;
}
/**
* Persist settings to the dedicated table. The API key is encrypted with
* the site salt before storage.
*
* @param array $data name => plaintext value; unknown names are dropped
* @return array Rows as written (apiKey encrypted)
*/
public function saveSettings(array $data) {
$rows = array_intersect_key($data, self::CONFIG_DEFAULTS);
foreach(self::ENCRYPTED_SETTINGS as $name) {
if(!isset($rows[$name])) continue;
if(strlen((string) $rows[$name])) {
$encrypted = Crypto::encrypt($rows[$name], $this->settingsSalt());
if(!strlen($encrypted)) {
$this->logError("Cannot store $name because encrypted settings are unavailable. Configure tableSalt or userAuthSalt in config.php.");
}
$rows[$name] = $encrypted;
} else {
$rows[$name] = '';
}
}
$stmt = $this->wire()->database->prepare(
'INSERT INTO `' . self::SETTINGS_TABLE . '` (`name`, `value`) VALUES (:name, :value) ' .
'ON DUPLICATE KEY UPDATE `value` = :value2'
);
foreach($rows as $name => $value) {
$stmt->execute([':name' => $name, ':value' => (string) $value, ':value2' => (string) $value]);
}
self::$settings = null;
return $rows;
}
/**
* Hook before Modules::saveConfig — intercepts module config screen saves
* for this module, stores them in the dedicated table and keeps the
* modules table entry empty. Empty-data saves pass through untouched.
*
* @param HookEvent $event
*/
public function hookModulesSaveConfig(HookEvent $event) {
$class = $event->arguments(0);
$className = is_object($class) ? $class->className() : (string) $class;
if($className !== 'Resend') return;
$data = $event->arguments(1);
// Modules::saveConfig($class, $property, $value) single-value form
if(is_string($data)) $data = [$data => $event->arguments(2)];
if(!is_array($data) || !count($data)) return;
$settings = $this->loadSettings();
// A blank secret on the config screen means "keep the current value"
foreach(self::ENCRYPTED_SETTINGS as $name) {
if(array_key_exists($name, $data) && !strlen((string) $data[$name])) {
$data[$name] = isset($settings[$name]) ? $settings[$name] : '';
}
}
$this->saveSettings(array_merge($settings, $data));
$event->arguments(1, []);
$event->arguments(2, null);
}
/*
* ------------------------------------------------------------------
* Module config
* ------------------------------------------------------------------
*/
public function getModuleConfigInputfields(InputfieldWrapper $inputfields) {
$modules = $this->wire()->modules;
if($this->className() === 'WireMailResend') {
/** @var InputfieldMarkup $f */
$f = $modules->get('InputfieldMarkup');
$f->label = $this->_('Compatibility Alias');
$f->icon = 'info-circle';
$url = $this->wire()->sanitizer->entities($this->wire()->config->urls->admin . 'module/edit?name=Resend');
$f->value = '<p>WireMailResend is installed only so modules that look for <code>WireMail*</code> providers can detect Resend.</p>' .
'<p>It uses the same encrypted settings, sender defaults and delivery behavior as Resend. Configure the module from <a href="' . $url . '">Resend settings</a>.</p>';
$inputfields->add($f);
return $inputfields;
}
/*
* API Access
*/
/** @var InputfieldFieldset $fs */
$fs = $modules->get('InputfieldFieldset');
$fs->label = $this->_('API Access');
$fs->icon = 'key';
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->attr('name', 'apiKey');
$f->label = $this->_('API Key');
// The stored key is never rendered back into the form
$f->attr('value', '');
$f->attr('type', 'password');
$f->attr('autocomplete', 'new-password');
$f->icon = 'key';
if(strlen($this->apiKey)) {
$f->description = $this->_('A key is configured and stored encrypted in the database. Enter a new key to replace it, or leave blank to keep the current one.');
$f->collapsed = Inputfield::collapsedYes;
} else {
$f->description = $this->_('Resend API key (re_...). Create one at resend.com/api-keys. Stored encrypted in a dedicated table using the site salt from config.php.');
$f->required = true;
}
$fs->add($f);
$f = $modules->get('InputfieldText');
$f->attr('name', 'webhookSigningSecret');
$f->label = $this->_('Webhook Signing Secret');
$f->attr('value', '');
$f->attr('type', 'password');
$f->attr('autocomplete', 'new-password');
$f->icon = 'plug';
if(strlen($this->webhookSigningSecret)) {
$f->description = $this->_('A secret is configured and stored encrypted. Enter a new value to replace it, or leave blank to keep the current one.');
$f->collapsed = Inputfield::collapsedYes;
} else {
$f->description = $this->_('Optional. Webhook signing secret (whsec_...). When set, verifyWebhookSignature() works without passing the secret. Stored encrypted.');
$f->collapsed = Inputfield::collapsedNo;
}
$fs->add($f);
$inputfields->add($fs);
/*
* Sending Defaults
*/
$fs = $modules->get('InputfieldFieldset');
$fs->label = $this->_('Sending Defaults');
$fs->icon = 'envelope-o';
$f = $modules->get('InputfieldEmail');
$f->attr('name', 'fromEmail');
$f->label = $this->_('Default From Email');
$f->description = $this->_('Must belong to a domain verified in Resend.');
$f->attr('value', $this->fromEmail);
$f->columnWidth = 50;
$fs->add($f);
$f = $modules->get('InputfieldText');
$f->attr('name', 'fromName');
$f->label = $this->_('Default From Name');
$f->attr('value', $this->fromName);
$f->columnWidth = 50;
$fs->add($f);
$f = $modules->get('InputfieldEmail');
$f->attr('name', 'replyToEmail');
$f->label = $this->_('Default Reply-To Email');
$f->attr('value', $this->replyToEmail);
$f->columnWidth = 50;
$fs->add($f);
$f = $modules->get('InputfieldText');
$f->attr('name', 'defaultTopicId');
$f->label = $this->_('Default Topic ID');
$f->description = $this->_('Optional. Applied to all sends unless overridden via topicId().');
$f->attr('value', $this->defaultTopicId);
$f->columnWidth = 50;
$fs->add($f);
$f = $modules->get('InputfieldText');
$f->attr('name', 'defaultTags');
$f->label = $this->_('Default Tags');
$f->description = $this->_('Optional. name:value pairs, comma separated (ASCII letters, digits, _ and -). Attached to every send; per-send tags with the same name take precedence.');
$f->attr('value', $this->defaultTags);
$f->attr('placeholder', 'site:example, env:production');
$fs->add($f);
$inputfields->add($fs);
/*
* Delivery Behavior
*/