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

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

Resolved 0000580: Send mail by define smtp configuration

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 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-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: class_smtp_mail.inc.php 2105 2007-09-25 21:49:21Z rub $
8// | last update   : $Date: 2007-09-25 21:49:21 +0000 (Tue, 25 Sep 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2105 $
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      $to_header = 'To: ';
125
126      @reset($recipients);
127      while (list(, $email) = @each($recipients))
128      {
129        $this->server_write('RCPT TO: <'.$email.'>'."\r\n");
130        $this->server_parse('250');
131
132        $to_header .= '<'.$email.'>, ';
133      }
134
135      $this->server_write('DATA'."\r\n");
136      $this->server_parse('354');
137
138      $this->server_write('Subject: '.$subject."\r\n".$to_header."\r\n".$headers."\r\n\r\n".$message."\r\n");
139      $this->server_write('.'."\r\n");
140      $this->server_parse('250');
141
142      $this->server_write('QUIT'."\r\n");
143      fclose($this->socket);
144    }
145    else
146    {
147      trigger_error('Could not connect to smtp host "'.$this->host.'" ('.$errno.') ('.$errstr.')', E_USER_WARNING);
148      $this->no_error = false;;
149    }
150
151    return $this->no_error;
152  }
153}
154
155?>
Note: See TracBrowser for help on using the repository browser.