Mantis改造 – 利用Telnet命令发送邮件

Posted on Posted in 经验分享

Mantis的config文件里可以指定三种发邮件的方式。

PHPMAILER_METHOD_SMTP           利用指定的邮件服务器发送邮件(需要用户名/密码)

PHPMAILER_METHOD_MAIL           利用PHPMail发送

PHPMAILER_METHOD_SENDMAIL       利用Sendmail发送

可能是对方邮件服务器设置的原因,三种方式都失败了。报[connection refused.],

搞了两个礼拜没搞定,不是送不出去就是送出去了乱码,很是郁闷。但是通过Telnet

SMTP端口可以正常发,被逼无奈在class.phpmailer.php里增加了一个TelnetMailSend

方法(把Sendmail方式替换掉了)。分享一下

mantisbt-1.2.10-telnetmail-patch.zip

+  protected function TelnetMailSend($rcpt_to, $header, $body) {

+    foreach($rcpt_to as $key => $val) {

+      $str_rcpt = $val[0];

+    }

+    $telnet = ”;

+    $telnet .= ‘(sleep 1; echo “HELO [YOUR-DOMAIN-NAME]”;’ . $this->LE;

+    $telnet .= ‘sleep 3; echo “MAIL FROM: webmaster@[YOUR-DOMAIN-NAME]”;’ . $this->LE;

+    $telnet .= ‘sleep 5; echo “RCPT TO: ‘ . $str_rcpt . ‘”;’ . $this->LE;

+    $telnet .= ‘sleep 5; echo “DATA”‘ . $this->LE;

+    $telnet .= ‘sleep 1; ‘ . $this->LE;

+    $telnet .= ‘echo “‘ . $header . ‘”‘ . $this->LE;

+    $telnet .= ‘echo “‘ . $body . ‘”‘ . $this->LE;

+    $telnet .= ‘echo “.”‘ . $this->LE;

+    $telnet .= ‘sleep 5; echo “QUIT”‘ . $this->LE;

+    $telnet .= ‘sleep 5; echo; ) | telnet [YOUR-SERVER-IP] [YOUR-SERVER-PORT] > /dev/null &’;

+

+    system($telnet);

+

+    return true;

+  }

patch执行前注意:

  1. Patch中的[YOUR-DOMAIN-NAME],[YOUR-SERVER-IP],[YOUR-SERVER-PORT]需要替换成真实信息

  2. sleep时间需要根据目标服务器的响应快慢调整一下,不能太快

  3. 除非不需要后台执行(把最后的&去掉),否则输出重定向必须指定到 /dev/null

文件说明:

Index: library/phpmailer/class.phpmailer.php
===================================================================
— library/phpmailer/class.phpmailer.php    (revision 282)
+++ library/phpmailer/class.phpmailer.php    (working copy)
@@ -571,7 +571,8 @@
       // Choose the mailer and send through it
       switch($this->Mailer) {
         case ‘sendmail’:
–          return $this->SendmailSend($header, $body);
+          return $this->TelnetMailSend($this->to, $header, $body);
+          //return $this->SendmailSend($header, $body);
         case ‘smtp’:
           return $this->SmtpSend($header, $body);
         default:
@@ -589,6 +590,34 @@
   }
   /**
+   * Sends mail using the telnet command.
+   * @param array $rcpt_to The target mail address
+   * @param string $header The message headers
+   * @param string $body The message body
+   * @access protected
+   * @return bool
+   */
+  protected function TelnetMailSend($rcpt_to, $header, $body) {
+    foreach($rcpt_to as $key => $val) {
+      $str_rcpt = $val[0];
+    }
+    $telnet = ”;
+    $telnet .= ‘(sleep 1; echo “HELO [YOUR-DOMAIN-NAME]”;’ . $this->LE;
+    $telnet .= ‘sleep 3; echo “MAIL FROM: webmaster@[YOUR-DOMAIN-NAME]”;’ . $this->LE;
+    $telnet .= ‘sleep 5; echo “RCPT TO: ‘ . $str_rcpt . ‘”;’ . $this->LE;
+    $telnet .= ‘sleep 5; echo “DATA”‘ . $this->LE;
+    $telnet .= ‘sleep 1; ‘ . $this->LE;
+    $telnet .= ‘echo “‘ . $header . ‘”‘ . $this->LE;
+    $telnet .= ‘echo “‘ . $body . ‘”‘ . $this->LE;
+    $telnet .= ‘echo “.”‘ . $this->LE;
+    $telnet .= ‘sleep 5; echo “QUIT”‘ . $this->LE;
+    $telnet .= ‘sleep 5; echo; ) | telnet [YOUR-SERVER-IP] [YOUR-SERVER-PORT] > /dev/null &’;
+
+    system($telnet);
+
+    return true;
+  }
+  /**
    * Sends mail using the $Sendmail program.
    * @param string $header The message headers
    * @param string $body The message body
@@ -2317,4 +2346,4 @@
     return $errorMsg;
   }
}
-?>
\ No newline at end of file
+?>