forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMail.php
1014 lines (892 loc) · 33.5 KB
/
Mail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
use Symfony\Component\Mailer\Exception\ExceptionInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\SendmailTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Crypto\DkimSigner;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\IdentificationHeader;
/**
* Class MailCore.
*/
class MailCore extends ObjectModel
{
public $id;
/** @var string Recipient */
public $recipient;
/** @var string Template */
public $template;
/** @var string Subject */
public $subject;
/** @var int Language ID */
public $id_lang;
/** @var int Timestamp */
public $date_add;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'mail',
'primary' => 'id_mail',
'fields' => [
'recipient' => [
'type' => self::TYPE_STRING,
'validate' => 'isEmail',
'copy_post' => false,
'required' => true,
'size' => 255,
],
'template' => [
'type' => self::TYPE_STRING,
'validate' => 'isTplName',
'copy_post' => false,
'required' => true,
'size' => 62,
],
'subject' => [
'type' => self::TYPE_STRING,
'validate' => 'isMailSubject',
'copy_post' => false,
'required' => true,
'size' => 255,
],
'id_lang' => [
'type' => self::TYPE_INT,
'validate' => 'isUnsignedId',
'copy_post' => false,
'required' => true,
],
'date_add' => [
'type' => self::TYPE_DATE,
'validate' => 'isDate',
'copy_post' => false,
'required' => true,
],
],
];
/**
* Mail content type.
*/
public const TYPE_HTML = 1;
public const TYPE_TEXT = 2;
public const TYPE_BOTH = 3;
/**
* Send mail under SMTP server.
*/
public const METHOD_SMTP = 2;
/**
* Disable mail, will return immediately after calling send method.
*/
public const METHOD_DISABLE = 3;
/**
* Send Email.
*
* @param int $idLang Language ID of the email (to translate the template)
* @param string $template Template: the name of template not be a var but a string !
* @param string $subject Subject of the email
* @param array $templateVars Template variables for the email
* @param string|array<string> $to To email
* @param string|array<string> $toName To name
* @param string $from From email
* @param string $fromName To email
* @param array $fileAttachment array with three parameters (content, mime and name).
* You can use an array of array to attach multiple files
* @param bool $mode_smtp SMTP mode (deprecated)
* @param string $templatePath Template path
* @param bool $die Die after error
* @param int $idShop Shop ID
* @param string|array<string>|null $bcc Bcc recipient address. You can use an array of array to send to multiple recipients
* @param string|null $replyTo Reply-To recipient address
* @param string|null $replyToName Reply-To recipient name
*
* @return bool|int Whether sending was successful. If not at all, false, otherwise amount of recipients succeeded.
*/
public static function send(
$idLang,
$template,
$subject,
$templateVars,
$to,
$toName = null,
$from = null,
$fromName = null,
$fileAttachment = null,
$mode_smtp = null,
$templatePath = _PS_MAIL_DIR_,
$die = false,
$idShop = null,
$bcc = null,
$replyTo = null,
$replyToName = ''
) {
if (!$idShop) {
$idShop = Context::getContext()->shop->id;
}
// An array [module_name => module_output] will be returned
$hookBeforeEmailResult = Hook::exec(
'actionEmailSendBefore',
[
'idLang' => &$idLang,
'template' => &$template,
'subject' => &$subject,
'templateVars' => &$templateVars,
'to' => &$to,
'toName' => &$toName,
'from' => &$from,
'fromName' => &$fromName,
'fileAttachment' => &$fileAttachment,
'mode_smtp' => &$mode_smtp,
'templatePath' => &$templatePath,
'die' => &$die,
'idShop' => &$idShop,
'bcc' => &$bcc,
'replyTo' => &$replyTo,
],
null,
true
);
if ($hookBeforeEmailResult === null) {
$keepGoing = false;
} else {
$keepGoing = array_reduce(
$hookBeforeEmailResult,
function ($carry, $item) {
return ($item === false) ? false : $carry;
},
true
);
}
if (!$keepGoing) {
return true;
}
if (is_numeric($idShop) && $idShop) {
$shop = new Shop((int) $idShop);
}
if (!isset($shop)) {
self::dieOrLog($die, 'Error: parameter "idShop" is corrupted');
return false;
}
$configuration = Configuration::getMultiple(
[
'PS_SHOP_EMAIL',
'PS_MAIL_METHOD',
'PS_MAIL_SERVER',
'PS_MAIL_USER',
'PS_MAIL_PASSWD',
'PS_SHOP_NAME',
'PS_MAIL_SMTP_ENCRYPTION',
'PS_MAIL_SMTP_PORT',
'PS_MAIL_TYPE',
'PS_MAIL_DKIM_ENABLE',
'PS_MAIL_DKIM_DOMAIN',
'PS_MAIL_DKIM_SELECTOR',
'PS_MAIL_DKIM_KEY',
],
null,
null,
$idShop
);
// Returns immediately if emails are deactivated
if ($configuration['PS_MAIL_METHOD'] == self::METHOD_DISABLE) {
return true;
}
// Hook to alter template vars
Hook::exec(
'sendMailAlterTemplateVars',
[
'template' => $template,
'template_vars' => &$templateVars,
]
);
if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION'])
|| Tools::strtolower($configuration['PS_MAIL_SMTP_ENCRYPTION']) === 'off'
) {
$isTls = false;
} else {
$isTls = true;
}
if (!isset($configuration['PS_MAIL_SMTP_PORT'])) {
$configuration['PS_MAIL_SMTP_PORT'] = 'default';
}
/*
* Sending an e-mail can be of vital importance for the merchant, when his password
* is lost for example, so we must not die but do our best to send the e-mail.
*/
if (!isset($from) || !Validate::isEmail($from)) {
$from = $configuration['PS_SHOP_EMAIL'];
}
if (!Validate::isEmail($from)) {
$from = null;
}
// $from_name is not that important, no need to die if it is not valid
if (!isset($fromName) || !Validate::isMailName($fromName)) {
$fromName = $configuration['PS_SHOP_NAME'];
}
if (!Validate::isMailName($fromName)) {
$fromName = null;
}
/*
* It would be difficult to send an e-mail if the e-mail is not valid,
* so this time we can die if there is a problem.
*/
if (!is_array($to) && !Validate::isEmail($to)) {
self::dieOrLog($die, 'Error: parameter "to" is corrupted');
return false;
}
// if bcc is not null, make sure it's a valid e-mail
if (null !== $bcc && !is_array($bcc) && !Validate::isEmail($bcc)) {
self::dieOrLog($die, 'Error: parameter "bcc" is corrupted');
$bcc = null;
}
if (!is_array($templateVars)) {
$templateVars = [];
}
// Do not crash for this error, that may be a complicated customer name
if (is_string($toName) && !empty($toName) && !Validate::isMailName($toName)) {
$toName = null;
}
if (!Validate::isTplName($template)) {
self::dieOrLog($die, 'Error: invalid e-mail template');
return false;
}
if (!Validate::isMailSubject($subject)) {
self::dieOrLog($die, 'Error: invalid e-mail subject');
return false;
}
$email = new Email();
/* Construct multiple recipients list if needed */
if (is_array($to)) {
foreach ($to as $key => $addr) {
$addr = trim($addr);
if (!Validate::isEmail($addr)) {
self::dieOrLog($die, 'Error: invalid e-mail address');
return false;
}
if (is_array($toName) && isset($toName[$key])) {
$addrName = $toName[$key];
} else {
$addrName = $toName;
}
$addrName = ($addrName == null || $addrName == $addr || !Validate::isGenericName($addrName)) ?
'' :
self::mimeEncode($addrName);
$email->addTo(new Address(self::toPunycode($addr), $addrName));
}
$toPlugin = $to[0];
} else {
/* Simple recipient, one address */
$toPlugin = $to;
$toName = (($toName == null || $toName == $to) ? '' : self::mimeEncode($toName));
$email->addTo(new Address(self::toPunycode($to), $toName));
}
if (isset($bcc) && is_array($bcc)) {
foreach ($bcc as $addr) {
$addr = trim($addr);
if (!Validate::isEmail($addr)) {
self::dieOrLog($die, 'Error: invalid e-mail address');
return false;
}
$email->addBcc(new Address(self::toPunycode($addr)));
}
} elseif (isset($bcc)) {
$email->addBcc(new Address(self::toPunycode($bcc)));
}
try {
/* Connect with the appropriate configuration */
if ($configuration['PS_MAIL_METHOD'] == self::METHOD_SMTP) {
if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT'])) {
self::dieOrLog($die, 'Error: invalid SMTP server or SMTP port');
return false;
}
$transport = (new EsmtpTransport(
$configuration['PS_MAIL_SERVER'],
$configuration['PS_MAIL_SMTP_PORT'],
$isTls
))
->setUsername($configuration['PS_MAIL_USER'])
->setPassword($configuration['PS_MAIL_PASSWD'])
;
} else {
$transport = new SendmailTransport();
}
$mailer = new Mailer($transport);
/* Get templates content */
$iso = Language::getIsoById((int) $idLang);
$isoDefault = Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT'));
$isoArray = [];
if ($iso) {
$isoArray[] = $iso;
}
if ($isoDefault && $iso !== $isoDefault) {
$isoArray[] = $isoDefault;
}
if (!in_array('en', $isoArray)) {
$isoArray[] = 'en';
}
$moduleName = false;
// get templatePath
if (preg_match('#' . $shop->physical_uri . 'modules/#', str_replace(DIRECTORY_SEPARATOR, '/', $templatePath))
&& preg_match('#modules/([a-z0-9_-]+)/#ui', str_replace(DIRECTORY_SEPARATOR, '/', $templatePath), $res)
) {
$moduleName = $res[1];
}
$isoTemplate = '';
foreach ($isoArray as $isoCode) {
$isoTemplate = $isoCode . '/' . $template;
$templatePath = self::getTemplateBasePath($isoTemplate, $moduleName, $shop->theme);
if (!file_exists($templatePath . $isoTemplate . '.txt')
&& (
$configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH
|| $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT
)
) {
PrestaShopLogger::addLog(
Context::getContext()->getTranslator()->trans(
'Error - The following e-mail template is missing: %s',
[$templatePath . $isoTemplate . '.txt'],
'Admin.Advparameters.Notification'
)
);
} elseif (!file_exists($templatePath . $isoTemplate . '.html')
&& (
$configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH
|| $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML
)
) {
PrestaShopLogger::addLog(
Context::getContext()->getTranslator()->trans(
'Error - The following e-mail template is missing: %s',
[$templatePath . $isoTemplate . '.html'],
'Admin.Advparameters.Notification'
)
);
} else {
$templatePathExists = true;
break;
}
}
if (empty($templatePathExists)) {
self::dieOrLog($die, 'Error - The following e-mail template is missing: %s', [$template]);
return false;
}
$templateHtml = '';
$templateTxt = '';
// An array [module_name => module_output] will be returned (no effect)
Hook::exec(
'actionEmailAddBeforeContent',
[
'template' => $template,
'template_html' => &$templateHtml,
'template_txt' => &$templateTxt,
'id_lang' => (int) $idLang,
],
null,
true
);
$templateHtml .= Tools::file_get_contents($templatePath . $isoTemplate . '.html');
$templateTxt .= strip_tags(
html_entity_decode(
Tools::file_get_contents($templatePath . $isoTemplate . '.txt'),
ENT_COMPAT,
'utf-8'
)
);
// An array [module_name => module_output] will be returned (no effect)
Hook::exec(
'actionEmailAddAfterContent',
[
'template' => $template,
'template_html' => &$templateHtml,
'template_txt' => &$templateTxt,
'id_lang' => (int) $idLang,
],
null,
true
);
/* Create mail and attach differents parts */
if (Configuration::get('PS_MAIL_SUBJECT_PREFIX')) {
$subject = '[' . strip_tags($configuration['PS_SHOP_NAME']) . '] ' . $subject;
}
$email->subject($subject);
/* Set Message-ID - getmypid() is blocked on some hosting */
$email
->getHeaders()
->add(new IdentificationHeader('Message-ID', Mail::generateId()))
;
if (!($replyTo && Validate::isEmail($replyTo))) {
$replyTo = $from;
}
if (!empty($replyTo) && $replyTo != $toPlugin) {
$email->replyTo(new Address($replyTo, (string) $replyToName));
}
if (false !== Configuration::get('PS_LOGO_MAIL', null, null, $idShop)
&& file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $idShop))
) {
$logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $idShop);
} else {
if (file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $idShop))) {
$logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $idShop);
} else {
$templateVars['{shop_logo}'] = '';
}
}
ShopUrl::cacheMainDomainForShop((int) $idShop);
/* don't attach the logo as */
if (isset($logo) && $configuration['PS_MAIL_TYPE'] != Mail::TYPE_TEXT) {
$templateVars['{shop_logo}'] = 'cid:shop_logo';
$email->embedFromPath($logo, 'shop_logo');
}
if (!(Context::getContext()->link instanceof Link)) {
Context::getContext()->link = new Link();
}
$templateVars['{shop_name}'] = Tools::safeOutput($configuration['PS_SHOP_NAME']);
$templateVars['{shop_url}'] = Context::getContext()->link->getPageLink(
'index',
null,
$idLang,
null,
false,
$idShop
);
$templateVars['{my_account_url}'] = Context::getContext()->link->getPageLink(
'my-account',
null,
$idLang,
null,
false,
$idShop
);
$templateVars['{guest_tracking_url}'] = Context::getContext()->link->getPageLink(
'guest-tracking',
null,
$idLang,
null,
false,
$idShop
);
$templateVars['{history_url}'] = Context::getContext()->link->getPageLink(
'history',
null,
$idLang,
null,
false,
$idShop
);
$templateVars['{order_slip_url}'] = Context::getContext()->link->getPageLink(
'order-slip',
null,
$idLang,
null,
false,
$idShop
);
$templateVars['{color}'] = Tools::safeOutput(Configuration::get('PS_MAIL_COLOR', null, null, $idShop));
// Get extra template_vars
$extraTemplateVars = [];
// An array [module_name => module_output] will be returned (no effect)
Hook::exec(
'actionGetExtraMailTemplateVars',
[
'template' => $template,
'template_vars' => $templateVars,
'extra_template_vars' => &$extraTemplateVars,
'id_lang' => (int) $idLang,
],
null,
true
);
$templateVars = array_merge($templateVars, $extraTemplateVars);
if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH
|| $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML
) {
$templateHtml = strtr($templateHtml, $templateVars);
$email->html($templateHtml);
if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH) {
$templateTxt = strtr($templateTxt, $templateVars);
$email->text($templateTxt);
}
} else {
$templateTxt = strtr($templateTxt, $templateVars);
$email->text($templateTxt);
}
if (!empty($fileAttachment)) {
// Multiple attachments?
if (!is_array(current($fileAttachment))) {
$fileAttachment = [$fileAttachment];
}
foreach ($fileAttachment as $attachment) {
if (isset($attachment['content'], $attachment['name'], $attachment['mime'])) {
$email->attach($attachment['content'], $attachment['name'], $attachment['mime']);
}
}
}
/* Send mail */
$email->from(new Address($from, (string) $fromName));
// Hook to alter Symfony Mailer before sending mail
Hook::exec('actionMailAlterMessageBeforeSend', [
'message' => &$email,
]);
/* Create new message and DKIM sign it, if enabled and all data for signature are provided */
if ((bool) $configuration['PS_MAIL_DKIM_ENABLE'] === true
&& !empty($configuration['PS_MAIL_DKIM_DOMAIN'])
&& !empty($configuration['PS_MAIL_DKIM_SELECTOR'])
&& !empty($configuration['PS_MAIL_DKIM_KEY'])
) {
$signer = new DkimSigner(
$configuration['PS_MAIL_DKIM_KEY'],
$configuration['PS_MAIL_DKIM_DOMAIN'],
$configuration['PS_MAIL_DKIM_SELECTOR']
);
$signedEmail = $signer->sign($email);
}
$mailer->send($signedEmail ?? $email);
ShopUrl::resetMainDomainCache();
if (Configuration::get('PS_LOG_EMAILS')) {
$mail = new Mail();
$mail->template = Tools::substr($template, 0, 62);
$mail->subject = Tools::substr($email->getSubject(), 0, 255);
$mail->id_lang = (int) $idLang;
$recipientsTo = self::convertAdressesToArray($email->getTo());
$recipientsCc = self::convertAdressesToArray($email->getCc());
$recipientsBcc = self::convertAdressesToArray($email->getBcc());
if (!is_array($recipientsTo)) {
$recipientsTo = [];
}
if (!is_array($recipientsCc)) {
$recipientsCc = [];
}
if (!is_array($recipientsBcc)) {
$recipientsBcc = [];
}
foreach (array_merge($recipientsTo, $recipientsCc, $recipientsBcc) as $emailAlias => $recipient_name) {
$mail->id = null;
$mail->recipient = Tools::substr($emailAlias, 0, 255);
$mail->add();
}
}
return true;
} catch (ExceptionInterface $e) {
PrestaShopLogger::addLog(
'Mailer Error: ' . $e->getMessage(),
3,
null,
'MailerMessage'
);
return false;
}
}
protected static function getTemplateBasePath($isoTemplate, $moduleName, $theme)
{
$basePathList = [
_PS_ROOT_DIR_ . '/themes/' . $theme->getName() . '/',
_PS_ROOT_DIR_ . '/themes/' . $theme->get('parent') . '/',
_PS_ROOT_DIR_,
];
if ($moduleName !== false) {
$templateRelativePath = '/modules/' . $moduleName . '/mails/';
} else {
$templateRelativePath = '/mails/';
}
foreach ($basePathList as $base) {
$templatePath = $base . $templateRelativePath;
if (file_exists($templatePath . $isoTemplate . '.txt') || file_exists($templatePath . $isoTemplate . '.html')) {
return $templatePath;
}
}
return '';
}
/**
* @param int $idMail Mail ID
*
* @return bool Whether removal succeeded
*/
public static function eraseLog($idMail)
{
return Db::getInstance()->delete('mail', 'id_mail = ' . (int) $idMail);
}
/**
* @return bool
*/
public static function eraseAllLogs()
{
return Db::getInstance()->execute('TRUNCATE TABLE ' . _DB_PREFIX_ . 'mail');
}
/**
* Send a test email.
*
* @param bool $smtpChecked Is SMTP checked?
* @param string $smtpServer SMTP Server hostname
* @param string $content Content of the email
* @param string $subject Subject of the email
* @param bool $type Deprecated
* @param string $to To email address
* @param string $from From email address
* @param string $smtpLogin SMTP login name
* @param string $smtpPassword SMTP password
* @param int $smtpPort SMTP Port
* @param bool|string $smtpEncryption Encryption type. "off" or false disable encryption.
*
* @return bool|string True if succeeded, otherwise the error message
*/
public static function sendMailTest(
$smtpChecked,
$smtpServer,
$content,
$subject,
$type,
$to,
$from,
$smtpLogin,
$smtpPassword,
$smtpPort,
$smtpEncryption,
bool $dkimEnable = false,
string $dkimKey = '',
string $dkimDomain = '',
string $dkimSelector = ''
) {
$result = false;
try {
if ($smtpChecked) {
if (Tools::strtolower($smtpEncryption) === 'off') {
$isTls = false;
} else {
$isTls = true;
}
$transport = (new EsmtpTransport(
$smtpServer,
$smtpPort,
$isTls
))
->setUsername($smtpLogin)
->setPassword($smtpPassword)
;
} else {
$transport = new SendmailTransport();
}
$mailer = new Mailer($transport);
$email = (new Email())
->from($from)
->to($to)
->subject($subject)
->text($content)
;
/* Create new message and DKIM sign it, if enabled and all data for signature are provided */
if ($dkimEnable === true
&& !empty($dkimKey)
&& !empty($dkimDomain)
&& !empty($dkimSelector)
) {
$signer = new DkimSigner(
$dkimKey,
$dkimDomain,
$dkimSelector
);
$signedEmail = $signer->sign($email);
}
$mailer->send($signedEmail ?? $email);
$result = true;
} catch (ExceptionInterface $e) {
$result = $e->getMessage();
}
return $result;
}
/**
* This method is used to get the translation for email Object.
* For an object is forbidden to use htmlentities,
* we have to return a sentence with accents.
*
* @param string $string raw sentence (write directly in file)
*
* @return mixed
*/
public static function l($string, $idLang = null, ?Context $context = null)
{
global $_LANGMAIL;
if (!$context) {
$context = Context::getContext();
}
if ($idLang === null) {
$idLang = (!isset($context->language) || !is_object($context->language)) ?
(int) Configuration::get('PS_LANG_DEFAULT') :
(int) $context->language->id;
}
$isoCode = Language::getIsoById((int) $idLang);
$file_core = _PS_ROOT_DIR_ . '/mails/' . $isoCode . '/lang.php';
if (Tools::file_exists_cache($file_core) && empty($_LANGMAIL)) {
include $file_core;
}
$fileTheme = _PS_THEME_DIR_ . 'mails/' . $isoCode . '/lang.php';
if (Tools::file_exists_cache($fileTheme)) {
include $fileTheme;
}
if (!is_array($_LANGMAIL)) {
return str_replace('"', '"', $string);
}
$key = str_replace('\'', '\\\'', $string);
return str_replace(
'"',
'"',
(array_key_exists($key, $_LANGMAIL) && !empty($_LANGMAIL[$key])) ? $_LANGMAIL[$key] : $string
);
}
/* Generate a Message-ID */
protected static function generateId($idstring = null)
{
$midparams = [
'utctime' => date('YmdHis'),
'randint' => mt_rand(),
'customstr' => ($idstring !== null && preg_match('/^(?<!\\.)[a-z0-9\\.]+(?!\\.)$/iD', $idstring) ? $idstring : 'mailer'),
'hostname' => !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : php_uname('n'),
];
return vsprintf('%s.%d.%s@%s', $midparams);
}
/**
* Check if a multibyte character set is used for the data.
*
* @param string $data Data
*
* @return bool Whether the string uses a multibyte character set
*/
public static function isMultibyte($data)
{
$length = Tools::strlen($data);
for ($i = 0; $i < $length; ++$i) {
if (ord($data[$i]) > 128) {
return true;
}
}
return false;
}
/**
* MIME encode the string.
*
* @param string $string The string to encode
* @param string $charset The character set to use
* @param string $newline The newline character(s)
*
* @return mixed|string MIME encoded string
*/
public static function mimeEncode($string, $charset = 'UTF-8', $newline = "\r\n")
{
if (!self::isMultibyte($string) && Tools::strlen($string) < 75) {
return $string;
}
$charset = Tools::strtoupper($charset);
$start = '=?' . $charset . '?B?';
$end = '?=';
$sep = $end . $newline . ' ' . $start;
$length = 75 - Tools::strlen($start) - Tools::strlen($end);
$length = $length - ($length % 4);
if ($charset === 'UTF-8') {
$parts = [];
$maxchars = floor(($length * 3) / 4);
$stringLength = Tools::strlen($string);
while ($stringLength > $maxchars) {
$i = (int) $maxchars;
$result = ord($string[$i]);
while ($result >= 128 && $result <= 191) {
$result = ord($string[--$i]);
}
$parts[] = base64_encode(Tools::substr($string, 0, $i));
$string = Tools::substr($string, $i);
$stringLength = Tools::strlen($string);
}
$parts[] = base64_encode($string);
$string = implode($sep, $parts);
} else {
$string = chunk_split(base64_encode($string), $length, $sep);
$string = preg_replace('/' . preg_quote($sep) . '$/', '', $string);
}
return $start . $string . $end;
}
/**
* Automatically convert email to Punycode.
*
* Try to use INTL_IDNA_VARIANT_UTS46 only if defined, else use INTL_IDNA_VARIANT_2003
* See https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
*
* @param string $to Email address
*
* @return string
*/
public static function toPunycode($to)
{
$address = explode('@', $to);
if (empty($address[0]) || empty($address[1])) {
return $to;
}
if (defined('INTL_IDNA_VARIANT_UTS46')) {
return $address[0] . '@' . idn_to_ascii($address[1], 0, INTL_IDNA_VARIANT_UTS46);
}
/*
* INTL_IDNA_VARIANT_2003 const will be removed in PHP 8.
* See https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
*/
if (defined('INTL_IDNA_VARIANT_2003')) {
return $address[0] . '@' . idn_to_ascii($address[1], 0, INTL_IDNA_VARIANT_2003);
}
return $address[0] . '@' . idn_to_ascii($address[1]);
}
/**
* Generic function to dieOrLog with translations.
*
* @param bool $die Should die
* @param string $message Message
* @param array $templates Templates list
* @param string $domain Translation domain
*/
protected static function dieOrLog(
$die,
$message,
$templates = [],
$domain = 'Admin.Advparameters.Notification'
) {
Tools::dieOrLog(
Context::getContext()->getTranslator()->trans(
$message,
$templates,
$domain
),
$die
);
}
/**
* @param Address[] $addresses