Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved SMTP script for sending mails. Now using the PEAR::Net_SMTP …

…class
  • Loading branch information...
commit 968bdc7c90f718560b84eb3b7e8e1d02cf4270f3 1 parent 15fee7b
Thomas B. authored September 30, 2005
3  UPGRADING
@@ -14,6 +14,7 @@ from versions 0.1-alpha and 0.1-20050811
14 14
   $rcmail_config['default_imap_folders'] = array('INBOX', 'Drafts', 'Sent', 'Junk', 'Trash');
15 15
   $rcmail_config['prefer_html'] = TRUE;
16 16
   $rcmail_config['prettydate'] = TRUE;
  17
+  $rcmail_config['smtp_port'] = 25;
17 18
 
18 19
 
19 20
 from version 0.1-20050820
@@ -21,4 +22,4 @@ from version 0.1-20050820
21 22
 - replace all files in folder /program/
22 23
 - add these line to /config/main.inc.php
23 24
   $rcmail_config['prettydate'] = TRUE;
24  
-
  25
+  $rcmail_config['smtp_port'] = 25;
5  config/main.inc.php.dist
@@ -15,7 +15,7 @@
15 15
 $rcmail_config = array();
16 16
 
17 17
 
18  
-// system error reporting: 1 = log; 2 = report (not implemented yet), 4 = show
  18
+// system error reporting: 1 = log; 2 = report (not implemented yet), 4 = show, 8 = trace
19 19
 $rcmail_config['debug_level'] = 5;
20 20
 
21 21
 // automatically create a new user when log-in the first time
@@ -31,6 +31,9 @@ $rcmail_config['default_host'] = '';
31 31
 // if left blank, the PHP mail() function is used
32 32
 $rcmail_config['smtp_server'] = '';
33 33
 
  34
+// SMTP port (default is 25)
  35
+$rcmail_config['smtp_port'] = 25;
  36
+
34 37
 // SMTP username (if required)
35 38
 $rcmail_config['smtp_user'] = '';
36 39
 
10  logs/errors
... ...
@@ -0,0 +1,10 @@
  1
+[30-Sep-2005 23:12:17 +0200] SMTP Error: Connection failed: SMTP: Failed to send data
  2
+ in /www/roundcube/webmail/program/steps/mail/sendmail.inc on line 198
  3
+[30-Sep-2005 23:18:44 +0200] SMTP Error: Connection failed: SMTP: Failed to send data
  4
+ in /www/roundcube/webmail/program/steps/mail/sendmail.inc on line 198
  5
+[30-Sep-2005 23:26:41 +0200] SMTP Error: Connection failed: SMTP: Failed to send data
  6
+ in /www/roundcube/webmail/program/steps/mail/sendmail.inc on line 198
  7
+[30-Sep-2005 23:31:29 +0200] SMTP Error: Connection failed: SMTP: Failed to send data
  8
+ in /www/roundcube/webmail/program/steps/mail/sendmail.inc on line 198
  9
+[30-Sep-2005 23:31:56 +0200] SMTP Error: Connection failed: SMTP: Failed to send data
  10
+ in /www/roundcube/webmail/program/steps/mail/sendmail.inc on line 198
8  logs/sendmail
... ...
@@ -0,0 +1,8 @@
  1
+[30-Sep-2005 23:13:28 +0200] User: 2; Message for Thomas Bruderli <thomas@brotherli.ch>; Subject: Testmail form RoundCube I
  2
+[30-Sep-2005 23:28:29 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>; Subject: Testmail form RoundCube II
  3
+[30-Sep-2005 23:33:23 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>; Subject: Testmail form RoundCube III
  4
+[30-Sep-2005 23:34:23 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>; Subject: Testmail form RoundCube IV
  5
+[30-Sep-2005 23:44:40 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>, tb@localhost; Subject: Testmail V
  6
+[30-Sep-2005 23:45:38 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>, roundcube@gmail.com; Subject: Testmail VI
  7
+[30-Sep-2005 23:54:05 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>; Subject: Testmail VII
  8
+[30-Sep-2005 23:55:08 +0200] User: 2; Message for Thomas Brüderli <thomas@brotherli.ch>; Subject: Testmail VII
324  program/include/rcube_smtp.inc
... ...
@@ -0,0 +1,324 @@
  1
+<?
  2
+
  3
+/*
  4
+ +-----------------------------------------------------------------------+
  5
+ | program/include/rcube_smtp.inc                                        |
  6
+ |                                                                       |
  7
+ | This file is part of the RoundCube Webmail client                     |
  8
+ | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
  9
+ | Licensed under the GNU GPL                                            |
  10
+ |                                                                       |
  11
+ | PURPOSE:                                                              |
  12
+ |   Provide SMTP functionality using socket connections                 |
  13
+ |                                                                       |
  14
+ +-----------------------------------------------------------------------+
  15
+ | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  16
+ +-----------------------------------------------------------------------+
  17
+
  18
+ $Id$
  19
+
  20
+*/
  21
+
  22
+
  23
+// include required PEAR classes
  24
+require_once('Net/SMTP.php');
  25
+
  26
+
  27
+// define headers delimiter
  28
+define('SMTP_MIME_CRLF', "\r\n");
  29
+
  30
+$SMTP_CONN = null;
  31
+
  32
+/**
  33
+ * Function for sending mail using SMTP.
  34
+ *
  35
+ * @param string Sender e-Mail address
  36
+ *
  37
+ * @param mixed  Either a comma-seperated list of recipients
  38
+ *               (RFC822 compliant), or an array of recipients,
  39
+ *               each RFC822 valid. This may contain recipients not
  40
+ *               specified in the headers, for Bcc:, resending
  41
+ *               messages, etc.
  42
+ *
  43
+ * @param mixed  The message headers to send with the mail
  44
+ *               Either as an associative array or a finally
  45
+ *               formatted string
  46
+ *
  47
+ * @param string The full text of the message body, including any Mime parts, etc.
  48
+ *
  49
+ * @return bool  Returns TRUE on success, or FALSE on error
  50
+ * @access public
  51
+ */
  52
+function smtp_mail($from, $recipients, $headers, $body)
  53
+  {
  54
+  global $SMTP_CONN, $CONFIG, $SMTP_ERROR;
  55
+  $smtp_timeout = null;
  56
+  $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
  57
+  
  58
+  // create Net_SMTP object and connect to server
  59
+  if (!is_object($smtp_conn))
  60
+    {
  61
+    $SMTP_CONN = new Net_SMTP($CONFIG['smtp_server'], $smtp_port, 'localhost');
  62
+
  63
+    // set debugging
  64
+    if ($CONFIG['debug_level'] & 8)
  65
+      $SMTP_CONN->setDebug(TRUE);
  66
+
  67
+
  68
+    // try to connect to server and exit on failure
  69
+    if (PEAR::isError($SMTP_CONN->connect($smtp_timeout)))
  70
+      {
  71
+      $SMTP_CONN = null;
  72
+      $SMTP_ERROR .= "Connection failed\n";
  73
+      return FALSE;
  74
+      }
  75
+
  76
+
  77
+    // attempt to authenticate to the SMTP server
  78
+    if ($CONFIG['smtp_user'] && $CONFIG['smtp_pass'])
  79
+      {
  80
+      if (PEAR::isError($SMTP_CONN->auth($CONFIG['smtp_user'], $CONFIG['smtp_pass'])))
  81
+        {
  82
+        smtp_reset();
  83
+        $SMTP_ERROR .= "authentication failure\n";
  84
+        return FALSE;
  85
+        }
  86
+      }
  87
+    }
  88
+
  89
+
  90
+  // prepare message headers as string
  91
+  if (is_array($headers))
  92
+    {
  93
+    $headerElements = smtp_prepare_headers($headers);
  94
+    if (!$headerElements)
  95
+      {
  96
+      smtp_reset();
  97
+      return FALSE;
  98
+      }
  99
+
  100
+    list($from, $text_headers) = $headerElements;
  101
+    }
  102
+  else if (is_string($headers))
  103
+    $text_headers = $headers;
  104
+  else
  105
+    {
  106
+    smtp_reset();
  107
+    $SMTP_ERROR .= "Invalid message headers\n";
  108
+    return FALSE;
  109
+    }
  110
+
  111
+  // exit if no from address is given
  112
+  if (!isset($from))
  113
+    {
  114
+    smtp_reset();
  115
+    $SMTP_ERROR .= "No From address has been provided\n";
  116
+    return FALSE;
  117
+    }
  118
+
  119
+
  120
+  // set From: address
  121
+  if (PEAR::isError($SMTP_CONN->mailFrom($from)))
  122
+    {
  123
+    smtp_reset();
  124
+    $SMTP_ERROR .= "Failed to set sender '$from'\n";
  125
+    return FALSE;
  126
+    }
  127
+
  128
+
  129
+  // prepare list of recipients
  130
+  $recipients = smtp_parse_rfc822($recipients);
  131
+  if (PEAR::isError($recipients))
  132
+    {
  133
+    smtp_reset();
  134
+    return FALSE;
  135
+    }
  136
+
  137
+
  138
+  // set mail recipients
  139
+  foreach ($recipients as $recipient)
  140
+    {
  141
+    if (PEAR::isError($SMTP_CONN->rcptTo($recipient)))
  142
+      {
  143
+      smtp_reset();
  144
+      $SMTP_ERROR .= "Failed to add recipient '$recipient'\n";
  145
+      return FALSE;
  146
+      }
  147
+    }
  148
+
  149
+
  150
+  // Send the message's headers and the body as SMTP data.
  151
+  if (PEAR::isError($SMTP_CONN->data("$text_headers\r\n$body")))
  152
+    {
  153
+    smtp_reset();
  154
+    $SMTP_ERROR .= "Failed to send data\n";
  155
+    return FALSE;
  156
+    }
  157
+
  158
+
  159
+  return TRUE;
  160
+  }
  161
+
  162
+
  163
+
  164
+/**
  165
+ * Reset the global SMTP connection
  166
+ * @access public
  167
+ */
  168
+function smtp_reset()
  169
+  {
  170
+  global $SMTP_CONN;
  171
+
  172
+  if (is_object($SMTP_CONN))
  173
+    {
  174
+    $SMTP_CONN->rset();
  175
+    smtp_disconnect();
  176
+    }
  177
+  }
  178
+
  179
+
  180
+
  181
+/**
  182
+ * Disconnect the global SMTP connection and destroy object
  183
+ * @access public
  184
+ */
  185
+function smtp_disconnect()
  186
+  {
  187
+  global $SMTP_CONN;
  188
+
  189
+  if (is_object($SMTP_CONN))
  190
+    {
  191
+    $SMTP_CONN->disconnect();
  192
+    $SMTP_CONN = null;
  193
+    }
  194
+  }
  195
+
  196
+
  197
+/**
  198
+ * Take an array of mail headers and return a string containing
  199
+ * text usable in sending a message.
  200
+ *
  201
+ * @param array $headers The array of headers to prepare, in an associative
  202
+ *              array, where the array key is the header name (ie,
  203
+ *              'Subject'), and the array value is the header
  204
+ *              value (ie, 'test'). The header produced from those
  205
+ *              values would be 'Subject: test'.
  206
+ *
  207
+ * @return mixed Returns false if it encounters a bad address,
  208
+ *               otherwise returns an array containing two
  209
+ *               elements: Any From: address found in the headers,
  210
+ *               and the plain text version of the headers.
  211
+ * @access private
  212
+ */
  213
+function smtp_prepare_headers($headers)
  214
+  {
  215
+  $lines = array();
  216
+  $from = null;
  217
+
  218
+  foreach ($headers as $key => $value)
  219
+    {
  220
+    if (strcasecmp($key, 'From') === 0)
  221
+      {
  222
+      $addresses = smtp_parse_rfc822($value);
  223
+
  224
+      if (is_array($addresses))
  225
+        $from = $addresses[0];
  226
+
  227
+      // Reject envelope From: addresses with spaces.
  228
+      if (strstr($from, ' '))
  229
+        return FALSE;
  230
+
  231
+
  232
+      $lines[] = $key . ': ' . $value;
  233
+      }
  234
+    else if (strcasecmp($key, 'Received') === 0)
  235
+      {
  236
+      $received = array();
  237
+      if (is_array($value))
  238
+        {
  239
+        foreach ($value as $line)
  240
+          $received[] = $key . ': ' . $line;
  241
+        }
  242
+      else
  243
+        {
  244
+        $received[] = $key . ': ' . $value;
  245
+        }
  246
+
  247
+      // Put Received: headers at the top.  Spam detectors often
  248
+      // flag messages with Received: headers after the Subject:
  249
+      // as spam.
  250
+      $lines = array_merge($received, $lines);
  251
+      }
  252
+
  253
+    else
  254
+      {
  255
+      // If $value is an array (i.e., a list of addresses), convert
  256
+      // it to a comma-delimited string of its elements (addresses).
  257
+      if (is_array($value))
  258
+        $value = implode(', ', $value);
  259
+
  260
+      $lines[] = $key . ': ' . $value;
  261
+      }
  262
+    }
  263
+
  264
+  return array($from, join(SMTP_MIME_CRLF, $lines) . SMTP_MIME_CRLF);
  265
+  }
  266
+
  267
+
  268
+
  269
+/**
  270
+ * Take a set of recipients and parse them, returning an array of
  271
+ * bare addresses (forward paths) that can be passed to sendmail
  272
+ * or an smtp server with the rcpt to: command.
  273
+ *
  274
+ * @param mixed Either a comma-seperated list of recipients
  275
+ *              (RFC822 compliant), or an array of recipients,
  276
+ *              each RFC822 valid.
  277
+ *
  278
+ * @return array An array of forward paths (bare addresses).
  279
+ * @access private
  280
+ */
  281
+function smtp_parse_rfc822($recipients)
  282
+  {
  283
+  // if we're passed an array, assume addresses are valid and implode them before parsing.
  284
+  if (is_array($recipients))
  285
+    $recipients = implode(', ', $recipients);
  286
+    
  287
+  $addresses = array();
  288
+  $recipients = smtp_explode_quoted_str(",", $recipients);
  289
+  
  290
+  reset($recipients);
  291
+  while (list($k, $recipient) = each($recipients))
  292
+    {
  293
+	$a = explode(" ", $recipient);
  294
+	while (list($k2, $word) = each($a))
  295
+	  {
  296
+      if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false))
  297
+        {
  298
+        $word = ereg_replace('^<|>$', '', trim($word));
  299
+        if (in_array($word, $addresses)===false)
  300
+          array_push($addresses, $word);
  301
+        }
  302
+      }
  303
+    }
  304
+  return $addresses;
  305
+  }
  306
+
  307
+
  308
+function smtp_explode_quoted_str($delimiter, $string)
  309
+  {
  310
+  $quotes=explode("\"", $string);
  311
+  while ( list($key, $val) = each($quotes))
  312
+    if (($key % 2) == 1) 
  313
+      $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
  314
+    $string=implode("\"", $quotes);
  315
+	
  316
+    $result=explode($delimiter, $string);
  317
+    while (list($key, $val) = each($result))
  318
+      $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
  319
+
  320
+  return $result;
  321
+  }	
  322
+
  323
+
  324
+?>
2  program/lib/Mail/mime.php
@@ -119,7 +119,7 @@ function Mail_mime($crlf = "\r\n")
119 119
         $this->_build_params = array(
120 120
                                      'text_encoding' => '7bit',
121 121
                                      'html_encoding' => 'quoted-printable',
122  
-                                     'header_encoding' => 'quoted-printable',
  122
+                                     'head_encoding' => 'quoted-printable',
123 123
                                      '7bit_wrap'     => 998,
124 124
                                      'html_charset'  => 'ISO-8859-1',
125 125
                                      'text_charset'  => 'ISO-8859-1',
351  program/lib/smtp.inc
... ...
@@ -1,351 +0,0 @@
1  
-<?php 
2  
-/////////////////////////////////////////////////////////
3  
-//	
4  
-//	include/smtp.inc
5  
-//
6  
-//	(C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
7  
-//
8  
-//		This file is part of IlohaMail.
9  
-//		IlohaMail is free software released under the GPL 
10  
-//		license.  See enclosed file COPYING for details,
11  
-//		or see http://www.fsf.org/copyleft/gpl.html
12  
-//
13  
-/////////////////////////////////////////////////////////
14  
-
15  
-/********************************************************
16  
-
17  
-	AUTHOR: Ryo Chijiiwa <ryo@ilohamail.org>
18  
-	FILE:  include/smtp.php
19  
-	PURPOSE:
20  
-		Provide SMTP functionality using pure PHP.
21  
-	PRE-CONDITIONS:
22  
-		The functions here require a SMTP server configured to allow relaying.
23  
-	POST-CONDITIONS:
24  
-		The following global variables are returned:
25  
-		$smtp_errornum - error number, 0 if successful
26  
-		$smtp_error - error message(s)
27  
-		$SMTP_TYPE - Optional
28  
-	COMMENTS:
29  
-		The optional $smtp_message_file can be used for sending extremely large
30  
-		messages.  Storing large messages in a variable will consume whatever
31  
-		amount of memory required, which may be more than is available if dealing
32  
-		with messages with large attachments.  By storing them in a file, it becomes
33  
-		possible to read/send bits at a time, drastically reducing memory useage.
34  
-		
35  
-		This library only provides bare-bones SMTP functionality.  It is up to the
36  
-		parent code to form valid RFC822 (MIME) messages.
37  
-			
38  
-********************************************************/
39  
-	
40  
-	//set some global constants
41  
-	if (strcasecmp($SMTP_TYPE, "courier")==0){
42  
-		$SMTP_REPLY_DELIM = "-";
43  
-		$SMTP_DATA_REPLY = 250;
44  
-	}else{
45  
-		$SMTP_REPLY_DELIM = " ";
46  
-		$SMTP_DATA_REPLY = 354;
47  
-	}
48  
-
49  
-	/* fgets replacement that's multi-line aware */
50  
-	function smtp_get_response($fp, $len){
51  
-		$end = false;
52  
-		do{
53  
-			$line = chop(fgets($fp, 5120));
54  
-			// echo "SMTP:".$line."<br>\n"; flush();
55  
-			if ((strlen($line)==3) || ($line[3]==' ')) $end = true;
56  
-		}while(!$end);
57  
-
58  
-		return $line;
59  
-	}
60  
-
61  
-
62  
-	function smtp_check_reply($reply){
63  
-		global $smtp_error;
64  
-		global $SMTP_REPLY_DELIM;
65  
-		
66  
-		$a = explode($SMTP_REPLY_DELIM, chop($reply));
67  
-
68  
-		if (count($a) >= 1){
69  
-
70  
-			if ($a[0]==250||$a[0]==354) return true;
71  
-			else{
72  
-				$smtp_error .= $reply."\n";
73  
-			}
74  
-		}else{
75  
-			$smtp_error .= "Invalid SMTP response line: $reply\n";
76  
-		}
77  
-		
78  
-		return false;
79  
-	}
80  
-	
81  
-	
82  
-	function smtp_split($str){
83  
-		$result = array();
84  
-		$pos = strpos($str, " ");
85  
-		if ($pos===false){
86  
-			$result[0] = $str;
87  
-		}else{
88  
-			$result[0] = substr($str, 0, $pos);
89  
-			$result[1] = substr($str, $pos+1);
90  
-		}
91  
-		
92  
-		return $result;
93  
-	}
94  
-	
95  
-	
96  
-	function smtp_ehlo(&$conn, $host){
97  
-		$result = "";
98  
-		fputs($conn, "EHLO $host\r\n");
99  
-		//echo "Sent: EHLO $host\n"; flush();
100  
-		do{
101  
-			$line = fgets($conn, 2048);
102  
-			//echo "Got: $line"; flush();
103  
-			$a = explode(" ", $line);
104  
-			if ($a[0]=="250-AUTH") $result .= substr($line, 9);
105  
-		}while(!is_numeric($a[0]));
106  
-		
107  
-		if ($a[0]==250) return $result;
108  
-		else return $a[0];
109  
-		
110  
-	}
111  
-	
112  
-	
113  
-	function smtp_auth_login(&$conn, $user, $pass){
114  
-		$auth["username"] = base64_encode($user);
115  
-		$auth["password"] = base64_encode($pass);
116  
-	
117  
-		fputs($conn, "AUTH LOGIN\r\n");
118  
-		
119  
-		//echo "Sent: AUTH LOGIN\n"; flush();
120  
-		
121  
-		//get first line
122  
-		$line = smtp_get_response($conn, 1024);
123  
-		//echo "AUTH_LOGIN << $line"; flush();
124  
-		$parts = smtp_split($line);
125  
-		//valid reply?
126  
-		if (($parts[0]!=334) || (empty($parts[1]))) return false;
127  
-		//send data
128  
-		$prompt = eregi_replace("[^a-z]", "", strtolower(base64_decode($parts[1])));
129  
-		fputs($conn, $auth[$prompt]."\r\n");
130  
-		//echo "AUT_LOGIN >> ".$auth[$prompt]."\n"; flush();
131  
-
132  
-		//get second line
133  
-		$line = smtp_get_response($conn, 1024);
134  
-		//echo "AUTH_LOGIN << $line"; flush();
135  
-		$parts = smtp_split($line);
136  
-		//valid reply?
137  
-		if (($parts[0]!=334) || (empty($parts[1]))) return false;
138  
-		$prompt = eregi_replace("[^a-z]", "", strtolower(base64_decode($parts[1])));
139  
-		fputs($conn, $auth[$prompt]."\r\n");
140  
-		//echo "AUT_LOGIN >> ".$auth[$prompt]."\n"; flush();
141  
-
142  
-		$line = smtp_get_response($conn, 1024);
143  
-		//echo "AUTH_LOGIN << $line"; flush();
144  
-		$parts = smtp_split($line);
145  
-		return ($parts[0]==235);
146  
-	}
147  
-	
148  
-	
149  
-	function smtp_connect($host, $port, $user, $pass){
150  
-		global $smtp_errornum;
151  
-		global $smtp_error;
152  
-
153  
-		//auth user?
154  
-		global $SMTP_USER, $SMTP_PASSWORD;
155  
-		if ((!empty($SMTP_USER)) && (!empty($SMTP_PASSWORD))){
156  
-			$user = $SMTP_USER;
157  
-			$pass = $SMTP_PASSWORD;
158  
-		}
159  
-		
160  
-		// echo "User: $user<br>\n";
161  
-		
162  
-		//figure out auth mode
163  
-		global $AUTH_MODE;
164  
-		$auth_mode = $AUTH_MODE["smtp"];
165  
-		if (empty($auth_mode)) $auth_mode = "none";
166  
-		if (empty($user) || empty($pass)) $auth_mode = "none";
167  
-		
168  
-		// echo "authmode: $auth_mode<br>\n"; flush();
169  
-		
170  
-		//initialize defaults
171  
-		if (empty($host)) $host = "localhost";
172  
-		if (empty($port)) $port = 25;
173  
-		
174  
-		// echo "Connecting to $host:$port<br>\n"; flush();
175  
-		
176  
-		//connect to SMTP server
177  
-		$conn = fsockopen($host, $port);
178  
-	
179  
-		if (!$conn){
180  
-			//echo "fsockopen failed\n";
181  
-			$smtp_error = "Couldn't connect to $host:$port<br>\n";
182  
-			return false;
183  
-		}
184  
-		
185  
-		//read greeting
186  
-		$greeting = smtp_get_response($conn, 1024);
187  
-			
188  
-		// echo "Connected: $greeting<br>\n"; flush();
189  
-
190  
-		if (($auth_mode=="check") || ($auth_mode=="auth")){
191  
-			// echo "Trying EHLO<br>\n"; flush();
192  
-			$auth_modes = smtp_ehlo($conn, $_SERVER["SERVER_NAME"]);
193  
-			// echo "smtp_ehlo returned: $auth_modes<br>\n"; flush();
194  
-			if ($auth_modes===false){
195  
-				$smtp_error = "EHLO failed\n";
196  
-				$conn = false;
197  
-			}else if (stristr($auth_modes, "LOGIN")!==false){
198  
-				echo "trying AUTH LOGIN\n"; flush();
199  
-				if (!smtp_auth_login($conn, $user, $pass)){
200  
-					//echo "CONN: AUTH_LOGIN failed\n"; flush();
201  
-					$conn = false;
202  
-				}
203  
-				//echo "Conn after LOGIN: $conn<br>\n"; flush();
204  
-			}
205  
-		}else{
206  
-			fputs($conn, "HELO ".$_SERVER["SERVER_NAME"]."\r\n");
207  
-			$line = smtp_get_response($conn, 1024);
208  
-			if (!smtp_check_reply($line)){
209  
-				$conn = false;
210  
-				$smtp_error .= $line."\n";
211  
-			}
212  
-			// echo "after HELO: $conn<br>\n"; flush();
213  
-		}
214  
-	
215  
-		return $conn;
216  
-	}
217  
-	
218  
-	
219  
-	function smtp_close($conn){
220  
-		fclose($conn);
221  
-	}
222  
-
223  
-
224  
-	function smtp_mail($conn, $from, $recipients, $message, $is_file){
225  
-		global $smtp_errornum;
226  
-		global $smtp_error;
227  
-		global $SMTP_DATA_REPLY;
228  
-		
229  
-		//check recipients and sender addresses
230  
-		if ((count($recipients)==0) || (!is_array($recipients))){
231  
-			$smtp_errornum = -1;
232  
-			$smtp_error .= "Recipients list is empty\n";
233  
-			return false;
234  
-		}
235  
-		if (empty($from)){
236  
-			$smtp_errornum = -2;
237  
-			$smtp_error .= "From address unspecified\n";
238  
-			return false;
239  
-		}
240  
-		
241  
-		if (!$conn){			
242  
-			$smtp_errornum = -3;
243  
-			$smtp_error .= "Invalid connection\n";
244  
-		}
245  
-
246  
-		if (!ereg("^<", $from)) $from = "<".$from;
247  
-		if (!ereg(">$", $from)) $from = $from.">";
248  
-		
249  
-		//send MAIL FROM command
250  
-		$command = "MAIL FROM: $from\r\n";
251  
-		// echo nl2br(htmlspecialchars($command));
252  
-		fputs($conn, $command);
253  
-
254  
-		if (smtp_check_reply(smtp_get_response($conn, 1024))){
255  
-			//send RCPT TO commands, count valid recipients
256  
-			$num_recipients = 0;	
257  
-			while ( list($k, $recipient) = each($recipients) ){
258  
-				$command = "RCPT TO: $recipient\r\n";
259  
-				fputs($conn, $command);
260  
-				$reply = smtp_check_reply(smtp_get_response($conn, 1024));
261  
-				if ($reply) $num_recipients++;
262  
-				else $smtp_error .= $reply."\n";
263  
-			}
264  
-			
265  
-			//error out if no valid recipiets
266  
-			if ($num_recipients == 0){
267  
-				$smtp_errornum = -1;
268  
-				$smtp_error .= "No valid recipients\n";
269  
-				return false;
270  
-			}
271  
-			
272  
-			//send DATA command
273  
-			fputs($conn, "DATA\r\n");
274  
-			$reply = chop(smtp_get_response($conn, 1024));
275  
-			$a = explode(" ", $reply);
276  
-			
277  
-			//error out if DATA command ill received
278  
-			if ($a[0]!=$SMTP_DATA_REPLY){
279  
-				$smtp_errornum = -4;
280  
-				$smtp_error .= $reply;
281  
-				return false;	
282  
-			}
283  
-			//send data
284  
-			if ($is_file){
285  
-				//if message file, open file
286  
-				$fp = false;
287  
-				if (file_exists(realpath($message))) $fp = fopen($message, "rb");
288  
-				if (!$fp)
289  
-				{ 
290  
-					$smtp_errornum = -4;
291  
-					$smtp_error .= "Invlid message file\n";
292  
-					return false;
293  
-				}
294  
-				
295  
-				//send file
296  
-				while(!feof($fp)){
297  
-					$buffer = chop(fgets($fp, 4096), "\r\n");
298  
-					fputs($conn, $buffer."\r\n");
299  
-				}
300  
-				fclose($fp);
301  
-				fputs($conn, "\r\n.\r\n");
302  
-				
303  
-				return smtp_check_reply(smtp_get_response($conn, 1024));
304  
-			}else{
305  
-				//else, send message
306  
-				$message = str_replace("\r\n", "\n", $message);
307  
-				$message = str_replace("\n", "\r\n", $message);
308  
-				$message = str_replace("\r\n.\r\n", "\r\n..\r\n", $message);
309  
-				fputs($conn, $message);
310  
-				fputs($conn, "\r\n.\r\n");
311  
-				
312  
-				return smtp_check_reply(smtp_get_response($conn, 1024));
313  
-			}
314  
-		}
315  
-		
316  
-		return false;
317  
-	}
318  
-
319  
-	
320  
-	function smtp_ExplodeQuotedString($delimiter, $string){
321  
-		$quotes=explode("\"", $string);
322  
-		while ( list($key, $val) = each($quotes))
323  
-			if (($key % 2) == 1) 
324  
-				$quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
325  
-		$string=implode("\"", $quotes);
326  
-	
327  
-		$result=explode($delimiter, $string);
328  
-		while ( list($key, $val) = each($result) )
329  
-			$result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
330  
-	
331  
-		return $result;
332  
-	}	
333  
-	
334  
-	
335  
-	function smtp_expand($str){
336  
-		$addresses = array();
337  
-		$recipients = smtp_ExplodeQuotedString(",", $str);
338  
-		reset($recipients);
339  
-		while ( list($k, $recipient) = each($recipients) ){
340  
-			$a = explode(" ", $recipient);
341  
-			while ( list($k2, $word) = each($a) ){
342  
-				if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false)){
343  
-					if (!ereg("^<", $word)) $word = "<".$word;
344  
-					if (!ereg(">$", $word)) $word = $word.">";
345  
-					if (in_array($word, $addresses)===false) array_push($addresses, $word);
346  
-				}
347  
-			}
348  
-		}
349  
-		return $addresses;
350  
-	}
351  
-?>
37  program/steps/mail/sendmail.inc
@@ -21,7 +21,8 @@
21 21
 */
22 22
 
23 23
 
24  
-require_once('lib/smtp.inc');
  24
+//require_once('lib/smtp.inc');
  25
+require_once('include/rcube_smtp.inc');
25 26
 require_once('Mail/mime.php');
26 27
 
27 28
 
@@ -155,30 +156,28 @@ $msg_subject = $headers['Subject'];
155 156
 // send thru SMTP server using cusotm SMTP library
156 157
 if ($CONFIG['smtp_server'])
157 158
   {
158  
-  // connect to SMTP server
159  
-  $smtp_conn = smtp_connect($CONFIG['smtp_server'], '25', $CONFIG['smtp_user'], $CONFIG['smtp_pass']);
160  
-	
161  
-  if ($smtp_conn)
162  
-    {
163  
-    // generate list of recipients
164  
-    $recipients = $mailto.', '.$headers['Cc'].', '.$headers['Bcc'];
165  
-    $a_recipients = smtp_expand($recipients);
166  
-
167  
-    // generate message headers
168  
-    $header_str = $MAIL_MIME->txtHeaders($headers);
169  
-	
170  
-    // send message
171  
-    $sent = smtp_mail($smtp_conn, $from, $a_recipients, $header_str."\r\n".$msg_body, FALSE);
172  
-	}
173  
-	
  159
+  // generate list of recipients
  160
+  $a_recipients = array($mailto);
  161
+
  162
+  if (strlen($headers['Cc']))
  163
+    $a_recipients[] = $headers['Cc'];
  164
+  if (strlen($headers['Bcc']))
  165
+    $a_recipients[] = $headers['Bcc'];
  166
+
  167
+  // generate message headers
  168
+  $header_str = $MAIL_MIME->txtHeaders($headers);
  169
+
  170
+  // send message
  171
+  $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body);
  172
+
174 173
   // log error
175  
-  if (!$smtp_conn || !$sent)
  174
+  if (!$sent)
176 175
     {
177 176
     raise_error(array('code' => 800,
178 177
                       'type' => 'smtp',
179 178
                       'line' => __LINE__,
180 179
                       'file' => __FILE__,
181  
-                      'message' => "Connection failed: $smtp_error"), TRUE, FALSE);
  180
+                      'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE);
182 181
     }
183 182
   }
184 183
 

0 notes on commit 968bdc7

Please sign in to comment.
Something went wrong with that request. Please try again.