-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendmail.php
More file actions
297 lines (248 loc) · 9.58 KB
/
Copy pathSendmail.php
File metadata and controls
297 lines (248 loc) · 9.58 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
<?php
/*
=================================================================================
+ 안내 : 이 파일은 아래의 웹싸이트 자료를 참고로 수정하였음을 알려드립니다.
+ 출처 : http://redqueen-textcube.blogspot.kr/2011/07/php-class.html
+ 제작자 : 하근호(hopegiver@korea.com)
=================================================================================
+ 파일명 : Sendmail.php
+ 수정일 : 2015-06-04/2015-06-04(최종수정일)
+ 수정자 : Redinfo (webmaster@redinfo.co.kr)
+ 기타 : (가이드 URL) http://b.redinfo.co.kr/87
(싸이트 URL) http://b.redinfo.co.kr
=================================================================================
*/
class Sendmail {
/* smtp 의 호스트 설정 : 아래는 gmail 일경우 */
var $host="ssl://smtp.gmail.com";
/* smtp 계정 아이디 입력 */
var $smtp_id="yicappenzeller@gmail.com";
/* smtp 계정 비밀번호 입력 */
var $smtp_pw="dkvpswpffj2018";
/* 디버그모드 - 활성 :1, 비활성 : 0; */
var $debug = 1;
/* 문자 인코딩 종류 설정*/
var $charset="UTF-8";
/* 메일의 기본 타입을 설정 */
var $ctype="text/html";
/* 아래 3개의 변수는 수정 금지 */
var $fp;
var $lastmsg;
var $parts=array();
/* 기본설정대신 초기화 할 값이 있다면 클래스 초기화시 배열로 값을넘겨준다. */
function Sendmail($data=false) {
if($data!=false)
{
if(is_array($data)){
/* 각각 배열의 정보는 기본설정 변수명과 같으니 그곳을 참고하길 바란다 */
$this->host = !empty($data['host'])?$data['host']:$this->host;
$this->smtp_id = !empty($data['smtp_id'])?$data['smtp_id']:$this->smtp_id;
$this->smtp_pw = !empty($data['smtp_pw'])?$data['smtp_pw']:$this->smtp_pw;
$this->debug = !empty($data['debug'])?$data['debug']:$this->debug;
$this->charset = !empty($data['charset'])?$data['charset']:$this->charset;
$this->ctype = !empty($data['ctype'])?$data['ctype']:$this->ctype;
}
}
}
/* smtp 통신을 한다. */
function dialogue($code, $cmd) {
fputs($this->fp, $cmd."\r\n");
$line = fgets($this->fp, 1024);
preg_match("/^([0-9]+).(.*)$/", $line, $matches);
$this->lastmsg = $matches[0];
if($this->debug) {
echo htmlspecialchars($cmd)."
".$this->lastmsg."
";
flush();
}
if($matches[1] != $code) return false;
return true;
}
/* smptp 서버에 접속을 한다. */
function connect($host='') {
if($this->debug) {
echo "SMTP(".$host.") Connecting...";
flush();
}
if(!$host) $host = $this->host;
if(!$this->fp = fsockopen($host, 465, $errno, $errstr, 10)) {
$this->lastmsg = "SMTP(".$host.") 서버접속에 실패했습니다.[".$errno.":".$errstr."]";
return false;
}
$line = fgets($this->fp, 1024);
preg_match("/^([0-9]+).(.*)$/", $line, $matches);
$this->lastmsg = $matches[0];
if($matches[1] != "220") return false;
if($this->debug) {
echo $this->lastmsg."
";
flush();
}
$this->dialogue(250, "HELO phpmail");
return true;
}
/* stmp 서버와의 접속을 끊는다. */
function close() {
$this->dialogue(221, "QUIT");
fclose($this->fp);
return true;
}
/* 메시지를 보낸다. */
function smtp_send($email, $from, $data,$cc_mail,$bcc_mail,$rel_to=false) {
$id = $this->smtp_id;
$pwd = $this->smtp_pw;
/* 이메일 형식 검사 구간*/
if(!$mail_from = $this->get_email($from)) return false;
if(!$rcpt_to = $this->get_email($email)) return false;
/* smtp 검사 구간 */
if(!$this->dialogue(334, "AUTH LOGIN")) { return false; }
if(!$this->dialogue(334, base64_encode($id))) return false;
if(!$this->dialogue(235, base64_encode($pwd))) return false;
if(!$this->dialogue(250, "MAIL FROM:".$mail_from)) return false;
if(!$this->dialogue(250, "RCPT TO:".$rcpt_to)) {
$this->dialogue(250, "RCPT TO:");
$this->dialogue(354, "DATA");
$this->dialogue(250, ".");
return false;
}
if($rel_to==false){ $rel_to=$email;}
$this->dialogue(354, "DATA");
$mime = "Message-ID: <".$this->get_message_id().">\r\n";
$mime .= "From: ".$from."\r\n";
$mime .= "To: ".$rel_to."\r\n";
/* CC 메일 이 있을경우 */
if($cc_mail!=false){
$mime .= "Cc: ".$cc_mail. "\r\n";
}
/* BCC 메일 이 있을경우 */
if($bcc_mail!=false) $mime .= "Bcc: ".$bcc_mail. "\r\n";
fputs($this->fp, $mime);
fputs($this->fp, $data);
$this->dialogue(250, ".");
}
/* Message ID 를 얻는다. */
function get_message_id() {
$id = date("YmdHis",time());
mt_srand((float) microtime() * 1000000);
$randval = mt_rand();
$id .= $randval."@phpmail";
return $id;
}
/* Boundary 값을 얻는다. */
function get_boundary() {
$uniqchr = uniqid(time());
$one = strtoupper($uniqchr[0]);
$two = strtoupper(substr($uniqchr,0,8));
$three = strtoupper(substr(strrev($uniqchr),0,8));
return "----=_NextPart_000_000${one}_${two}.${three}";
}
/* 첨부파일이 있을 경우 이 함수를 이용해 파일을 첨부한다. */
function attach($path, $name="", $ctype="application/octet-stream") {
if(is_file($path)) {
$fp = fopen($path, "r");
$message = fread($fp, filesize($path));
fclose($fp);
$this->parts[] = array ("ctype" => $ctype, "message" => $message, "name" => $name);
} else return false;
}
/* Multipart 메시지를 생성시킨다. */
function build_message($part) {
$msg = "Content-Type: ".$part['ctype'];
if($part['name']) $msg .= "; name=\"".$part['name']."\"";
$msg .= "\r\nContent-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"".$part['name']."\"\r\n\r\n";
$msg .= chunk_split(base64_encode($part['message']));
return $msg;
}
/* SMTP에 보낼 DATA를 생성시킨다. */
function build_data($subject, $body) {
$boundary = $this->get_boundary();
$attcnt = sizeof($this->parts);
$mime= "Subject: ".$subject."\r\n";
$mime .= "Date: ".date ("D, j M Y H:i:s T",time())."\r\n";
$mime .= "MIME-Version: 1.0\r\n";
if($attcnt > 0) {
$mime .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n\r\n".
"This is a multi-part message in MIME format.\r\n\r\n";
$mime .= "--".$boundary."\r\n";
}
$mime .= "Content-Type: ".$this->ctype."; charset=\"".$this->charset."\"\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n" . chunk_split(base64_encode($body));
if($attcnt > 0) {
$mime .= "\r\n\r\n--".$boundary;
for($i=0; $i<$attcnt; $i++) {
$mime .= "\r\n".$this->build_message($this->parts[$i])."\r\n\r\n--".$boundary;
}
$mime .= "--\r\n";
}
return $mime;
}
/* MX 값을 찾는다. */
function get_mx_server($email) {
if(!preg_match("/([\._0-9a-zA-Z-]+)@([0-9a-zA-Z-]+\.[a-zA-Z\.]+)/", $email, $matches)) return false;
getmxrr($matches[2], $host);
if(!$host) $host[0] = $matches[2];
return $host;
}
/* 이메일의 형식이 맞는지 체크한다. */
function get_email($email) {
if(!preg_match("/([\._0-9a-zA-Z-]+)@([0-9a-zA-Z-]+\.[a-zA-Z\.]+)/", $email, $matches)) return false;
return "<".$matches[0].">";
}
/* 메일을 전송한다. */
function send_mail($to, $from, $subject, $body,$cc_mail=false,$bcc_mail=false) {
$from.=" <".$this->smtp_id.">";
if(!is_array($to)){
$rel_to=$to;
$to = explode(",",$to);
}
else{
$rel_to=implode(',',$to);
}
$data = $this->build_data($subject, $body);
if($this->host == "auto") {
foreach($to as $email) {
if($host = $this->get_mx_server($email)) {
for($i=0, $max=count($host); $i<$max; $i++) {
if($conn = $this->connect($host[$i])) break;
}
if($conn) {
$this->smtp_send($email, $from, $data,$cc_mail,$bcc_mail);
$this->close();
}
}
}
} else {
foreach($to as $key=>$email){
$this->connect($this->host);
$this->smtp_send($email, $from, $data,$cc_mail,$bcc_mail,$rel_to);
$this->close();
}
if($cc_mail!=false){
$this->cc_email($rel_to,$from,$data,$cc_mail,$bcc_mail);
}
if($bcc_mail!=false){
$this->bcc_email($rel_to,$from,$data,$cc_mail,$bcc_mail);
}
}
}
function cc_email($rel_to,$from,$data,$cc_mail,$bcc_mail)
{
if(!is_array($cc_mail)) $cc = explode(",",$cc_mail);
foreach($cc as $email){
$this->connect($this->host);
$this->smtp_send($email, $from, $data,$cc_mail,$bcc_mail,$rel_to);
$this->close();
}
}
function bcc_email($rel_to,$from,$data,$cc_mail,$bcc_mail){
if(!is_array($bcc_mail)) $bcc = explode(",",$bcc_mail);
foreach($bcc as $email){
$this->connect($this->host);
$this->smtp_send($email, $from, $data,$cc_mail,$bcc_mail,$rel_to);
$this->close();
}
}
}
?>