-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.php
More file actions
73 lines (63 loc) · 1.57 KB
/
mail.php
File metadata and controls
73 lines (63 loc) · 1.57 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
<?php
/**
* Mail 邮件发送类
* 通过 SMTP 协议发送邮件
* 无需 PHP 扩展支持
*/
class Mail
{
protected $config;
/**
* 构造函数
* @param array $config SMTP 配置
*/
function __construct($config)
{
$this->config = $config;
}
/**
* 发送邮件
* @param string $to_user 收件人邮箱
* @param string $subject 邮件主题
* @param string $content 邮件内容
* @return bool 发送结果
*/
public function send($to_user,$subject,$content)
{
$type = $this->config['html'] ? 'Content-type: text/html;' : 'Content-type: text/plain;';
$cmd = [
"EHLO {$this->config['smtp_name']}\r\n",
"AUTH LOGIN\r\n",
base64_encode($this->config['smtp_user'])."\r\n",
base64_encode($this->config['smtp_pass'])."\r\n",
"MAIL FROM: <{$this->config['smtp_user']}>\r\n",
"RCPT TO: <{$to_user}>\r\n",
"DATA\r\n",
"From: \"{$this->config['smtp_name']}\"<{$this->config['smtp_user']}>\r\n",
"To: <{$to_user}>\r\n",
"Subject:{$subject}\r\n",
$type."\r\n",
"\r\n",
$content." \r\n",
".\r\n",
"QUIT\r\n",
];
$this->connect($cmd);
return true;
}
/**
* 连接 SMTP 服务器并发送命令
* @param array $cmd SMTP 命令列表
*/
protected function connect($cmd)
{
$fp = @pfsockopen($this->config['smtp_host'], $this->config['smtp_port']);
$fp or die("Error: Cannot conect to ".$smtp_host);
foreach ($cmd as $k => $v) {
@fputs($fp, $v );
$res= fgets($fp);
echo "\n {$v} {$res} \n";
usleep(500000);
}
}
}