当前位置:百问九>百科问答>php如何发送邮件

php如何发送邮件

2023-04-26 03:24:20 编辑:zane 浏览量:577

php如何发送邮件

你好,用这个邮件类,需要在调用时,填写一个smtp服务器和你的用户名密码。 username = substr( $from, 0, strpos( $from , "@" ) ); $this->password = $password; $this->smtp = $smtp ? $smtp : $this->smtp; $this->check = $check; $this->s_from = $from; } } /* * 功能:发送邮件 * $to 目标邮箱 * $from 来源邮箱 * $subject 邮件标题 * $message 邮件内容 */ function send ( $to, $from, $subject, $message ) { //连接服务器 $fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60); if (!$fp ) return "联接服务器失败".__LINE__; set_socket_blocking($fp, true ); $lastmessage=fgets($fp,512); if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__; //HELO $yourname = "YOURNAME"; if($this->check == "1") $lastact="EHLO ".$yourname."\r\n"; else $lastact="HELO ".$yourname."\r\n"; fputs($fp, $lastact); $lastmessage == fgets($fp,512); if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__; while (true) { $lastmessage = fgets($fp,512); if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) ) break; } //身份验证 if ($this->check=="1") { //验证开始 $lastact="AUTH LOGIN"."\r\n"; fputs( $fp, $lastact); $lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__; //用户姓名 $lastact=base64_encode($this->username)."\r\n"; fputs( $fp, $lastact); $lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__; //用户密码 $lastact=base64_encode($this->password)."\r\n"; fputs( $fp, $lastact); $lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__; } //FROM: $lastact="MAIL FROM: <". $this->s_from . ">\r\n"; fputs( $fp, $lastact); $lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__; //TO: $lastact="RCPT TO: <". $to ."> \r\n"; fputs( $fp, $lastact); $lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__; //DATA $lastact="DATA\r\n"; fputs($fp, $lastact); $lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__; //处理Subject头 $head="Subject: $subject\r\n"; $message = $head."\r\n".$message; //处理From头 $head="From: $from\r\n"; $message = $head.$message; //处理To头 $head="To: $to\r\n"; $message = $head.$message; //加上结束串 $message .= "\r\n.\r\n"; //发送信息 fputs($fp, $message); $lastact="QUIT\r\n"; fputs($fp,$lastace); fclose($fp); return 0; } } // 发送示例 // 只需要把这部分改成你的信息就行$sm = new smail( "用户名", "密码", "发件smtp服务器" ); $end = $sm->send( "收件人", "发件人(可以伪造哦)", "标题", "内容" ); if( $end ) echo $end; else echo "发送成功!$x"; ?>

版权声明:文章由 百问九 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.baiwen9.com/answer/158420.html
热门文章
二维码