@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | namespace tinymeng\mailer\Gateways; |
8 | 8 | use tinymeng\mailer\Connector\Gateway; |
9 | 9 | |
10 | -class Smtp extends Gateway{ |
|
10 | +class Smtp extends Gateway { |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * @var |
@@ -30,8 +30,8 @@ discard block |
||
30 | 30 | { |
31 | 31 | $mail_from = $this->getAddress($this->stripComment($this->from_address)); |
32 | 32 | $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body); |
33 | - $this->boundary = "====" . md5(uniqid()) . "===="; |
|
34 | - $body = $this->prepareBody($body,$mailType); |
|
33 | + $this->boundary = "====".md5(uniqid())."===="; |
|
34 | + $body = $this->prepareBody($body, $mailType); |
|
35 | 35 | $header = $this->buildHeaders($subject, $mailType, $mail_from); |
36 | 36 | |
37 | 37 | // Prepare recipient list |
@@ -41,14 +41,14 @@ discard block |
||
41 | 41 | foreach ($TO as $rcpt_to) { |
42 | 42 | $rcpt_to = $this->getAddress($rcpt_to); |
43 | 43 | if (!$this->smtpSockopen($rcpt_to)) { |
44 | - $this->logWrite("Error: Cannot send email to " . $rcpt_to . "\n"); |
|
44 | + $this->logWrite("Error: Cannot send email to ".$rcpt_to."\n"); |
|
45 | 45 | $sent = false; |
46 | 46 | continue; |
47 | 47 | } |
48 | 48 | if ($this->smtpSend($this->host_name, $mail_from, $rcpt_to, $header, $body)) { |
49 | - $this->logWrite("E-mail has been sent to <" . $rcpt_to . ">\n"); |
|
49 | + $this->logWrite("E-mail has been sent to <".$rcpt_to.">\n"); |
|
50 | 50 | } else { |
51 | - $this->logWrite("Error: Cannot send email to <" . $rcpt_to . ">\n"); |
|
51 | + $this->logWrite("Error: Cannot send email to <".$rcpt_to.">\n"); |
|
52 | 52 | $sent = false; |
53 | 53 | } |
54 | 54 | fclose($this->sock); |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @return string |
65 | 65 | * @author Tinymeng <[email protected]> |
66 | 66 | */ |
67 | - private function buildHeaders($subject, $mailType, $mail_from){ |
|
67 | + private function buildHeaders($subject, $mailType, $mail_from) { |
|
68 | 68 | // Set headers |
69 | 69 | $header = "MIME-Version: 1.0\r\n"; |
70 | 70 | if (strtoupper($mailType) == "HTML") { |
@@ -72,20 +72,20 @@ discard block |
||
72 | 72 | } else { |
73 | 73 | $header .= "Content-Type: text/plain; charset=UTF-8\r\n"; |
74 | 74 | } |
75 | - $header .= "To: " . $this->to_address . "\r\n"; |
|
75 | + $header .= "To: ".$this->to_address."\r\n"; |
|
76 | 76 | if (!empty($this->cc_address)) { |
77 | - $header .= "Cc: " . $this->cc_address . "\r\n"; |
|
77 | + $header .= "Cc: ".$this->cc_address."\r\n"; |
|
78 | 78 | } |
79 | - $header .= "From: $this->from_address <" . $this->from_address . ">\r\n"; |
|
80 | - $header .= "Subject: " . $subject . "\r\n"; |
|
81 | - if(!empty($this->headers)){ |
|
82 | - foreach ($this->headers as $head){ |
|
79 | + $header .= "From: $this->from_address <".$this->from_address.">\r\n"; |
|
80 | + $header .= "Subject: ".$subject."\r\n"; |
|
81 | + if (!empty($this->headers)) { |
|
82 | + foreach ($this->headers as $head) { |
|
83 | 83 | $header .= $head; |
84 | 84 | } |
85 | 85 | } |
86 | - $header .= "Date: " . date("r") . "\r\n"; |
|
87 | - $header .= "X-Mailer: By Redhat (PHP/" . phpversion() . ")\r\n"; |
|
88 | - $header .= "Message-ID: <" . date("YmdHis") . "." . uniqid() . "@" . $mail_from . ">\r\n"; |
|
86 | + $header .= "Date: ".date("r")."\r\n"; |
|
87 | + $header .= "X-Mailer: By Redhat (PHP/".phpversion().")\r\n"; |
|
88 | + $header .= "Message-ID: <".date("YmdHis").".".uniqid()."@".$mail_from.">\r\n"; |
|
89 | 89 | return $header; |
90 | 90 | } |
91 | 91 | |
@@ -96,22 +96,22 @@ discard block |
||
96 | 96 | * @return string |
97 | 97 | * @author Tinymeng <[email protected]> |
98 | 98 | */ |
99 | - private function prepareBody($body, $mailType){ |
|
99 | + private function prepareBody($body, $mailType) { |
|
100 | 100 | if (strtoupper($mailType) == "HTML") { |
101 | 101 | // Body with text and attachments |
102 | - $body = "--$this->boundary\r\n" . |
|
103 | - "Content-Type: text/html; charset=UTF-8\r\n" . |
|
104 | - "Content-Transfer-Encoding: 7bit\r\n\r\n" . |
|
105 | - $body . "\r\n"; |
|
102 | + $body = "--$this->boundary\r\n". |
|
103 | + "Content-Type: text/html; charset=UTF-8\r\n". |
|
104 | + "Content-Transfer-Encoding: 7bit\r\n\r\n". |
|
105 | + $body."\r\n"; |
|
106 | 106 | |
107 | - if(!empty($this->attachments)){ |
|
107 | + if (!empty($this->attachments)) { |
|
108 | 108 | foreach ($this->attachments as $fileName=>$attachment) { |
109 | 109 | $fileContent = chunk_split(base64_encode(file_get_contents($attachment))); |
110 | - $body .= "--$this->boundary\r\n" . |
|
111 | - "Content-Type: application/octet-stream; name=\"$fileName\"\r\n" . |
|
112 | - "Content-Transfer-Encoding: base64\r\n" . |
|
113 | - "Content-Disposition: attachment; filename=\"$fileName\"\r\n\r\n" . |
|
114 | - $fileContent . "\r\n"; |
|
110 | + $body .= "--$this->boundary\r\n". |
|
111 | + "Content-Type: application/octet-stream; name=\"$fileName\"\r\n". |
|
112 | + "Content-Transfer-Encoding: base64\r\n". |
|
113 | + "Content-Disposition: attachment; filename=\"$fileName\"\r\n\r\n". |
|
114 | + $fileContent."\r\n"; |
|
115 | 115 | } |
116 | 116 | $this->attachments = array(); |
117 | 117 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | return $this->smtpError("sending HELO command"); |
157 | 157 | } |
158 | 158 | //auth |
159 | - if($this->auth){ |
|
159 | + if ($this->auth) { |
|
160 | 160 | if (!$this->smtpPutCmd("AUTH LOGIN", base64_encode($this->username))) { |
161 | 161 | return $this->smtpError("sending HELO command"); |
162 | 162 | } |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | private function smtpPutCmd($cmd, $arg = "") |
308 | 308 | { |
309 | 309 | if ($arg != "") { |
310 | - if($cmd=="") $cmd = $arg; |
|
310 | + if ($cmd == "") $cmd = $arg; |
|
311 | 311 | else $cmd = $cmd." ".$arg; |
312 | 312 | } |
313 | 313 | fputs($this->sock, $cmd."\r\n"); |
@@ -17,20 +17,20 @@ discard block |
||
17 | 17 | * @author Tinymeng <[email protected]> |
18 | 18 | * @date: 2019/9/26 15:21 |
19 | 19 | */ |
20 | - protected $host; //发送email Host |
|
21 | - protected $port; //端口 25 or 456 |
|
22 | - protected $encryption; //加密 ssl |
|
23 | - protected $username; //邮箱用户名(发送人email) |
|
24 | - protected $password; //邮箱密码(如果是第三方请去设置里获取) |
|
25 | - protected $from_address;//发送人email |
|
26 | - |
|
27 | - protected $to_address; //接收人email |
|
28 | - protected $cc_address; //抄送人email |
|
29 | - protected $bcc_address; //隐性抄送人email |
|
30 | - |
|
31 | - protected $log_file = false;//记录日志 |
|
20 | + protected $host; //发送email Host |
|
21 | + protected $port; //端口 25 or 456 |
|
22 | + protected $encryption; //加密 ssl |
|
23 | + protected $username; //邮箱用户名(发送人email) |
|
24 | + protected $password; //邮箱密码(如果是第三方请去设置里获取) |
|
25 | + protected $from_address; //发送人email |
|
26 | + |
|
27 | + protected $to_address; //接收人email |
|
28 | + protected $cc_address; //抄送人email |
|
29 | + protected $bcc_address; //隐性抄送人email |
|
30 | + |
|
31 | + protected $log_file = false; //记录日志 |
|
32 | 32 | protected $host_name = "localhost"; //is used in HELO command |
33 | - protected $time_out = 30;//is used in fsockopen() |
|
33 | + protected $time_out = 30; //is used in fsockopen() |
|
34 | 34 | |
35 | 35 | /** |
36 | 36 | * 是否开启debug |
@@ -77,16 +77,16 @@ discard block |
||
77 | 77 | 'from_address'=> '', |
78 | 78 | 'to_address'=> '', |
79 | 79 | ]; |
80 | - $this->config = array_replace_recursive($_config,$config); |
|
81 | - if(empty($this->config['from_address'])){ |
|
80 | + $this->config = array_replace_recursive($_config, $config); |
|
81 | + if (empty($this->config['from_address'])) { |
|
82 | 82 | $this->config['from_address'] = $this->config['username']; |
83 | 83 | } |
84 | - if(!empty($this->config['encryption'])){ |
|
85 | - $this->config['host'] = $this->config['encryption'] . '://' . $this->config['host']; |
|
84 | + if (!empty($this->config['encryption'])) { |
|
85 | + $this->config['host'] = $this->config['encryption'].'://'.$this->config['host']; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | foreach ($this->config as $key => $value) { |
89 | - if (property_exists($this,$key)) { |
|
89 | + if (property_exists($this, $key)) { |
|
90 | 90 | $this->$key = $value; |
91 | 91 | } |
92 | 92 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * @author Tinymeng <[email protected]> |
120 | 120 | * @date: 2019/9/26 10:44 |
121 | 121 | */ |
122 | - public function setDebug($debug){ |
|
122 | + public function setDebug($debug) { |
|
123 | 123 | $this->debug = $debug; |
124 | 124 | } |
125 | 125 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * @author Tinymeng <[email protected]> |
131 | 131 | * @date: 2019/9/26 15:20 |
132 | 132 | */ |
133 | - public function toEmail($email){ |
|
133 | + public function toEmail($email) { |
|
134 | 134 | $this->to_address = $email; |
135 | 135 | return $this; |
136 | 136 | } |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | * @author Tinymeng <[email protected]> |
143 | 143 | * @date: 2019/9/26 15:20 |
144 | 144 | */ |
145 | - public function ccEmail($email){ |
|
145 | + public function ccEmail($email) { |
|
146 | 146 | $this->cc_address = $email; |
147 | 147 | return $this; |
148 | 148 | } |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | * @author Tinymeng <[email protected]> |
155 | 155 | * @date: 2019/9/26 15:20 |
156 | 156 | */ |
157 | - public function bccEmail($email){ |
|
157 | + public function bccEmail($email) { |
|
158 | 158 | $this->bcc_address = $email; |
159 | 159 | return $this; |
160 | 160 | } |
@@ -164,13 +164,13 @@ discard block |
||
164 | 164 | * @param array|string $attachments |
165 | 165 | * @return $this |
166 | 166 | */ |
167 | - public function addAttachments($attachments){ |
|
167 | + public function addAttachments($attachments) { |
|
168 | 168 | $attachments = is_array($attachments) ? $attachments : [$attachments]; |
169 | - foreach ($attachments as $fileName=>$attachment){ |
|
170 | - if(file_exists($attachment) || $this->isURL($attachment)) { |
|
171 | - if(is_numeric($fileName)){ |
|
169 | + foreach ($attachments as $fileName=>$attachment) { |
|
170 | + if (file_exists($attachment) || $this->isURL($attachment)) { |
|
171 | + if (is_numeric($fileName)) { |
|
172 | 172 | $fileName = basename($attachment); |
173 | - }else{ |
|
173 | + } else { |
|
174 | 174 | $extension = pathinfo($attachment, PATHINFO_EXTENSION); |
175 | 175 | if (strpos($fileName, '.'.$extension) === false) { |
176 | 176 | $fileName .= '.'.$extension; |
@@ -196,10 +196,10 @@ discard block |
||
196 | 196 | * @param array|string $headers |
197 | 197 | * @return $this |
198 | 198 | */ |
199 | - public function addHeaders($headers){ |
|
200 | - if(is_array($headers)){ |
|
201 | - $this->headers = array_merge($this->headers,$headers); |
|
202 | - }else{ |
|
199 | + public function addHeaders($headers) { |
|
200 | + if (is_array($headers)) { |
|
201 | + $this->headers = array_merge($this->headers, $headers); |
|
202 | + } else { |
|
203 | 203 | $this->headers[] = $headers; |
204 | 204 | } |
205 | 205 | return $this; |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | if(file_exists($attachment) || $this->isURL($attachment)) { |
171 | 171 | if(is_numeric($fileName)){ |
172 | 172 | $fileName = basename($attachment); |
173 | - }else{ |
|
173 | + } else{ |
|
174 | 174 | $extension = pathinfo($attachment, PATHINFO_EXTENSION); |
175 | 175 | if (strpos($fileName, '.'.$extension) === false) { |
176 | 176 | $fileName .= '.'.$extension; |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | public function addHeaders($headers){ |
200 | 200 | if(is_array($headers)){ |
201 | 201 | $this->headers = array_merge($this->headers,$headers); |
202 | - }else{ |
|
202 | + } else{ |
|
203 | 203 | $this->headers[] = $headers; |
204 | 204 | } |
205 | 205 | return $this; |
@@ -12,21 +12,21 @@ discard block |
||
12 | 12 | |
13 | 13 | //******************** 配置信息 start ******************************** |
14 | 14 | $config = [ |
15 | - 'host' => 'smtp.mxhichina.com',// 邮箱的服务器地址 |
|
16 | - 'port' => 465,// SMTP 端口 |
|
17 | - 'from_address' => '[email protected]',// 发件人地址,一般和username一致 |
|
18 | - 'username' => '[email protected]',// 用户名 |
|
19 | - 'password' => 'J^*****&H',// 密码 |
|
20 | - 'encryption' => 'ssl',// 加密方式 |
|
15 | + 'host' => 'smtp.mxhichina.com', // 邮箱的服务器地址 |
|
16 | + 'port' => 465, // SMTP 端口 |
|
17 | + 'from_address' => '[email protected]', // 发件人地址,一般和username一致 |
|
18 | + 'username' => '[email protected]', // 用户名 |
|
19 | + 'password' => 'J^*****&H', // 密码 |
|
20 | + 'encryption' => 'ssl', // 加密方式 |
|
21 | 21 | ]; |
22 | 22 | //******************** 配置信息 end ******************************** |
23 | 23 | |
24 | - $smtpemailto = '[email protected]';//发送给谁 |
|
25 | - $mailtitle = "测试邮件";//邮件主题 |
|
26 | - $mailcontent = "<h1>吃饭了嘛</h1>";//邮件内容 |
|
24 | + $smtpemailto = '[email protected]'; //发送给谁 |
|
25 | + $mailtitle = "测试邮件"; //邮件主题 |
|
26 | + $mailcontent = "<h1>吃饭了嘛</h1>"; //邮件内容 |
|
27 | 27 | |
28 | 28 | $email = Email::smtp($config); |
29 | - $email->setDebug(true);//线上注释此行 |
|
29 | + $email->setDebug(true); //线上注释此行 |
|
30 | 30 | $email->toEmail($smtpemailto); |
31 | 31 | $mailtype = "html"; |
32 | 32 | // $cc = "[email protected]"; |
@@ -34,10 +34,10 @@ discard block |
||
34 | 34 | // $attachments = ['E:\git\phpMailer\example\sendmail.php']; |
35 | 35 | // $attachments = ['http://oss-joywork.oss-cn-beijing.aliyuncs.com/miningplatform/api/6bb8b72d99de37d8e7b86243b52898ec.csv']; |
36 | 36 | $attachments = ['财务报表.csv'=>'http://oss-joywork.oss-cn-beijing.aliyuncs.com/miningplatform/api/6bb8b72d99de37d8e7b86243b52898ec.csv']; |
37 | - $email->addAttachments($attachments);//添加附件 |
|
37 | + $email->addAttachments($attachments); //添加附件 |
|
38 | 38 | // $email->ccEmail($cc);//抄送人 |
39 | 39 | // $email->bccEmail($bcc);//隐性抄送人 |
40 | - $state = $email->sendmail( $mailtitle, $mailcontent, $mailtype); |
|
40 | + $state = $email->sendmail($mailtitle, $mailcontent, $mailtype); |
|
41 | 41 | var_dump($state); |
42 | 42 | } |
43 | 43 |