-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathsmtp.cls.php
executable file
·138 lines (117 loc) · 4.2 KB
/
smtp.cls.php
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
<?php
/*
* This file is part of the phpems/phpems.
*
* (c) oiuv <[email protected]>
*
* 项目维护:oiuv(QQ:7300637) | 定制服务:火眼(QQ:278768688)
*
* This source file is subject to the MIT license that is bundled.
*/
class smtp
{
public $host; //主机
public $port; //端口 一般为25
public $user; //SMTP认证的帐号
public $pass; //认证密码
public $debug = true; //是否显示和服务器会话信息?
public $conn;
public $result_str; //结果
public $in; //客户机发送的命令
public $from_r; //真实的源信箱,一般与smtp服务器的用户名一样,否则可能由于smtp服务器的设置而发送不成功
public $mailformat = 1; //邮件格式 0=普通文本 1=html邮件
public function smtp_mail($host, $port, $user, $pass, $debug = false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->debug = $debug;
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //具体用法请参考手册
if ($this->socket) {
$this->result_str = '创建SOCKET:'.socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
} else {
exit('初始化失败,请检查您的网络连接和参数');
}
$this->conn = socket_connect($this->socket, $this->host, $this->port);
if ($this->conn) {
$this->result_str = '创建SOCKET连接:'.socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
} else {
exit('初始化失败,请检查您的网络连接和参数');
}
$this->result_str = '服务器应答:<font color=#cc0000>'.socket_read($this->socket, 1024).'</font>';
$this->debug_show($this->result_str);
}
public function debug_show($str)
{
if ($this->debug) {
echo $str."<p>\r\n";
}
}
public function send($from, $to, $subject, $body)
{
if ('' == $from || '' == $to) {
exit('请输入信箱地址');
}
if ('' == $subject) {
$sebject = '无标题';
}
if ('' == $body) {
$body = '无内容';
}
$All = 'From:'.$from."\r\n";
$All .= 'To:'.$to."\r\n";
$All .= 'Subject:'.$subject."\r\n";
if (1 == $this->mailformat) {
$All .= "Content-Type: text/html;\r\n";
} else {
$All .= "Content-Type: text/plain;\r\n";
}
$All .= "charset=\r\n\r\n";
$All .= $body;
/*
如果把$All的内容再加处理,就可以实现发送MIME邮件了
不过还需要加很多程序
*/
//以下是和服务器会话
$this->in = "EHLO HELO\r\n";
$this->docommand();
$this->in = "AUTH LOGIN\r\n";
$this->docommand();
$this->in = $this->user."\r\n";
$this->docommand();
$this->in = $this->pass."\r\n";
$this->docommand();
if (!preg_match('/235/', $this->result_str)) {
$this->result_str = 'smtp 认证失败';
$this->debug_show($this->result_str);
return 0;
}
$this->in = 'MAIL FROM:'.$from."\r\n";
$this->docommand();
$this->in = 'RCPT TO:'.$to."\r\n";
$this->docommand();
$this->in = "DATA\r\n";
$this->docommand();
$this->in = $All."\r\n.\r\n";
$this->docommand();
if (!preg_match('/250/', $this->result_str)) {
$this->result_str = '邮件发送失败';
$this->debug_show($this->result_str);
return 0;
}
$this->in = "QUIT\r\n";
$this->docommand();
//结束,关闭连接
return 1;
}
public function docommand()
{
socket_write($this->socket, $this->in, strlen($this->in));
$this->debug_show('客户机命令:'.$this->in);
$this->result_str = '服务器应答:<span color=#cc0000>'.socket_read($this->socket, 1024).'</span>';
$this->debug_show($this->result_str);
}
} //end class