yii 2011-04-12 08:51:51 4125次浏览 7条回复 0 0 0

舰长好,帮忙看看我的代码有什么问题,多谢!

1.main.php

'components' => array('smtpMail'=>array(
	'class'=>'SmtpMail',
	'host'=>'smtp.163.com',
	'port'=>25,
	'user'=>'test@163.com',
	'pass'=>'111111',
),)

2.components/SmtpMail.php

class SmtpMail  extends CApplicationComponent
{
	public $host;           //主机
	public $port;           //端口 一般为25
	public $user;           //SMTP认证的帐号
	public $pass;           //认证密码
	public $conn;             
	public $result_str;      //结果
	public $in;           //客户机发送的命令
	public $from;    //源信箱
	public $to;           //目标信箱
	public $subject;         //主题
	public $body;           //内容
	
	/*
	 * 构造函数
	 */
	function __construct()
	{
	   $this->host=$this->host;
	   $this->port=$this->port;
	   $this->user=base64_encode($this->user);
	   $this->pass=base64_encode($this->pass);
       	   $this->socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);  
	   $this->conn = socket_connect($this->socket,$this->host,$this->port);
	}
	/*
	 * 发送邮件 
	 */
	function send($from,$to,$subject,$body)
	{
	   if($from == "" || $to == "")
	   {
		   exit("请输入邮箱地址");
	   }
	   if($subject == "") $sebject="无标题";
	   if($body== "") $body= "无内容";
	   $this->from=$from;
	   $this->to=$to;
	   $this->subject=$subject;
	   $this->body=$body;
	   
	   $output = "MIME-Version:1.0\r\n";
       $output.= "Content-Type:text/html; charset=UTF-8\r\n";
	   $output.= "From:<".$this->from.">\r\n";
	   $output.= "To:<".$this->to.">\r\n";
	   $output.= "Subject:".$this->subject."\r\n\r\n";
	   $output.= $this->body;
	
	   //以下是和服务器会话
	   $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=$output."\r\n.\r\n";
	   $this->doCommand();
	
	   $this->in="QUIT\r\n";
	   $this->doCommand();
	}
	/*
	 * 执行命令
	 */
	function doCommand()
	{
	   socket_write ($this->socket, $this->in, strlen ($this->in));
	   $this->result_str = socket_read ($this->socket, 1024);
	}
}
您需要登录后才可以回复。登录 | 立即注册