自己写的一个简易邮件发送类
程序员文章站
2024-01-24 09:30:22
...
跳至
调用示例: smtp('smtp.qq.com'); //邮件发送服务器 $smtp->pors(25); //邮件服务器端口 $smtp->login( '369758482@qq.com'); //邮件服务器登录用户 $smtp->pass('您的邮箱密码'); //邮件服务器登录密码 $smtp->mails( '1140714215@qq.com'); //邮件接收人 $smtp->from( '369758482@qq.com'); //邮件发送人 $smtp->come( 'baiyi1983000@163.com'); //接收邮件回复地址 $smtp->title('这是一封邮件测试地址邮件'); //邮件主题 $smtp->body('这是一封邮件测试内容邮件'); //邮件内容 $smtp->send(); //发送邮件 echo $smtp -> Mesg === true ? '邮件发送成功' : $smtp -> Mesg; //邮件发送结果
2. [文件] PHPMailSMTP.class.php ~ 4KB
Smtp = $Smtp; return $this; } public function pors( $pors = 25 ){ $this -> PORS = $pors; return $this; } public function login( $login = '' ) { $this -> LoginMail = $login; return $this; } public function pass( $pass = '' ){ $this -> LoginPass = $pass; return $this; } public function mails( $mail = '' ) { $this -> MailTo = $mail; return $this; } public function from( $form = '' ) { $this -> MailFrom = $form; return $this; } public function come( $come = '' ) { $this -> MailCome = $come; return $this; } public function title( $title = '' ) { $this -> Title = $title; return $this; } public function body( $body = '' ) { $this -> Body = $body; return $this; } public function send(){ $fp = @fsockopen( $this->Smtp, $this->PORS); @set_socket_blocking($fp, true); $Server = @fgets($fp, 512); if( !$this->MsgErr( $Server, '/^220/i', '邮件服务器连接失败' ) ) { return false; } @fputs($fp, 'HELO phpsetmail' . "\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^250/i', '与服务器会话失败' ) ) { return false; } @fputs($fp, 'AUTH LOGIN' . "\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^334/i', '请求与服务器进行用户验证失败' ) ) { return false; } @fputs($fp, base64_encode( $this->LoginMail ) . "\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^334/i', '用户验证失败[用户名]' ) ) { return false; } @fputs($fp, base64_encode( $this->LoginPass ) . "\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^235/i', '用户验证失败[密码]' ) ) { return false; } @fputs($fp, 'MAIL FROM:LoginMail . ">\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^250/i', '发送用户验证失败' ) ) { return false; } @fputs($fp, 'RCPT TO:MailTo . ">\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^250/i', '接受用户名验证失败' ) ) { return false; } @fputs($fp, 'DATA' . "\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^354/i', '请求与服务器发送邮件数据失败' ) ) { return false; } @fputs($fp, 'From:' . $this->MailCome ."\r\n"); @fputs($fp, 'Subject:' . $this->Title . "\r\n"); @fputs($fp, 'To:' . $this->MailTo . "\r\n"); @fputs($fp, "\r\n"); @fputs($fp, $this->Body . "\r\n"); @fputs($fp, '.' . "\r\n"); $Server = @fgets($fp, 2000); if( !$this->MsgErr( $Server, '/^250/i', '发送邮件失败' ) ) { return false; } @@fputs($fp, 'QUIT' . "\r\n"); @fclose($fp); unset( $fp, $Server ); $this -> Mesg = true; return true; } private function MsgErr( $ObjServer = '', $rezg = '/(.*)/i', $Msg = '' ) { if ( !preg_match( $rezg, $ObjServer ) ) { $this -> Mesg = $Msg . $ObjServer; return false; } else { $this -> Mesg = true; return true; } } }