免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6050 | 回复: 17

[SMTP] 我所用的一个SMTP类(包括预定义mail函数) [复制链接]

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
发表于 2005-10-11 14:07 |显示全部楼层
[SMTP] 我所用的一个SMTP类(包括预定义mail函数)


看看下面的,我所使用的,完全正常,已经用在超多场合。

我常用的一个Class.smtp.php

  1. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  2. $smtp->debug = true; //是否显示发送的调试信息
  3. $emailtype = "HTML"; //信件类型,文本:text;网页:HTML
  4. $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
复制代码



  1. // #########################################
  2. $smtpserver     = "localhost"; //SMTP服务器
  3. $smtpserverport = 25; //SMTP服务器端口
  4. $smtpusermail   = "postmaster@test.com"; //SMTP服务器的用户邮箱
  5. $smtpuser       = "postmaster@test.com"; //SMTP服务器的用户帐号
  6. $smtppass       = "password"; //SMTP服务器的用户密码

  7. // bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

  8. if (!function_exists('mail')) {

  9.         $smtp = new smtp($smtpserver, $smtpserverport, false, $smtpuser, $smtppass);
  10.                 //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  11.         $smtp->debug = false;
  12.                 //是否显示发送的调试信息

  13.         function mail($to, $subject, $message, $headers)
  14.         {
  15.                 global $smtp;

  16.                 $smtpemailto = $to;
  17.                 if (preg_match('/From:(.+?)\\n/sim', $headers, $regs)) {
  18.                         $smtpusermail = $regs[0];
  19.                 } else {
  20.                         $smtpusermail = $headers;
  21.                 }
  22.                 $mailsubject = $subject;
  23.                 $mailbody = $message;
  24.                 return $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $maibody, $mailtype);
  25.         }
  26. }



  27. class smtp {
  28.     /* Public Variables */

  29.     var $smtp_port;

  30.     var $time_out;

  31.     var $host_name;

  32.     var $log_file;

  33.     var $relay_host;

  34.     var $debug;

  35.     var $auth;

  36.     var $user;

  37.     var $pass;

  38.     /* Private Variables */
  39.     var $sock;

  40.     /* Constractor */

  41.     function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass)
  42.     {
  43.         $this->debug = false;

  44.         $this->smtp_port = $smtp_port;

  45.         $this->relay_host = $relay_host;

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

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

  48.         $this->user = $user;

  49.         $this->pass = $pass;

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

  52.         $this->sock = false;
  53.     }

  54.     /* Main Function */

  55.     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  56.     {
  57.         $mail_from = $this->get_address($this->strip_comment($from));

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

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

  60.         if ($mailtype == "HTML") {
  61.             $header .= "Content-Type:text/html\r\n";
  62.         }

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

  64.         if ($cc != "") {
  65.             $header .= "Cc: " . $cc . "\r\n";
  66.         }

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

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

  69.         $header .= $additional_headers;

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

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

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

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

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

  75.         if ($cc != "") {
  76.             $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  77.         }

  78.         if ($bcc != "") {
  79.             $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  80.         }

  81.         $sent = true;

  82.         foreach ($TO as $rcpt_to) {
  83.             $rcpt_to = $this->get_address($rcpt_to);

  84.             if (!$this->smtp_sockopen($rcpt_to)) {
  85.                 $this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");

  86.                 $sent = false;

  87.                 continue;
  88.             }

  89.             if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  90.                 $this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
  91.             } else {
  92.                 $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");

  93.                 $sent = false;
  94.             }

  95.             fclose($this->sock);

  96.             $this->log_write("Disconnected from remote host\n");
  97.         }

  98.         return $sent;
  99.     }

  100.     /* Private Functions */

  101.     function smtp_send($helo, $from, $to, $header, $body = "")
  102.     {
  103.         if (!$this->smtp_putcmd("HELO", $helo)) {
  104.             return $this->smtp_error("sending HELO command");
  105.         }
  106.         // auth
  107.         if ($this->auth) {
  108.             if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  109.                 return $this->smtp_error("sending HELO command");
  110.             }

  111.             if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  112.                 return $this->smtp_error("sending HELO command");
  113.             }
  114.         }

  115.         if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
  116.             return $this->smtp_error("sending MAIL FROM command");
  117.         }

  118.         if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
  119.             return $this->smtp_error("sending RCPT TO command");
  120.         }

  121.         if (!$this->smtp_putcmd("DATA")) {
  122.             return $this->smtp_error("sending DATA command");
  123.         }

  124.         if (!$this->smtp_message($header, $body)) {
  125.             return $this->smtp_error("sending message");
  126.         }

  127.         if (!$this->smtp_eom()) {
  128.             return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  129.         }

  130.         if (!$this->smtp_putcmd("QUIT")) {
  131.             return $this->smtp_error("sending QUIT command");
  132.         }

  133.         return true;
  134.     }

  135.     function smtp_sockopen($address)
  136.     {
  137.         if ($this->relay_host == "") {
  138.             return $this->smtp_sockopen_mx($address);
  139.         } else {
  140.             return $this->smtp_sockopen_relay();
  141.         }
  142.     }

  143.     function smtp_sockopen_relay()
  144.     {
  145.         $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");

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

  147.         if (!($this->sock && $this->smtp_ok())) {
  148.             $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");

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

  150.             return false;
  151.         }

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

  153.         return true;;
  154.     }

  155.     function smtp_sockopen_mx($address)
  156.     {
  157.         $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);

  158.         if (!@getmxrr($domain, $MXHOSTS)) {
  159.             $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");

  160.             return false;
  161.         }

  162.         foreach ($MXHOSTS as $host) {
  163.             $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");

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

  165.             if (!($this->sock && $this->smtp_ok())) {
  166.                 $this->log_write("Warning: Cannot connect to mx host " . $host . "\n");

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

  168.                 continue;
  169.             }

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

  171.             return true;
  172.         }

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

  174.         return false;
  175.     }

  176.     function smtp_message($header, $body)
  177.     {
  178.         fputs($this->sock, $header . "\r\n" . $body);

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

  180.         return true;
  181.     }

  182.     function smtp_eom()
  183.     {
  184.         fputs($this->sock, "\r\n.\r\n");

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

  186.         return $this->smtp_ok();
  187.     }

  188.     function smtp_ok()
  189.     {
  190.         $response = str_replace("\r\n", "", fgets($this->sock, 512));

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

  192.         if (!ereg("^[23]", $response)) {
  193.             fputs($this->sock, "QUIT\r\n");

  194.             fgets($this->sock, 512);

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

  196.             return false;
  197.         }

  198.         return true;
  199.     }

  200.     function smtp_putcmd($cmd, $arg = "")
  201.     {
  202.         if ($arg != "") {
  203.             if ($cmd == "") $cmd = $arg;

  204.             else $cmd = $cmd . " " . $arg;
  205.         }

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

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

  208.         return $this->smtp_ok();
  209.     }

  210.     function smtp_error($string)
  211.     {
  212.         $this->log_write("Error: Error occurred while " . $string . ".\n");

  213.         return false;
  214.     }

  215.     function log_write($message)
  216.     {
  217.         $this->smtp_debug($message);

  218.         if ($this->log_file == "") {
  219.             return true;
  220.         }

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

  222.         if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  223.             $this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");

  224.             return false;;
  225.         }

  226.         flock($fp, LOCK_EX);

  227.         fputs($fp, $message);

  228.         fclose($fp);

  229.         return true;
  230.     }

  231.     function strip_comment($address)
  232.     {
  233.         $comment = "\([^()]*\)";

  234.         while (ereg($comment, $address)) {
  235.             $address = ereg_replace($comment, "", $address);
  236.         }

  237.         return $address;
  238.     }

  239.     function get_address($address)
  240.     {
  241.         $address = ereg_replace("([ \t\r\n])+", "", $address);

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

  243.         return $address;
  244.     }

  245.     function smtp_debug($message)
  246.     {
  247.         if ($this->debug) {
  248.             echo $message . "
  249. ;";
  250.         }
  251.     }
  252. }


复制代码

[ 本帖最后由 HonestQiao 于 2007-3-19 10:23 编辑 ]

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2005-10-11 14:21 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

有个小问题,这样发邮件会导致页面反映非常慢,让用户失去耐心。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
发表于 2005-10-11 14:23 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

[quote]原帖由 "北京野狼"]有个小问题,这样发邮件会导致页面反映非常慢,让用户失去耐心。[/quote 发表:


不慢,速度很快。

而且,你还可以采用iframe+limit不限时发送啊。

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2005-10-11 14:41 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

在cgi里面完成发邮件会有慢的时候。
邮件的寻址,连接,发送过程很繁琐。
建议把邮件保存成文件,system(sendmail )
再放到后台发送

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
发表于 2005-10-11 15:02 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

原帖由 "北京野狼" 发表:
在cgi里面完成发邮件会有慢的时候。
邮件的寻址,连接,发送过程很繁琐。
建议把邮件保存成文件,system(sendmail )
再放到后台发送


并不是每个人都可以操作后台的。

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2005-10-11 15:05 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

原帖由 "HonestQiao" 发表:


并不是每个人都可以操作后台的。


你没理解。

$tmpfilename = tempnam("/ tmp", "dm");
  $fp = fopen($tmpfilename, "w");

  fwrite($fp, "From: "."\n");
  fwrite($fp, "Subject: "."\ n\ n");
  fwrite($fp, $message."\ n\ n");
  fclose($fp);

  $execstr="cat ".$tmpfilename." | /usr/lib/sendmail ".$mailto." >;/dev/null &";
  exec($execstr)

[ 本帖最后由 北京野狼 于 2008-9-12 16:39 编辑 ]

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
发表于 2005-10-11 15:10 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

原帖由 "北京野狼" 发表:


你没理解。

$tmpfilename = tempnam("/tmp", "dm";
  $fp = fopen($tmpfilename, "w";

  fwrite($fp, "From: "."\n";
  fwrite($fp, "Subject: "."\n\n";
  fwrite($fp, $message."\n\n";
  fclo..........


这就更不好了,不能直接使用exec的机会更多。

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2005-10-11 15:12 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

不能直接使用exec的机会更多???

这样做页面会立刻返回,不等邮件发送成功。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
发表于 2005-10-11 15:14 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

原帖由 "北京野狼" 发表:
不能直接使用exec的机会更多???

这样做页面会立刻返回,不等邮件发送成功。


作为安全设置,很多都把exec给直接关闭了。

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2005-10-11 15:20 |显示全部楼层

[SMTP] 我所用的一个SMTP类(包括预定义mail函数)

那就system.

只要服务器是自己的,完全可以设置什么可用,什么不可用,

如果是虚拟主机,那就只有采用你的程序了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP