1
+ <?php
2
+ namespace Ddrv \Mailer ;
3
+ /**
4
+ * PHP Class for sending email.
5
+ *
6
+ * PHP version 5.3
7
+ *
8
+ * @category Ddrv
9
+ * @package Mailer
10
+ * @author Ivan Dudarev (https://github.com/ddrv)
11
+ * @license MIT
12
+ * @link http://ddrv.ru/
13
+ *
14
+ * @property string $sender
15
+ * @property string $charset
16
+ * @property array $headers
17
+ */
18
+
19
+ class Mailer {
20
+ const MAILER_VERSION = '1.0.0 ' ;
21
+
22
+ /**
23
+ * send from this Email
24
+ *
25
+ * @var string
26
+ */
27
+ public $ sender ;
28
+
29
+ /**
30
+ * headers of mail
31
+ *
32
+ * @var array
33
+ */
34
+ public $ headers ;
35
+
36
+ /**
37
+ * charset of message
38
+ *
39
+ * @var array
40
+ */
41
+ public $ charset = 'utf8 ' ;
42
+
43
+ /**
44
+ * Consructor
45
+ * @param array $sets
46
+ */
47
+ public function __construct ($ sets =array ()) {
48
+ if ($ sets ) {
49
+ foreach ($ sets as $ property => $ value ) {
50
+ if (in_array ($ property , array ('sender ' ,'headers ' )) ) {
51
+ $ this ->$ property = $ value ;
52
+ }
53
+ }
54
+ $ this ->init ();
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Initialization this component and first record
60
+ * This method requred for Yii Framevork
61
+ */
62
+ public function init () {
63
+ if (empty ($ this ->charset )) $ this ->charset = 'utf8 ' ;
64
+ if (empty ($ this ->sender )) $ this ->sender = 'sender@localhost ' ;
65
+ }
66
+
67
+ /**
68
+ * Send email to address
69
+ *
70
+ * @param string $address
71
+ * @param string $subject
72
+ * @param string $message
73
+ * @param array $attachments
74
+ */
75
+ public function send ($ address ,$ subject ,$ message ,$ attachments =array ()) {
76
+ $ rawAttachments = empty ($ attachments )?array ():$ this ->getRawAttachment ($ attachments );
77
+ $ noModifiedHeaders = $ this ->headers ;
78
+ $ this ->setHeader ('From ' , $ this ->sender , false );
79
+ $ this ->setHeader ('Reply-To ' , $ this ->sender , false );
80
+ $ this ->setHeader ('MIME-Version ' ,'1.0 ' , false );
81
+ $ this ->setHeader ('X-Mailer ' , 'ddrvMailer- ' .self ::MAILER_VERSION .' (https://github.com/ddrv/mailer) ' , false );
82
+ $ body = empty ($ rawAttachments )?$ this ->getBodySimpleText ($ message ):$ this ->getBodyMultipart ($ message , $ rawAttachments );
83
+ $ headers = implode ("\r\n" ,$ this ->headers );
84
+ mail ($ address , $ subject , $ body , $ headers );
85
+ $ this ->headers = $ noModifiedHeaders ;
86
+ }
87
+
88
+ protected function getRawAttachment ($ attachments =array ()) {
89
+ $ rawAttachments = array ();
90
+ foreach ((array )$ attachments as $ attachment ) {
91
+ if (
92
+ !empty ($ attachment ['content ' ])
93
+ && !empty ($ attachment ['name ' ])
94
+ && !empty ($ attachment ['type ' ])
95
+ && in_array ($ attachment ['type ' ], array ('file ' ,'string ' ))
96
+ ) {
97
+ $ name = preg_replace ('/[ \\\\\/\:\*\?\"<>]/ui ' , '_ ' , $ attachment ['name ' ]);
98
+ $ content = null ;
99
+ switch ($ attachment ['type ' ]) {
100
+ case 'file ' :
101
+ $ content = (file_exists ($ attachment ['content ' ]))?chunk_split (base64_encode (file_get_contents ($ attachment ['content ' ]))):null ;
102
+ break ;
103
+ case 'string ' :
104
+ $ content = chunk_split (base64_encode ($ attachment ['content ' ]));
105
+ break ;
106
+ }
107
+ if ($ name && $ content ) {
108
+ $ rawAttachments [$ name ] = $ content ;
109
+ }
110
+ }
111
+ }
112
+ return $ rawAttachments ;
113
+ }
114
+
115
+ protected function getBodySimpleText ($ message ) {
116
+ $ this ->setHeader ('Content-type ' ,'text/html; charset= ' .$ this ->charset , true );
117
+ return $ message ;
118
+ }
119
+
120
+ protected function getBodyMultipart ($ message , $ attachments ) {
121
+ $ separator = md5 (time ());
122
+ $ eol = "\r\n" ;
123
+ $ this ->setHeader ('Content-Type ' , 'multipart/mixed; boundary=" ' .$ separator .'" ' , true );
124
+ $ this ->setHeader ('Content-Transfer-Encoding ' , '7bit ' , true );
125
+
126
+ $ body [] = null ;
127
+ $ b = $ eol .'Content-type: text/html; charset= ' .$ this ->charset .$ eol ;
128
+ $ bits = '7bit ' ;
129
+ switch ($ this ->charset ) {
130
+ case 'utf-8 ' :
131
+ case 'utf8 ' : $ bits ='8bit ' ; break ;
132
+ }
133
+ $ b .= 'Content-Transfer-Encoding: ' .$ bits .$ eol ;
134
+ $ b .= $ eol .$ message .$ eol ;
135
+ $ body [] = $ b ;
136
+ foreach ($ attachments as $ file =>$ content ) {
137
+ $ b = $ eol .'Content-Type: application/octet-stream; name=" ' . $ file . '" ' . $ eol ;
138
+ $ b .= 'Content-Transfer-Encoding: base64 ' . $ eol ;
139
+ $ b .= 'Content-Disposition: attachment ' . $ eol ;
140
+ $ b .= $ eol .$ content .$ eol ;
141
+ $ body [] = $ b ;
142
+ }
143
+ $ body [] = '-- ' ;
144
+ return implode ('-- ' .$ separator ,$ body );
145
+ }
146
+
147
+ public function setHeaderFrom ($ address ) {
148
+ $ this ->setHeader ('From ' ,$ address );
149
+ }
150
+
151
+ protected function setHeader ($ header , $ value =null , $ replace =true ) {
152
+ if (!$ replace && !empty ($ this ->headers [mb_strtolower ($ header )])) return ;
153
+ if (($ value === null || $ value === false ) && !empty ($ this ->headers [mb_strtolower ($ header )])) {
154
+ unset($ this ->headers [mb_strtolower ($ header )]);
155
+ } else {
156
+ $ this ->headers [mb_strtolower ($ header )] = $ header . ': ' . $ value ;
157
+ }
158
+ }
159
+ }
0 commit comments