source: branches/branch-1_7/include/class_smtp_mail.inc.php @ 2283

Last change on this file since 2283 was 2283, checked in by rub, 17 years ago

Resolved issue 0000815: Email format standardization

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