Отправка e-mail через SMTP function send_mail to subject text add_head

 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
/* Отправка e-mail через SMTP */
function send_mail($to, $subject, $text, $add_headers = "", $cmd = "") {
// return mail($to, $subject, $text, $add_headers, $cmd);
$smtp_host = "mail.evilworlds.ru";
$smtp_user = "robot@evilworlds.ru";
$smtp_password = "";
$smtp_port = "25";
$from = "admin@evilworlds.ru";
if(!filter_var($to, FILTER_VALIDATE_EMAIL))
return false; // Невалидный e-mail
$fp = fsockopen($smtp_host, $smtp_port, $errno, $errstr);
if($errno)
return false; // Ошибка подключения к серверу
$headers = "";
$headers .= "Subject: =?utf-8?Q?".encode_subject($subject)."?=\r\n"
."To: <".$to.">\r\n"
."Content-Transfer-Encoding: 8bit\r\n";
if(!empty($add_headers))
$headers .= $add_headers;
$message_body = $headers."\n".$text;
get_smtp_data($fp);
fwrite($fp, "EHLO Azq2\r\n");
get_smtp_data($fp);
fwrite($fp, "AUTH LOGIN\r\n");
get_smtp_data($fp);
fwrite($fp, base64_encode($smtp_user)."\r\n");
get_smtp_data($fp);
fwrite($fp, base64_encode($smtp_password)."\r\n");
get_smtp_data($fp);
fwrite($fp, "MAIL FROM: ".$from." SIZE=".strlen($message_body)."\r\n");
get_smtp_data($fp);
fwrite($fp, "RCPT TO: <".$to.">\r\n");
get_smtp_data($fp);
fwrite($fp, "DATA\r\n");
get_smtp_data($fp);
fwrite($fp, $message_body."\r\n.\r\n");
get_smtp_data($fp);
fwrite($fp, "QUIT\r\n");
get_smtp_data($fp);
fclose($fp);
return true;
}
/* Кодирование заголовка */
function encode_subject($text) {
return str_replace(
array("%", "+"),
array("=", "_"),
urlencode($text));
}
/* Прием данных с сервера SMTP */
function get_smtp_data($ch) {
$data = "";
while($buff = fgets($ch, 515)) {
$data .= $buff;
if(feof($ch) || substr($buff, 3, 1) == " ")
break;
}
return $data;
}