1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2010 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 | |
---|
26 | class 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 | function add_recipients(&$recipients, $headers, $type_header) |
---|
77 | { |
---|
78 | if (preg_match('/^\s*'.$type_header.'\s*:.*/mi', $headers, $matches) != 0) |
---|
79 | { |
---|
80 | $list = explode(',', $matches[0]); |
---|
81 | foreach ($list as $email) |
---|
82 | { |
---|
83 | if (strpos($email, '<') !== false) |
---|
84 | { |
---|
85 | $email = preg_replace('/.*<(.*)>.*/i', '$1', $email); |
---|
86 | } |
---|
87 | $recipients[] = trim($email); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | // Adaptation of pun_mail |
---|
93 | function mail($to, $subject, $message, $headers = '') |
---|
94 | { |
---|
95 | $this->no_error = true; |
---|
96 | |
---|
97 | // Are we using port 25 or a custom port? |
---|
98 | if (strpos($this->host, ':') !== false) |
---|
99 | { |
---|
100 | list($smtp_host, $smtp_port) = explode(':', $this->host); |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | $smtp_host = $this->host; |
---|
105 | $smtp_port = 25; |
---|
106 | } |
---|
107 | |
---|
108 | if ($this->socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)) |
---|
109 | { |
---|
110 | $this->server_parse('220'); |
---|
111 | |
---|
112 | if (!empty($this->user) && !empty($this->password)) |
---|
113 | { |
---|
114 | $this->server_write('EHLO '.$smtp_host."\r\n"); |
---|
115 | $this->server_parse('250'); |
---|
116 | |
---|
117 | $this->server_write('AUTH LOGIN'."\r\n"); |
---|
118 | $this->server_parse('334'); |
---|
119 | |
---|
120 | $this->server_write(base64_encode($this->user)."\r\n"); |
---|
121 | $this->no_error = $this->no_error && $this->no_error = $this->server_parse('334'); |
---|
122 | |
---|
123 | $this->server_write(base64_encode($this->password)."\r\n"); |
---|
124 | $this->server_parse('235'); |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | $this->server_write('HELO '.$smtp_host."\r\n"); |
---|
129 | $this->server_parse('250'); |
---|
130 | } |
---|
131 | |
---|
132 | $this->server_write('MAIL FROM:<'.$this->email_webmaster.'>'."\r\n"); |
---|
133 | $this->server_parse('250'); |
---|
134 | |
---|
135 | // Add "To:" on headers if there are included |
---|
136 | if ((preg_match('/^\s*to\s*:.*/mi', $headers) === 0) and !empty($to)) |
---|
137 | { |
---|
138 | $to_header = 'To:'.implode(',', array_map(create_function('$email','return "<".$email.">";'), explode(',', $to))); |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | $to_header = ''; |
---|
143 | } |
---|
144 | |
---|
145 | if (!empty($to)) |
---|
146 | { |
---|
147 | $recipients = explode(',', $to); |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | $recipients = array(); |
---|
152 | } |
---|
153 | |
---|
154 | $this->add_recipients($recipients, $headers, 'Cc'); |
---|
155 | $this->add_recipients($recipients, $headers, 'Bcc'); |
---|
156 | |
---|
157 | @reset($recipients); |
---|
158 | while (list(, $email) = @each($recipients)) |
---|
159 | { |
---|
160 | $this->server_write('RCPT TO:<'.$email.'>'."\r\n"); |
---|
161 | $this->server_parse('250'); |
---|
162 | } |
---|
163 | |
---|
164 | $this->server_write('DATA'."\r\n"); |
---|
165 | $this->server_parse('354'); |
---|
166 | |
---|
167 | $this->server_write('Subject:'.$subject."\r\n".(empty($to_header) ? "" : $to_header."\r\n").$headers."\r\n\r\n".$message."\r\n"); |
---|
168 | $this->server_write('.'."\r\n"); |
---|
169 | $this->server_parse('250'); |
---|
170 | |
---|
171 | $this->server_write('QUIT'."\r\n"); |
---|
172 | fclose($this->socket); |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | trigger_error('Could not connect to smtp host "'.$this->host.'" ('.$errno.') ('.$errstr.')', E_USER_WARNING); |
---|
177 | $this->no_error = false;; |
---|
178 | } |
---|
179 | |
---|
180 | return $this->no_error; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | ?> |
---|