文章目录

闲了很多天,又不知道写点什么,看书看到了socket这块,还挺有意思,照着书上的模仿了一下,用socket模拟smtp协议发送邮件,邮件服务器是qq的。不过书有点老,qq的邮件服务器必须用https了,所以自己摸索着实现了一下

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
<?php
/**
* Created by PhpStorm.
* User: eric
* Date: 15/12/24
* Time: 上午10:54
*/
class Mail{
private $sock;
private $errorStr;
private $errorNo;
private $timeout=30;
private $host;
private $port;
private $user;
private $password;
function __construct($host,$port,$user,$pass,$format=1,$debug=0){
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->password = base64_encode($pass);
$this->sock = fsockopen($this->host,$this->port,$this->errorNo,$this->errorStr,$this->timeout);
if(!$this->sock){
exit("oops");
}
$response = fgets($this->sock);
if(strstr($response,'220') === false){
exit("server error");
}
}
function showDebug($response,$commond){
echo $commond;
echo $response.PHP_EOL;
}
function doCommond($commond,$return_code){
fwrite($this->sock,$commond);
$response = fgets($this->sock);
if(stristr($response,"$return_code") == false){
$this->showDebug($response,$commond);
return false;
}
return true;
}
function sendMail($from,$to,$sub,$body){
$detail = "FROM:".$from."\r\n";
$detail.="To:".$to."\r\n";
$detail.="Subject:".$sub."\r\n";
$detail.="Content-type:text/plain;\r\n\r\n";
$detail.=$body;
$this->doCommond("HELO smtp.qq.com\r\n",250);
$this->doCommond("AUTH LOGIN\r\n",334);
$this->doCommond($this->user."\r\n",334);
$this->doCommond($this->password."\r\n",235);
$this->doCommond("MAIL FROM:".$from."\r\n",250);
$this->doCommond("RCPT TO:".$to."\r\n",250);
$this->doCommond("DATA\r\n",354);
$this->doCommond($detail."\r\n.\r\n",250);
$this->doCommond("QUIT\r\n",221);
return true;
}
}
$mail = new Mail('ssl://smtp.qq.com',465,'xxx@qq.com','特殊的密码,因为我有邮箱独立密码,所以只能使用qq邮箱额外提供的');
$mail->sendMail('xxx@qq.com','xxxx@qq.com','hi','嘿嘿嘿');

因为smtp可以在telnet中模拟,所以想着去模拟一个请求去访问redis也应该是可以的,在没有装redis扩展或者composer依赖的时候,简单的请求可以直接用socket实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/**
* Created by PhpStorm.
* User: eric
* Date: 15/12/24
* Time: 下午2:15
*/
$socket = fsockopen('127.0.0.1',6379);
fwrite($socket,"ping\r\n");
$response = fgets($socket);
echo "<pre>";print_r($response);
fwrite($socket,"set a aaa\r\n ");
$response = fgets($socket);
echo "<pre>";print_r($response);
fwrite($socket,"get a\r\n");
$response = fgets($socket);
echo "<pre>";print_r($response);

接下来尝试下用php socket搭建一个煎蛋代理服务(因为之前转过了一篇node)

文章目录