PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)


Posted in PHP onOctober 09, 2006

smtp.php

<?php
class smtp

{

/* Public Variables */

var $smtp_port;

var $time_out;

var $host_name;

var $log_file;

var $relay_host;

var $debug;

var $auth;

var $user;

var $pass;

/* Private Variables */
var $sock;

/* Constractor */

function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)

{

$this->debug = FALSE;

$this->smtp_port = $smtp_port;

$this->relay_host = $relay_host;

$this->time_out = 30; //is used in fsockopen()
#

$this->auth = $auth;//auth

$this->user = $user;

$this->pass = $pass;

#

$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";

 

$this->sock = FALSE;

}

/* Main Function */

function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")

{

$mail_from = $this->get_address($this->strip_comment($from));

$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);

$header .= "MIME-Version:1.0\r\n";

if($mailtype=="HTML"){

$header .= "Content-Type:text/html\r\n";

}

$header .= "To: ".$to."\r\n";

if ($cc != "") {

$header .= "Cc: ".$cc."\r\n";

}

$header .= "From: $from<".$from.">\r\n";

$header .= "Subject: ".$subject."\r\n";

$header .= $additional_headers;

$header .= "Date: ".date("r")."\r\n";

$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";

list($msec, $sec) = explode(" ", microtime());

$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";

$TO = explode(",", $this->strip_comment($to));

if ($cc != "") {

$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));

}

if ($bcc != "") {

$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));

}

$sent = TRUE;

foreach ($TO as $rcpt_to) {

$rcpt_to = $this->get_address($rcpt_to);

if (!$this->smtp_sockopen($rcpt_to)) {

$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");

$sent = FALSE;

continue;

}

if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {

$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");

} else {

$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");

$sent = FALSE;

}

fclose($this->sock);

$this->log_write("Disconnected from remote host\n");

}

return $sent;

}

 

/* Private Functions */

 

function smtp_send($helo, $from, $to, $header, $body = "")

{

if (!$this->smtp_putcmd("HELO", $helo)) {

return $this->smtp_error("sending HELO command");

}

#auth

if($this->auth){

if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {

return $this->smtp_error("sending HELO command");

}

if (!$this->smtp_putcmd("", base64_encode($this->pass))) {

return $this->smtp_error("sending HELO command");

}

}

#

if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {

return $this->smtp_error("sending MAIL FROM command");

}

if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {

return $this->smtp_error("sending RCPT TO command");

}

if (!$this->smtp_putcmd("DATA")) {

return $this->smtp_error("sending DATA command");

}

if (!$this->smtp_message($header, $body)) {

return $this->smtp_error("sending message");

}

if (!$this->smtp_eom()) {

return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");

}

if (!$this->smtp_putcmd("QUIT")) {

return $this->smtp_error("sending QUIT command");

}

return TRUE;

}

function smtp_sockopen($address)

{

if ($this->relay_host == "") {

return $this->smtp_sockopen_mx($address);

} else {

return $this->smtp_sockopen_relay();

}

}

function smtp_sockopen_relay()

{

$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");

$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);

if (!($this->sock && $this->smtp_ok())) {

$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");

$this->log_write("Error: ".$errstr." (".$errno.")\n");

return FALSE;

}

$this->log_write("Connected to relay host ".$this->relay_host."\n");

return TRUE;;

}

 

function smtp_sockopen_mx($address)

{

$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);

if (!@getmxrr($domain, $MXHOSTS)) {

$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");

return FALSE;

}

foreach ($MXHOSTS as $host) {

$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");

$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);

if (!($this->sock && $this->smtp_ok())) {

$this->log_write("Warning: Cannot connect to mx host ".$host."\n");

$this->log_write("Error: ".$errstr." (".$errno.")\n");

continue;

}

$this->log_write("Connected to mx host ".$host."\n");

return TRUE;

}

$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");

return FALSE;

}

 

function smtp_message($header, $body)

{

fputs($this->sock, $header."\r\n".$body);

$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));

 

return TRUE;

}

 

function smtp_eom()

{

fputs($this->sock, "\r\n.\r\n");

$this->smtp_debug(". [EOM]\n");

 

return $this->smtp_ok();

}

 

function smtp_ok()

{

$response = str_replace("\r\n", "", fgets($this->sock, 512));

$this->smtp_debug($response."\n");

 

if (!ereg("^[23]", $response)) {

fputs($this->sock, "QUIT\r\n");

fgets($this->sock, 512);

$this->log_write("Error: Remote host returned \"".$response."\"\n");

return FALSE;

}

return TRUE;

}

function smtp_putcmd($cmd, $arg = "")

{

if ($arg != "") {

if($cmd=="") $cmd = $arg;

else $cmd = $cmd." ".$arg;

}

fputs($this->sock, $cmd."\r\n");

$this->smtp_debug("> ".$cmd."\n");

return $this->smtp_ok();

}

function smtp_error($string)

{

$this->log_write("Error: Error occurred while ".$string.".\n");

return FALSE;

}

function log_write($message)

{

$this->smtp_debug($message);

if ($this->log_file == "") {

return TRUE;

}

$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;

if (log_file">!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {

$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");

return FALSE;;

}

flock($fp, LOCK_EX);

fputs($fp, $message);

fclose($fp);

return TRUE;

}

function strip_comment($address)

{

$comment = "\([^()]*\)";

while (ereg($comment, $address)) {

$address = ereg_replace($comment, "", $address);

}

return $address;

}

function get_address($address)

{

$address = ereg_replace("([ \t\r\n])+", "", $address);

$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);

return $address;

}

function smtp_debug($message)

{

if ($this->debug) {

echo $message;

}

}

}

?>

test.php

<?php

/*

这是一个测试程序!!!

请按照说明设置好以下的参数,以下是以tom.com的用户为例设置好的.

*/

require("sm.php");

##########################################

$smtpserver = "smtp.tom.com";//SMTP服务器

$smtpserverport =25;//SMTP服务器端口

$smtpusermail = "someone@tom.com";//SMTP服务器的用户邮箱

$smtpemailto = "jack@knowsky.com";//发送给谁

$smtpuser = "someone";//SMTP服务器的用户帐号

$smtppass = "someonepass";//SMTP服务器的用户密码

$mailsubject = "Test Subject";//邮件主题

$mailbody = "<h1>This is a test mail</h1>";//邮件内容

$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件

##########################################

$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.

$smtp->debug = TRUE;//是否显示发送的调试信息

$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

?>

PHP 相关文章推荐
无法载入 mcrypt 扩展,请检查 PHP 配置终极解决方案
Jul 18 PHP
PHP中数组的三种排序方法分享
May 07 PHP
PHP 使用MySQL管理Session的回调函数详解
Jun 21 PHP
php另类上传图片的方法(PHP用Socket上传图片)
Oct 30 PHP
php多功能图片处理类分享(php图片缩放类)
Mar 14 PHP
php json_encode()函数返回json数据实例代码
Oct 10 PHP
WebQQ最新登陆协议的用法
Dec 22 PHP
PHP获取文件扩展名的4种方法
Nov 24 PHP
PHP访问数据库集群的方法小结
Mar 14 PHP
PHPStudy下如何为Apache安装SSL证书的方法步骤
Jan 23 PHP
php链式操作的实现方式分析
Aug 12 PHP
基于PHP实现邮箱验证激活过程详解
Oct 28 PHP
PHP5.0正式发布 不完全兼容PHP4 新增多项功能
Oct 09 #PHP
动态网站web开发 PHP、ASP还是ASP.NET
Oct 09 #PHP
WINDOWS服务器安装多套PHP的另类解决方案
Oct 09 #PHP
在同一窗体中使用PHP来处理多个提交任务
Oct 09 #PHP
使用MaxMind 根据IP地址对访问者定位
Oct 09 #PHP
Zend公司全球首推PHP认证
Oct 09 #PHP
真正面向对象编程:PHP5.01发布
Oct 09 #PHP
You might like
php 变量定义方法
2009/06/14 PHP
PHP文件上传、客户端和服务器端加限制、抓取错误信息、完整步骤解析
2017/01/12 PHP
转一个日期输入控件,支持FF
2007/04/27 Javascript
js 页面输出值
2008/11/30 Javascript
jValidate 基于jQuery的表单验证插件
2009/12/12 Javascript
Js控制弹窗实现在任意分辨率下居中显示
2013/08/01 Javascript
JavaScript判断访问的来源是手机还是电脑,用的哪种浏览器
2013/12/12 Javascript
js中精确计算加法和减法示例
2014/03/28 Javascript
关于JavaScript作用域你想知道的一切
2016/02/04 Javascript
使用JavaScript为Kindeditor自定义按钮增加Audio标签
2016/03/18 Javascript
js cookie实现记住密码功能
2017/01/17 Javascript
学习使用Bootstrap栅格系统
2017/05/11 Javascript
js canvas实现适用于移动端的百分比仪表盘dashboard
2017/07/18 Javascript
jquery对table做排序操作的实例演示
2017/08/10 jQuery
使用JavaScript实现点击循环切换图片效果
2017/09/03 Javascript
js用类封装pop弹窗组件
2017/10/08 Javascript
jQuery获取随机颜色的实例代码
2018/05/21 jQuery
在Vue中使用CSS3实现内容无缝滚动的示例代码
2020/11/27 Vue.js
pyv8学习python和javascript变量进行交互
2013/12/04 Python
python获取外网ip地址的方法总结
2015/07/02 Python
python学习 流程控制语句详解
2016/06/01 Python
python3实现多线程聊天室
2018/12/12 Python
python基础知识(一)变量与简单数据类型详解
2019/04/17 Python
django中SMTP发送邮件配置详解
2019/07/19 Python
python同步两个文件夹下的内容
2019/08/29 Python
jupyter notebook实现显示行号
2020/04/13 Python
python 进制转换 int、bin、oct、hex的原理
2021/01/13 Python
CSS3中的Media Queries学习笔记
2016/05/23 HTML / CSS
浅谈html5 响应式布局
2014/12/24 HTML / CSS
捷克购买家具网站:JENA nábytek
2020/03/19 全球购物
《骆驼和羊》教学反思
2014/02/27 职场文书
2014年社区庆元旦活动方案
2014/03/08 职场文书
社团活动总结报告
2014/06/27 职场文书
关于拾金不昧的感谢信
2015/01/21 职场文书
2016年母亲节寄语
2015/12/04 职场文书
Python实现双向链表基本操作
2022/05/25 Python