· 10 years ago · Jun 26, 2015, 10:59 AM
1<?php
2function get_data($smtp_conn)
3{
4 $data="";
5 while($str = fgets($smtp_conn,515))
6 {
7 $data .= $str;
8 if(substr($str,3,1) == " ") { break; }
9 }
10 return $data;
11}
12function SendMail($config)
13{
14if(!isset($config['priority']))
15 $config['priority']=3;
16
17$header="Date: ".date("D, j M Y G:i:s")." +0700rn";
18$header.="From: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode("{$config['from_title']}")))."?= <{$config['from']}>rn";
19$header.="X-Mailer: The Bat! (v3.99.3) Professionalrn";
20$header.="Reply-To: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode("{$config['from_title']}")))."?= <{$config['from']}>rn";
21$header.="X-Priority: {$config['priority']}rn";
22$header.="Message-ID: <172562218.".date("YmjHis")."@microsoft.com>rn";
23$header.="To: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode($config['to_title'])))."?= <{$config['to']}>rn";
24$header.="Subject: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode($config['subject'])))."?=rn";
25$header.="MIME-Version: 1.0rn";
26$header.="Content-Type: multipart/mixed; boundary="----------A4D921C2D10D7DB"rn";
27
28$attach=NULL;
29$code_file=NULL;
30if(isset($config['attach_file']))
31{
32 if(file_exists($config['attach_file']))
33 {
34 $fp = fopen($config['attach_file'], "rb");
35 $code_file = chunk_split(base64_encode(fread($fp, filesize($config['attach_file']))));
36 fclose($fp);
37 $attach.="------------A4D921C2D10D7DB
38Content-Type: application/octet-stream; name="".basename($config['attach_file']).""
39Content-transfer-encoding: base64
40Content-Disposition: attachment; filename="".basename($config['attach_file']).""
41
42{$code_file}";
43 }
44}
45
46if(isset($config['attach_dir']))
47{
48 $dir = $config['attach_dir'];
49 if (is_dir($dir))
50 if ($dh = opendir($dir))
51 {
52 while (($file = readdir($dh)) !== false)
53 if($file!="." && $file!="..")
54 {
55 $fp = fopen($dir.$file, "rb");
56 $code_file = chunk_split(base64_encode(fread($fp, filesize($dir.$file))));
57 fclose($fp);
58 $attach.="------------A4D921C2D10D7DB
59Content-Type: application/octet-stream; name="".basename($dir.$file).""
60Content-transfer-encoding: base64
61Content-Disposition: attachment; filename="".basename($dir.$file).""
62
63{$code_file}";
64
65 }
66 closedir($dh);
67 }
68
69}
70
71if(isset($config['html_file']))
72{
73 if(file_exists($config['html_file']))
74 {
75 $filename = $config['html_file'];
76 $handle = fopen($filename, "r");
77 $contents = fread($handle, filesize($filename));
78 fclose($handle);
79 $config['html'].=$contents;
80 }
81}
82
83$text="------------A4D921C2D10D7DB
84Content-Type: text/html; charset=windows-1251
85Content-Transfer-Encoding: 8bit
86
87{$config['html']}
88
89{$attach}rn
90
91
92------------A4D921C2D10D7DB--
93";
94
95$smtp_conn = fsockopen($config['server'], $config['port'],$errno, $errstr, 10);
96if(!$smtp_conn) {print "Connect is fail"; fclose($smtp_conn); exit;}
97$data = get_data($smtp_conn);
98fputs($smtp_conn,"EHLO ".time()."rn");
99$code = substr(get_data($smtp_conn),0,3);
100if($code != 250) {print "Command fail EHLO"; fclose($smtp_conn); exit;}
101
102if(isset($config['login']))
103{
104 fputs($smtp_conn,"AUTH LOGINrn");
105 $code = substr(get_data($smtp_conn),0,3);
106 if($code != 334) {print "Command fail AUTH LOGIN"; fclose($smtp_conn); exit;}
107
108 fputs($smtp_conn,base64_encode($config['login'])."rn");
109 $code = substr(get_data($smtp_conn),0,3);
110 if($code != 334) {print "Login is not exists"; fclose($smtp_conn); exit;}
111
112 fputs($smtp_conn,base64_encode($config['password'])."rn");
113 $code = substr(get_data($smtp_conn),0,3);
114 if($code != 235) {print "Password is not correct"; fclose($smtp_conn); exit;}
115}
116
117$size_msg=strlen($header."rn".$text);
118
119fputs($smtp_conn,"MAIL FROM:<{$config['from']}> SIZE=".$size_msg."rn");
120
121$code = substr(get_data($smtp_conn),0,3);
122if($code != 250) {print "Command fail MAIL FROM"; fclose($smtp_conn); exit;}
123
124fputs($smtp_conn,"RCPT TO:<{$config['to']}>rn");
125$code = substr(get_data($smtp_conn),0,3);
126if($code != 250 AND $code != 251) {print "Command fail RCPT TO"; fclose($smtp_conn); exit;}
127
128fputs($smtp_conn,"DATArn");
129$code = substr(get_data($smtp_conn),0,3);
130if($code != 354) {print "Command fail DATA"; fclose($smtp_conn); exit;}
131
132fputs($smtp_conn,$header."rn".$text."rn.rn");
133$code = substr(get_data($smtp_conn),0,3);
134if($code != 250) {print "Send main error"; fclose($smtp_conn); exit;}
135
136fputs($smtp_conn,"QUITrn");
137fclose($smtp_conn);
138
139if(@$config['debug']=="on")
140{
141 echo "<pre><code>";
142 print_r($config);
143 echo "</code></pre>";
144
145 echo "Attach file:<br>{$attach}";
146}
147else
148 echo "200: OK";
149}
150/*
151server - smtp.mail.ru
152port - 25
153
154login - barsukov.vv@mail.ru
155passowrd - *********
156
157from - admin@yandex.ru
158from_title - ÐдминиÑÑ‚Ñ€Ð°Ñ†Ð¸Ñ Ð¯Ð½Ð´ÐµÐºÑ.ру
159
160to - target@domain.net
161to_title - Target
162
163subject - Spam
164html - <div style='font-size:36pt;'>html</div>
165html_file - html/plain.html
166
167priority : важноÑть
168 1 - выÑокаÑ
169 3 - обычнаÑ(по умолчанию)
170 5 - низкаÑ
171
172attach_file - image.jpg
173attach_dir - image/
174
175debug - De`жук(on)
176*/
177SendMail(array("from"=>"admin@yandex.ru", "from_title"=>"ÐдминиÑÑ‚Ñ€Ð°Ñ†Ð¸Ñ Yandex.ru", "to"=>"barsukov.vv@mail.ru", "to_title"=>"БарÑуков Владимир", "server"=>"mail-omsk.omsk.srsh.ru", "port"=>25, "subject"=>time(), "html"=>"html", "attach_file"=>"test/mazda.jpg", "attach_dir"=>"test/", "html_file"=>"test.html", "priority"=>"1", "debug"=>"on"));
178?>
179
180<?php
181 mail("to@somewhere.ru", "Subject", "message");
182?>