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

Last change on this file since 2297 was 2297, checked in by plg, 17 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

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