- 论坛徽章:
- 0
|
- 通过crontab 跑个程序通过以下的类来完成发邮件
- <?
- class smtp_mail
- {
- var $host;
- var $port;
- var $user;
- var $pass;
- var $debug = false;
- var $conn;
- var $result_str;
- var $in; //conmand send to smtp server
- var $from;
- var $to;
- var $subject;
- var $body;
- function __destruct()
- {
- }
- function __construct($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, 0);
- if($this->socket)
- {
- $this->result_str = "create SOCKET:".socket_strerror(socket_last_error());
- $this->debug_show($this->result_str);
- }
- else
- {
- die("init fail, check your parameter");
- }
- $this->conn = socket_connect($this->socket,$this->host,$this->port);
- if($this->conn)
- {
- $this->result_str = "connect SOCKET:".socket_strerror(socket_last_error());
- $this->debug_show($this->result_str);
- }
- else
- {
- exit("init fail, check your parameter");
- }
- $this->result_str = "server:".socket_read($this->socket, 1024);
- $this->debug_show($this->result_str);
- }
- function debug_show($str)
- {
- if($this->debug)
- {
- echo $str."n";
- }
- }
- function send($from,$to,$body)
- {
- if($from == "" || $to == "")
- {
- die("no from or to address");
- }
- $this->subject = "mail user data".date('Y-m-d',time()-86400);
- $this->body = $body;
- $this->from = $from;
- $this->to = $to;
- $this->body = $body;
- $All = "From:<".$this->from.">\r\n";
- $All .= "To:<".$this->to.">\r\n";
- $All .= "Subject:".$this->subject."\r\n\r\n";
- $All .= $this->body;
- //interactive with smtp server
- $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();
- $this->in = "MAIL FROM:<".$this->from.">\r\n";
- $this->docommand();
- $this->in = "RCPT TO:<".$this->to.">\r\n";
- $this->docommand();
- $this->in = "DATA\r\n";
- $this->docommand();
- $this->in = $All."\r\n.\r\n";
- $this->docommand();
- $this->in = "QUIT\r\n";
- $this->docommand();
- //interactive end
- }
- function docommand()
- {
- socket_write($this->socket, $this->in, strlen($this->in));
- $this->debug_show("client:".$this->in);
- $this->result_str = "server:".socket_read($this->socket, 1024);
- $this->debug_show($this->result_str);
- }
- }
复制代码 |
|