-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrachioMailer.php
1367 lines (1206 loc) · 54.7 KB
/
BrachioMailer.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 (c) 2020-2021, D9ping
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied.
*/
/**
* Class for creating a mail/RFC822 message.
*/
class BrachioMailer {
const MIMEVERSION = '1.0';
const MAILLINEMAXLENGTHHEADER = 998;
// Sendmail relay support actually support up till 2040 characters per line.
// http://www.jebriggs.com/blog/2010/07/smtp-maximum-line-lengths/
// We try to limit it to 998 characters excluding enters(\r\n)
// as a MUST in RFC 5322.
const MAILLINEMAXLENGTHBODY = 998;
const CONVMAILMAXLINKS = 32;
const BOUNDARYPREFIX = '--';
const ENDPARTSUFFIX = '--';
const DEFAULTHASHCASHCALCBITS = 20;
private $attachments = array();
private $autosubmitted = 'auto-generated';
private $delay = 0;
private $debugmode = false;
private $messagecharset = 'UTF-8';
private $messagecontenttype = 'text/plain';
private $organization = null;
private $replyto = null;
private $returnpath = null;
private $scheduleFor = false;
private $precedencebulk = false;
private $nopublicarchive = false;
private $nondeliveryreport = false;
private $importance = '';
private $pgpkeyid = null;
private $pgpkeyfingerprint = null;
private $pgpkeygetkeyserverurl = null;
private $pgpSignKeyFp = '';
private $pgpEncryptPubKeyFp = '';
private $sensitivity = null;
private $abuseemail = null;
private $abuseurl = null;
private $includeipsender = true;
private $useencodedip = false;
private $usexpriority = false;
private $eipencryptionkey = '';
private $reportmailer = true;
private $dispositionnotificationto = null;
private $returnreceiptto = null;
private $usehashcash = false;
private $usesmime = false;
private $smimecachefolder = null;
private $smimekeypublic = null;
private $smimekeyprivate = null;
private $smimekeyprivatepassphrase = null;
private $smimeextracerts = '';
/**
* Creating a new instance of BrachioMailer class.
*
* @param bool $debugmode In debug mode the generated mail message will be outputted to the
* client as eml file instead of being send/scheduled.
*/
public function __construct($debugmode = false)
{
$this->debugmode = (bool)$debugmode;
if (!function_exists('quoted_printable_encode')) {
throw new Exception('Please upgrade PHP to at least version 5.3.');
}
if (!defined('CHRENTER')) {
define('CHRENTER', "\r\n");
}
}
/**
* RFC2045, RFC2046, RFC2047, RFC4288, RFC4289 and RFC2049 MIME content type.
*
* @param string $messageContentType The message content-type e.g. this can be: "text/plain" or "text/html"
*/
public function setMessagecontenttype($messageContentType)
{
$this->messagecontenttype = $messageContentType;
}
/**
* The character set of the data part of the message.
*
* @param string $messageCharset The character set of the content of the message.
* E.g. this can be: "7bit", "8bit", "UCS-4" etc.
*/
public function setMessagecharset($messageCharset)
{
$validencoding = false;
$listEncodings = mb_list_encodings();
$numEncodings = count($listEncodings);
if ($numEncodings > 10) {
// Start from "7bit"
for ($i = 11; $i < $numEncodings; ++$i) {
if ($listEncodings[$i] === $messageCharset) {
$validencoding = true;
break;
}
}
}
if (!$validencoding) {
throw new InvalidArgumentException('No valid message charset used.');
return;
}
$this->messagecharset = $messageCharset;
}
/**
* Unofficial. RFC 1036 2.2.8. The organization of the send message.
*
* @param string $organization The organization that sends the message.
*/
public function setOrganization($organization)
{
$this->organization = $organization;
}
/**
* The e-mail address to reply to.
* If this is set (not null or empty string) it will be used otherwise Reply-to: header is not included.
*
* @param string $replyTo The reply to e-mail address to send a reaction to this message to.
*/
public function setReplyto($replyTo)
{
if (!filter_var($replyTo, FILTER_VALIDATE_EMAIL) ||
strpos($replyTo, ' ') !== false ||
strpos($replyTo, "\r") !== false ||
strpos($replyTo, "\n") !== false) {
throw new InvalidArgumentException(sprintf('The %1$s value is not a valid e-mail address.', '$replyTo'));
return;
}
$this->replyto = $replyTo;
}
/**
* The e-mail address to return the e-mail to in case of errors.
* If not set the emailfrom parameter will be used as returnpath/envelope from email address.
*
* @param string $returnPath The bounce\envelope e-mail address.
*/
public function setReturnpath($returnPathEmailaddr)
{
if (!filter_var($returnPathEmailaddr, FILTER_VALIDATE_EMAIL) ||
strpos($returnPathEmailaddr, ' ') !== false ||
strpos($returnPathEmailaddr, "\r") !== false ||
strpos($returnPathEmailaddr, "\n") !== false) {
throw new InvalidArgumentException(sprintf('The %1$s value is not a valid e-mail address.', '$returnPathEmailaddr'));
return;
}
$this->returnpath = $returnPathEmailaddr;
}
/**
* Unofficial. RFC2076, used to mark mass mailing send at once it's use is now unofficial and discouraged.
* Warning: it's not recommeded to use this.
*
* @param bool $precedenceBulk True to use the Precedence: bulk mail header to mark mass mailing(discouraged).
*/
public function setPrecedencebulk($precedenceBulk)
{
$this->precedencebulk = (bool)$precedenceBulk;
}
/**
* Unofficial. Do not archive the message in publicly available archives.
*
* @param bool $noPublicArchive
*/
public function setNopublicarchive($noPublicArchive)
{
$this->nopublicarchive = (bool)$noPublicArchive;
}
/**
* Unofficial. Try preventing receiving Non delivery reports.
* Warning: this header has often no effect at all.
*
* @param bool $nonDeliveryReport True to add the mail headers to prevent Non delivery reports.
*/
public function setNondeliveryreport($nonDeliveryReport)
{
$this->nonondeliveryreport = (bool)$nonDeliveryReport;
}
/**
* RFC2421 section 4.2.14: A hint from the sender how important a message is.
*
* @param string $importance Can be high, normal or low.
*/
public function setImportance($importance)
{
if ($importance !== 'high' &&
$importance !== 'normal' &&
$importance !== 'low') {
throw new InvalidArgumentException(sprintf('Invalid %1$s value.', '$importance'));
return;
}
$this->importance = $importance;
}
/**
* RFC2156: Autosubmitted header.
* If you are sending a 'templated' standard message you should set it at: auto-generated.
*
* @param string $autoSubmitted Can be "not-auto-submitted", "auto-generated", "auto-replied"
* or "auto-forwarded".
*/
public function setAutosubmitted($autoSubmitted)
{
$autoSubmittedLowercase = mb_strtolower($autoSubmitted);
if ($autoSubmittedLowercase !== '' &&
$autoSubmittedLowercase !== 'not-auto-submitted' &&
$autoSubmittedLowercase !== 'auto-generated' &&
$autoSubmittedLowercase !== 'auto-replied' &&
$autoSubmittedLowercase !== 'auto-forwarded') {
throw new InvalidArgumentException(sprintf('Invalid %1$s value.', '$autoSubmitted'));
return;
}
$this->autosubmitted = $autoSubmittedLowercase;
}
/**
* Unofficial. Use the X-Priority header.
* warning the use of this header is not recommeded.
*
* @param bool $useXPriority True to use the X-Priority header to define the priority based on
* a scale from 1(highest) to 5(lowest) of the message.
*/
public function setUseXPriority($useXPriority)
{
$this->usexpriority = (bool)$useXPriority;
}
/**
* Set the OpenPGP short KeyId.
*
* @param string $pgpKeyId The hexidecimal PGP key-Id.
*/
public function setPgpkeyid($pgpKeyId)
{
if (strlen($pgpKeyId) < 8) {
throw new InvalidArgumentException('PGP Key-Id too short.');
}
$this->pgpkeyid = $pgpKeyId;
}
/**
* Set the OpenPGP fingerprint.
*
* @param string $pgpKeyFingerprint The hexidecimal PGP key fingerprint.
*/
public function setPgpkeyfingerprint($pgpKeyFingerprint)
{
if (strlen($pgpKeyFingerprint) < 40) {
throw new InvalidArgumentException('PGP fingerprint too short.');
}
$this->pgpkeyfingerprint = $pgpKeyFingerprint;
}
/**
* Key server to get the OpenPGP public key.
*
* @param string $pgpKeyserverUrl A url to a server to get the PGP key from.
*/
public function setPgpkeygetkeyserverurl($pgpKeyserverUrl)
{
if (!filter_var($pgpKeyserverUrl, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException(sprintf('%1$s is not a valid URL.', '$pgpKeyserverUrl'));
return;
}
$this->pgpkeygetkeyserverurl = $pgpKeyserverUrl;
}
/**
* RFC2421 section 4.2.13 Indicates the requested privacy level.
*
* @param string $sensitivity The sensitivity level of the message.
* This can be: "personal", "private" or "confidential".
*/
public function setSensitivity($sensitivity)
{
if ($sensitivity !== '' &&
$sensitivity !== 'personal' &&
$sensitivity !== 'private' &&
$sensitivity !== 'confidential') {
throw new InvalidArgumentException(sprintf('Invalid %1$s value.', '$sensitivity'));
return;
}
$this->sensitivity = $sensitivity;
}
/**
* unofficial. E-mail address for reporting abuse for this message.
*
* @param string $abuseEmailAddr The e-mail address to report abuse on.
*/
public function setAbuseemail($abuseEmailAddr)
{
if (!filter_var($abuseEmailAddr, FILTER_VALIDATE_EMAIL) ||
strpos($abuseEmailAddr, ' ') !== false ||
strpos($abuseEmailAddr, "\r") !== false ||
strpos($abuseEmailAddr, "\n") !== false) {
throw new InvalidArgumentException(sprintf('The %1$s value is not a valid e-mail address.',
$abuseEmailAddr));
return;
}
$this->abuseemail = $abuseEmailAddr;
}
/**
* unofficial. Information url for reporting abuse for this message.
*
* @param string $abuseUrl A url to report abuse on.
*/
public function setAbuseurl($abuseUrl)
{
if (!filter_var($abuseUrl, FILTER_VALIDATE_URL)) {
if (!empty($abuseUrl)) {
throw new InvalidArgumentException(sprintf('%1$s is not a valid URL.', $abuseUrl));
} else {
throw new InvalidArgumentException(sprintf('No valid URL provided.', $abuseUrl));
}
}
$this->abuseurl = $abuseUrl;
}
/**
* unofficial. Should the ip address of the client/user be included in the message.
*
* @param bool $includeIpSender True to include the ip address of the submitter of the message,
* it will be encrypted if setUseencodedip is set to true.
*/
public function setIncludeipsender($includeIpSender)
{
$this->includeipsender = (bool)$includeIpSender;
}
/**
* unofficial. Include an encrypted the ip address of the requested sender in the header.
* warning: mcrypt php extension needs to be loaded to use it.
*
* @param bool $useEncodedIp If true the X-EIP header will be added and the X-Originating-IP will not be used.
*/
public function setUseencodedip($useEncodedIp)
{
if (!extension_loaded('mcrypt') && !function_exists('openssl_encrypt') && $useEncodedIp) {
error_log('mcrypt php extension not loaded and openssl_encrypt also not available, privacy risk because encrypted X-EIP not used.');
} else {
$this->useencodedip = (bool)$useEncodedIp;
}
}
/**
* The secret encryption key for encrypting the ip address and the ciphertext is included in the
* X-EIP header of the message. It's recommended to use a key that is at least 16 characters
* or longer.
*
* @param string $eipEncryptionKey The secret encryption key used for generating the X-EIP header.
*/
public function setEipencryptionkey($eipEncryptionKey)
{
$this->eipencryptionkey = $eipEncryptionKey;
}
/**
* unofficial. Report the client software used. This is PHP/Major.Minor version numbers.
*
* @param bool $reportMailer True to report the version of PHP used.
*/
public function setReportmailer($reportMailer)
{
$this->reportmailer = (bool)$reportMailer;
}
/**
* Request for the receiving mailclient/MUA to send a Delivery Status Notification/DSN message
* as soon as the person opens the email. Warning a lot of mailclients/MUA's will warn the
* user before sending a Delivery Status Notification(DSN) message. It's also possible
* that always a denied disposition message is send.
*
* @param string $dispositionNotificationEmail The e-mail address that receives the DSN.
*/
public function setDispositionnotificationto($dispositionNotificationEmail)
{
if (!filter_var($dispositionNotificationEmail, FILTER_VALIDATE_EMAIL) ||
strpos($dispositionNotificationEmail, ' ') !== false ||
strpos($dispositionNotificationEmail, "\r") !== false ||
strpos($dispositionNotificationEmail, "\n") !== false) {
throw new InvalidArgumentException(sprintf('The %1$s value is not a valid e-mail address.', $dispositionNotificationEmail));
return;
}
$this->dispositionnotificationto = $dispositionNotificationEmail;
}
/**
* Request for the receiving mail server/MTA to send a DSN (delivery status notification) as soon as it receives the email.
*
* @param string $returnReceiptToEmail The email address that receives the DSN.
*/
public function setReturnreceiptto($returnReceiptToEmail)
{
if (!filter_var($returnReceiptToEmail, FILTER_VALIDATE_EMAIL) ||
strpos($returnReceiptToEmail, ' ') !== false ||
strpos($returnReceiptToEmail, "\r") !== false ||
strpos($returnReceiptToEmail, "\n") !== false) {
throw new InvalidArgumentException(sprintf('The %1$s value is not a valid e-mail address.', $returnReceiptToEmail));
return;
}
$this->returnreceiptto = $returnReceiptToEmail;
}
/**
* Delay in milliseconds before try sending the mail to the MTA.
*
* @param int $delayMilliseconds A positive number of milliseconds to delay the handing off the email to sendmail.
*/
public function setDelay($delayMilliseconds)
{
if ($delayMilliseconds < 0) {
throw new InvalidArgumentException(sprintf('Invalid %1$s value.', '$delayMiliseconds'));
}
$this->delay = (int)$delayMilliseconds;
}
/**
* Use the X-Hashcash proof of work anti-spam header.
* Enabling hashcash will make send a mails very slow as designed.
*
* @param bool $useHashCash True to generate the HashCash header. Requires a lot of CPU use
* and the Hashcash class.
*/
public function setUsehashcash($useHashCash)
{
if ($useHashCash) {
require_once(__DIR__ .'/Hashcash.php');
}
$this->usehashcash = (bool)$useHashCash;
}
/**
* Use the a database table to store scheduled mails to send at a later time.
*
* @param string $dateTime
*/
public function ScheduleMailFor($dateTime)
{
if (empty($dateTime)) {
throw new InvalidArgumentException('Required date and time missing.');
}
if (file_exists(__DIR__ .'/config.php')) {
require_once(__DIR__ .'/config.php');
}
if (file_exists(__DIR__ .'/DB.php')) {
require_once(__DIR__ .'/DB.php');
}
if (is_readable(__DIR__ .'/d_Mailschedule.php')) {
require_once(__DIR__ .'/d_Mailschedule.php');
$this->scheduleFor = $dateTime;
} else {
exit('d_Mailschedule class not readable or missing.');
}
}
/**
* Use S/MIME to (only)sign the message.
* This requires a S/MIME certificate signed by a trusted CA and the private key of the certificate.
*
* @param bool $useSmimeSigning True to sign the message with S/MIME.
*/
public function setUsesmime($useSmimeSigning)
{
$this->usesmime = (bool)$useSmimeSigning;
}
/**
* The S/MIME cache folder to store the signed and unsigned messages.
*
* @param string $smimeCacheFolder A path to a not public folder.
*/
public function setSmimecachefolder($smimeCacheFolder)
{
if (!is_dir($smimeCacheFolder)) {
throw new Exception(sprintf('%1$s value is not a folder.', '$smimeCacheFolder'));
}
$this->smimecachefolder = $smimeCacheFolder;
}
/**
* Set the S/MIME public certificate.
*
* @param string $smimePublicKey The file path to the public certificate.
*/
public function setSmimekeypublic($smimePublicKey)
{
if (!is_file($smimePublicKey)) {
throw new Exception(sprintf('The %1$s value is not valid file path.', $smimePublicKey));
}
$this->smimekeypublic = $smimePublicKey;
}
/**
* Set the S/MIME private key for the S/MIME certificate.
*
* @param string $smimePrivateKey The file path to the (possible encypted)certificate.
*/
public function setSmimekeyprivate($smimePrivateKey)
{
if (!is_file($smimePrivateKey)) {
throw new Exception(sprintf('The %1$s value is not a file.', '$smimePrivateKey'));
}
$this->smimekeyprivate = $smimePrivateKey;
}
/**
* Set the passphrase for the private key for the S/MIME certificate.
*
* @param string $smimekeyprivatepassphrase The secret passphrase to decrypt the private certificate.
*/
public function setSmimekeyprivatepassphrase($smimekeyprivatepassphrase)
{
$this->smimekeyprivatepassphrase = $smimekeyprivatepassphrase;
}
/**
* Set the intermediate CA Certificates.
*/
public function setSmimeextracerts($smimeExtraCerts)
{
if (!is_file($smimeExtraCerts)) {
throw new Exception(sprintf('The %1$s value is not a file.', '$smimeExtraCerts'));
}
$this->smimeextracerts = $smimeExtraCerts;
}
/**
* Override the magic __debugInfo method (new in PHP 5.6.0) because
* if the method isn't defined on an object, then ALL public, protected and private properties could be shown.
*/
public function __debugInfo()
{
return array('error' => '__debugInfo disabled.');
}
/**
* Override the magic __toString method.
*/
public function __toString()
{
return '__toString disabled.';
}
/**
* Sends an e-mail
*
* @param string $emailto An valid e-mail address (required).
* @param string $emailfrom An valid e-mail address (required).
* @param string $subject Subject of the email (required).
* @param string $body The message body of the email, by default text/plain MIME. (required).
* @param string $nameto The full name of the receiver. (optional)
* @param string $namefrom The full name of the sender. (optional)
* @return bool True if mail is send or added in queue for sending later succesfully.
*/
public function Send($emailto, $emailfrom, $subject, $body, $nameto = '', $namefrom = '')
{
if (empty($emailto)) {
return false;
}
if (empty($emailfrom)) {
return false;
}
if (empty($subject)) {
return false;
}
// For the message to be stored in the database for scheduling the email
// the to and from address and the subject is limited to 255 characters.
// Excluding the header name e.g. 'Subject: '/'From:' so we limit these
// provided fields to 200 characters.
if (strlen($emailto) > 200) {
error_log('To may not be more than 200 characters.');
return false;
}
if (strlen($emailfrom) > 200) {
error_log('From may not be more than 200 characters.');
return false;
}
if (strlen($subject) > 200) {
error_log('Subject may not be more than 200 characters.');
return false;
}
if (empty($body)) {
return false;
}
if (!filter_var($emailfrom, FILTER_VALIDATE_EMAIL) ||
strpos($emailfrom, ' ') !== false ||
strpos($emailfrom, "\r") !== false ||
strpos($emailfrom, "\n") !== false) {
return false;
}
if (empty($this->returnpath)) {
$this->returnpath = $emailfrom;
}
if (empty($this->messagecharset)) {
$this->messagecharset = 'UTF-8';
}
$headers = '';
$this->addHeaderLine('Return-Path', $this->returnpath, $headers);
if (empty($namefrom) || !$this->isValidName($namefrom)) {
$this->addHeaderLine('From', $emailfrom, $headers);
} else {
if (preg_match("/^[a-zA-Z0-9\s\.\-\'\\\\,\/]+$/", $namefrom)) {
// Only allow: a-z, A-Z, 0-9, space, dot, comma, dash, single quote, slash and backslash.
$this->addHeaderLine('From', $namefrom.' <'.$emailfrom.'>', $headers);
} else {
mb_internal_encoding('UTF-8');
$encFromName = mb_encode_mimeheader($namefrom, 'UTF-8', 'Q').' <'.$emailfrom.'>';
$this->addHeaderLine('From', $encFromName, $headers);
}
}
if (!empty($this->replyto)) {
$this->addHeaderLine('Reply-To', $this->replyto, $headers);
}
if (!empty($this->dispositionnotificationto)) {
$this->addHeaderLine('Disposition-Notification-To', $this->dispositionnotificationto, $headers);
}
if (!empty($this->returnreceiptto)) {
$this->addHeaderLine('Return-Receipt-To', $this->returnreceiptto, $headers);
}
if (!empty(self::MIMEVERSION) && ctype_digit((string) self::MIMEVERSION)) {
$headers .= 'MIME-Version: '.self::MIMEVERSION.CHRENTER;
}
if (!empty($this->autosubmitted)) {
$this->addHeaderLine('Auto-Submitted', $this->autosubmitted, $headers);
}
if ($this->precedencebulk) {
$this->addHeaderLine('Precedence', 'bulk', $headers);
}
if ($this->nondeliveryreport) {
$this->addHeaderLine('Prevent-NonDelivery-Report', 'true', $headers);
}
if (!empty($this->pgpkeygetkeyserverurl) && !empty($this->pgpkeyid) && !empty($this->pgpkeyfingerprint)) {
$this->addHeaderLine('OpenPGP', 'url="'.$this->pgpkeygetkeyserverurl.'"; id='.$this->pgpkeyid.';', $headers);
$this->addHeaderLine('X-PGP-Key', 'fp="'.$this->pgpkeyfingerprint.'"; id="'.$this->pgpkeyid.'"; get=<'.$this->pgpkeygetkeyserverurl.'>;', $headers);
} elseif (!empty($this->pgpkeygetkeyserverurl) && !empty($this->pgpkeyid)) {
$this->addHeaderLine('OpenPGP', 'url='.$this->pgpkeygetkeyserverurl.'; id='.$this->pgpkeyid.';', $headers);
$this->addHeaderLine('X-PGP-Key', 'fp="'.$this->pgpkeyfingerprint.'"; id="'.$this->pgpkeyid.'";', $headers);
} elseif (!empty($this->pgpkeygetkeyserverurl) && empty($this->pgpkeyid)) {
$this->addHeaderLine('OpenPGP', 'url='.$this->pgpkeygetkeyserverurl.';', $headers);
} elseif (empty($this->pgpkeygetkeyserverurl) && !empty($this->pgpkeyid) && !empty($this->pgpkeyfingerprint)) {
$this->addHeaderLine('OpenPGP', 'id='.$this->pgpkeyid.';', $headers);
$this->addHeaderLine('X-PGP-Key', 'fp="'.$this->pgpkeyfingerprint.'"; id="'.$this->pgpkeyid.'";', $headers);
} elseif (empty($this->pgpkeygetkeyserverurl) && !empty($this->pgpkeyid)) {
$this->addHeaderLine('OpenPGP', 'id='.$this->pgpkeyid.';', $headers);
}
if (!empty($this->sensitivity)) {
$this->addHeaderLine('Sensitivity', $this->sensitivity, $headers);
}
if (!empty($this->organization)) {
$this->addHeaderLine('Organization', $this->organization, $headers, 255);
}
if (!empty($this->reportmailer)) {
// We don't report the release version number or the extra section in the full php version string.
$this->addHeaderLine('X-Mailer', 'PHP/'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION, $headers);
}
if ($this->nopublicarchive) {
$this->addHeaderLine('X-No-Archive', 'Yes', $headers);
}
if (!empty($this->importance)) {
$this->addHeaderLine('Importance', $this->importance, $headers);
if ($this->usexpriority) {
// Set the unofficial x-priority based on the set importance headers.
// Possible X-Priority values are: 1 for highest, 2 for high/above-normal, 3 for normal, 4 for low/below-normal or 5 for lowest.
$xpriority = 3;
switch ($this->importance) {
case 'high':
$xpriority = 1;
break;
case 'low':
$xpriority = 5;
break;
}
$this->addHeaderLine('X-Priority', $xpriority, $headers);
}
}
if (!empty($this->abuseurl)) {
if (filter_var($this->abuseurl, FILTER_VALIDATE_URL)) {
$this->addHeaderLine('X-Abuse-Info', $this->abuseurl, $headers);
}
}
if (!empty($this->abuseemail)) {
if (filter_var($this->abuseemail, FILTER_VALIDATE_EMAIL) &&
strpos($this->abuseemail, ' ') === false &&
strpos($this->abuseemail, "\r") === false &&
strpos($this->abuseemail, "\n") === false) {
$this->addHeaderLine('X-Report-Abuse-To', $this->abuseemail, $headers, 255);
//$this->addHeaderLine('Abuse-Reports-To', $this->abuseemail, $headers, 255);
//$this->addHeaderLine('X-Notice', $this->abuseemail, $headers, 255);
}
}
if ($this->includeipsender) {
$ipaddr = '';
if (isset($_SERVER['REMOTE_ADDR'])) {
$ipaddr = $_SERVER['REMOTE_ADDR'];
}
if (filter_var($ipaddr, FILTER_VALIDATE_IP)) {
if ($this->useencodedip && !empty($this->eipencryptionkey) &&
(extension_loaded('mcrypt') || function_exists('openssl_encrypt')) ) {
$encodedip = '';
if (function_exists('openssl_encrypt')) {
// use openssl
$openssl_cipher_str = 'aes-128-cfb'; // cipher should be in lowercase.
if (!in_array($openssl_cipher_str, openssl_get_cipher_methods())) {
error_log('Error '.$openssl_cipher_str.' not supported by openssl!');
}
$sizeiv = openssl_cipher_iv_length($openssl_cipher_str);
$strongiv = true;
$iv = openssl_random_pseudo_bytes($sizeiv, $strongiv);
if (!$strongiv) {
usleep(100000); // Try again in 100 ms.
$iv = openssl_random_pseudo_bytes($this->ivsize, $strongiv);
if (!$strongiv) {
error_log('IV could not be generated on strong random data.');
}
}
$rawencodedip = openssl_encrypt($ipaddr, $openssl_cipher_str, $this->eipencryptionkey, OPENSSL_ZERO_PADDING, $iv);
$encodedip = base64_encode($iv).'/'.base64_encode($rawencodedip);
} else {
// use mcrypt
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CFB, '');
// Using cipher feedback(CFB) mode, best mode for encrypting byte streams where single bytes must be encrypted.
$sizeiv = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CFB);
// Use MCRYPT_DEV_RANDOM the blocking slow randomness generator.
$iv = mcrypt_create_iv($sizeiv, MCRYPT_DEV_RANDOM);
mcrypt_generic_init($cipher, $this->eipencryptionkey, $iv);
$rawencodedip = mcrypt_generic($cipher, $ipaddr);
$encodedip = base64_encode($iv).'/'.base64_encode($rawencodedip);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
}
// Include encrypted ip address in header.
$this->addHeaderLine('X-EIP', $encodedip, $headers, self::MAILLINEMAXLENGTHHEADER);
} else {
// Set unencrypted ip address in header.
// The maximum length of a hexdec IPv6 address with IPv4 tunneling feature, is 45 characters.
$this->addHeaderLine('X-Originating-IP', '['.$ipaddr.']', $headers, 65); // 'X-Originating-IP: []'=> 20 characters
}
}
}
$orgmessage = $body;
$body = '';
$numAttachments = count($this->attachments);
if ($numAttachments >= 1) {
// Has attachments
//$this->addHeaderLine('X-MS-Has-Attach', 'Yes', $headers);
$multipartmixed = $this->GenerateBoundary('');
if ($this->usesmime) {
// S/mime signing will add "Content-Type: multipart/signed" to headers already
// so Content-Type needs to be added to mail body instead.
$body .= 'Content-Type: multipart/mixed;'.CHRENTER;
$body .= "\t".'boundary="'.$multipartmixed.'"'.CHRENTER; // line folding
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
} else {
$headers .= 'Content-Type: multipart/mixed;'.CHRENTER;
$headers .= "\t".'boundary="'.$multipartmixed.'"'.CHRENTER; // line folding
}
$body .= self::BOUNDARYPREFIX.$multipartmixed.CHRENTER;
if ($this->messagecontenttype === 'text/html') {
$multipartalternative = $this->GenerateBoundary($multipartmixed);
$body .= 'Content-Type: multipart/alternative;'.CHRENTER;
$body .= "\t".'boundary="'.$multipartalternative.'"'.CHRENTER; // line folding
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
$body .= self::BOUNDARYPREFIX.$multipartalternative.CHRENTER;
}
// add plaintext content part
$this->addHeaderLine('Content-Type', 'text/plain; charset='.$this->messagecharset, $body, self::MAILLINEMAXLENGTHBODY);
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
if ($this->messagecontenttype === 'text/html') {
// add text/plain fallback from text/html part
$body .= quoted_printable_encode($this->ConvertHtmlToText($orgmessage)).CHRENTER;
// add text/html part
$body .= self::BOUNDARYPREFIX.$multipartalternative.CHRENTER;
$this->addHeaderLine('Content-Type', $this->messagecontenttype.'; charset='.$this->messagecharset, $body, self::MAILLINEMAXLENGTHBODY);
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
$body .= quoted_printable_encode($orgmessage).CHRENTER;
} else {
// add text/plain part
$body .= quoted_printable_encode($orgmessage).CHRENTER;
}
$body .= CHRENTER;
// Add attachments
$attachments = $this->attachments;
foreach ($attachments as $attachmentname => $attachment) {
$binaryFileContent = file_get_contents($attachment['file']);
if ($binaryFileContent === false) {
error_log(sprint('Could not read %s.', $attachment['file']));
continue;
}
$body .= self::BOUNDARYPREFIX.$multipartmixed.CHRENTER;
$encodedAttachmentname = $attachmentname;
if (!preg_match("/^[a-zA-Z0-9\s\.\-\'\\\\,\/]+$/", $attachmentname)) {
$encodedAttachmentname = mb_encode_mimeheader($attachmentname, 'UTF-8', 'Q');
}
$this->addHeaderLine('Content-Type',
$attachment['mime'].'; name="'.$encodedAttachmentname.'"',
$body,
self::MAILLINEMAXLENGTHBODY);
// make file description shorter.
$headerKeyMime = 'Content-Type';
if (strlen($attachment['description']) > 253 - strlen($headerKeyMime)) {
$this->addHeaderLine($headerKeyMime, substr($attachment['description'], 0, 253 - strlen($headerKeyMime)), $body, self::MAILLINEMAXLENGTHBODY);
} else {
$this->addHeaderLine('Content-Description', $attachment['description'], $body, self::MAILLINEMAXLENGTHBODY);
}
$body .= 'Content-Transfer-Encoding: base64'.CHRENTER;
$body .= 'Content-Disposition: attachment;'.CHRENTER;
$body .= "\t".'filename="'.$encodedAttachmentname.'"; size='.$attachment['size'].';'.CHRENTER; // line folding
$body .= CHRENTER;
$body .= chunk_split(base64_encode($binaryFileContent));
}
$body .= self::BOUNDARYPREFIX.$multipartmixed.self::ENDPARTSUFFIX.CHRENTER;
} else {
// No attachments.
//$this->addHeaderLine('X-MS-Has-Attach', 'No', $headers);
$multipartalternative = '';
if ($this->messagecontenttype === 'text/html') {
$multipartalternative = $this->GenerateBoundary('');
}
if ($this->usesmime) {
// S/mime signing will add "Content-Type: multipart/signed" to headers already
// so Content-Type needs to be added to mail body instead.
if ($this->messagecontenttype === 'text/html') {
// Create fallback part so use multipart/alternative.
//$body .= CHRENTER;
//$body .= self::BOUNDARYPREFIX.$multipartalternative.CHRENTER;
$body .= 'Content-Type: multipart/alternative;'.CHRENTER;
$body .= "\t".'boundary="'.$multipartalternative.'"'.CHRENTER; // line folding
$body .= CHRENTER;
} else {
$this->addHeaderLine('Content-Type', $this->messagecontenttype.'; charset='.$this->messagecharset, $body, self::MAILLINEMAXLENGTHHEADER);
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
}
} else {
if ($this->messagecontenttype === 'text/html') {
// Create fallback so use multipart/alternative in header.
$headers .= 'Content-Type: multipart/alternative;'.CHRENTER;
$headers .= "\t".'boundary="'.$multipartalternative.'"'.CHRENTER; // line folding
} else {
$this->addHeaderLine('Content-Type', $this->messagecontenttype.'; charset='.$this->messagecharset, $headers, self::MAILLINEMAXLENGTHHEADER);
}
}
if ($this->messagecontenttype === 'text/html') {
// add text/plain fallback
$body .= self::BOUNDARYPREFIX.$multipartalternative.CHRENTER;
$this->addHeaderLine('Content-Type', 'text/plain; charset='.$this->messagecharset, $body, self::MAILLINEMAXLENGTHBODY);
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
$body .= quoted_printable_encode($this->ConvertHtmlToText($orgmessage)).CHRENTER;
// add text/html part
$body .= self::BOUNDARYPREFIX.$multipartalternative.CHRENTER;
$this->addHeaderLine('Content-Type', $this->messagecontenttype.'; charset='.$this->messagecharset, $body, self::MAILLINEMAXLENGTHBODY);
$body .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
$body .= CHRENTER;
}
$body .= quoted_printable_encode($orgmessage).CHRENTER;
}
$headers .= 'Content-Transfer-Encoding: quoted-printable'.CHRENTER;
if (!empty($this->returnpath)) {
if (strpos($this->returnpath, ':') !== false ||
strpos($this->returnpath, "\r") !== false ||
strpos($this->returnpath, "\n") !== false) {
return;
}
// The user that the webserver runs as should be added as a trusted user to the sendmail
// configuration to prevent a 'X-Warning' header from being added to the message when
// the envelope sender (-f) is set using mail.
$arguments = '-f '.escapeshellarg($this->returnpath).' ';
}
if ($this->usehashcash) {
$hashcash = new Hashcash(self::DEFAULTHASHCASHCALCBITS, $emailto);
try {
$this->addHeaderLine('X-Hashcash', $hashcash->mint(), $headers);
} catch (Exception $hashcashExc) {
error_log('Generation hashcash header error: '.$hashcashExc->getMessage());
}
}