source: trunk/include/class_smtp_mail.inc.php @ 2299

Last change on this file since 2299 was 2299, checked in by plg, 16 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24// These function were originally a part of the punBB.
25
26class smtp_mail
27{
28  var $socket;
29  var $no_error;
30  var $host;
31  var $user;
32  var $password;
33  var $email_webmaster;
34
35  function smtp_mail($host, $user, $password, $email_webmaster)
36  {
37    $this->host = $host;
38    $this->user = $user;
39    $this->password = $password;
40    $this->email_webmaster = $email_webmaster;
41  }
42
43  // Adaptation of server_parse
44  function server_parse($expected_response)
45  {
46    if ($this->no_error)
47    {
48      $server_response = '';
49      while (substr($server_response, 3, 1) != ' ')
50      {
51        if (!($server_response = fgets($this->socket, 256)))
52        {
53          trigger_error('Couldn\'t get mail server response codes.', E_USER_WARNING);
54          $this->no_error = false;
55        }
56      }
57    }
58
59    if ($this->no_error)
60    {
61      if (!(substr($server_response, 0, 3) == $expected_response))
62      {
63        trigger_error('Unable to send e-mail. Error message reported by the SMTP server: "'.$server_response.'"', E_USER_WARNING);
64        $this->no_error = false;
65      }
66    }
67    return $this->no_error;
68  }
69
70  function server_write($s)
71  {
72    $this->no_error = $this->no_error && (fwrite($this->socket, $s) !== false);
73    return $this->no_error;
74  }
75
76  // Adaptation of pun_mail
77  function mail($to, $subject, $message, $headers = '')
78  {
79    $this->no_error = true;
80
81    $recipients = explode(',', $to);
82
83    // Are we using port 25 or a custom port?
84    if (strpos($this->host, ':') !== false)
85    {
86      list($smtp_host, $smtp_port) = explode(':', $this->host);
87    }
88    else
89    {
90      $smtp_host = $this->host;
91      $smtp_port = 25;
92    }
93
94    if ($this->socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15))
95    {
96      $this->server_parse('220');
97
98      if (!empty($this->user) && !empty($this->password))
99      {
100        $this->server_write('EHLO '.$smtp_host."\r\n");
101        $this->server_parse('250');
102
103        $this->server_write('AUTH LOGIN'."\r\n");
104        $this->server_parse('334');
105
106        $this->server_write(base64_encode($this->user)."\r\n");
107        $this->no_error = $this->no_error && $this->no_error = $this->server_parse('334');
108
109        $this->server_write(base64_encode($this->password)."\r\n");
110        $this->server_parse('235');
111      }
112      else
113      {
114        $this->server_write('HELO '.$smtp_host."\r\n");
115        $this->server_parse('250');
116      }
117
118      $this->server_write('MAIL FROM: <'.$this->email_webmaster.'>'."\r\n");
119      $this->server_parse('250');
120
121      if (preg_match('/^\s*to\s*:.*/mi', $headers) === 0)
122      {
123        $to_header = 'To: '.implode(',', array_map(create_function('$email','return "<".$email.">";'), $recipients));
124      }
125      else
126      {
127        $to_header = '';
128      }
129
130      @reset($recipients);
131      while (list(, $email) = @each($recipients))
132      {
133        $this->server_write('RCPT TO: <'.$email.'>'."\r\n");
134        $this->server_parse('250');
135      }
136
137      $this->server_write('DATA'."\r\n");
138      $this->server_parse('354');
139
140      $this->server_write('Subject: '.$subject."\r\n".(empty($to_header) ? "" : $to_header."\r\n").$headers."\r\n\r\n".$message."\r\n");
141      $this->server_write('.'."\r\n");
142      $this->server_parse('250');
143
144      $this->server_write('QUIT'."\r\n");
145      fclose($this->socket);
146    }
147    else
148    {
149      trigger_error('Could not connect to smtp host "'.$this->host.'" ('.$errno.') ('.$errstr.')', E_USER_WARNING);
150      $this->no_error = false;;
151    }
152
153    return $this->no_error;
154  }
155}
156
157?>
Note: See TracBrowser for help on using the repository browser.