1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?php /*************************************************************************** * smtp.php * ------------------- * begin : Wed May 09 2001 * copyright : (C) 2001 The phpBB Group * email : support@phpbb.com * * phpBB Id: smtp.php,v 1.16.2.9 2003/07/18 16:34:01 acydburn * G2 $Id: smtp.php 20954 2009-12-14 20:10:04Z mindless $ * ***************************************************************************/
/*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * ***************************************************************************/
function server_parse(&$socket, $response) { for ($server_response = ''; substr($server_response, 3, 1) != ' ';) { if (!($server_response = fgets($socket, 256))) { return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__, "Couldn't get mail server response code"); } } if (!(substr($server_response, 0, 3) == $response)) { return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__, "Problem sending mail. Server response: $server_response"); } return null; }
function smtpmail($config, $to, $subject, $body, $headers=null) { // Fix any bare linefeeds in the message to make it RFC821 Compliant. $body = preg_replace("#(?<!\r)\n#si", "\r\n", $body);
$cc = $bcc = array(); if (isset($headers)) { $headers = rtrim($headers);
// Make sure there are no bare linefeeds in the headers $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
if (preg_match('#^cc:\s*(.*?)\s*$#mi', $headers, $match)) { $cc = preg_split('/, */', $match[1]); } if (preg_match('#^bcc:\s*(.*?)\s*$#mi', $headers, $match)) { $bcc = preg_split('/, */', $match[1]); $headers = preg_replace('#^bcc:.*$#mi', '', $headers); } }
if (trim($subject) == '') { return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__, "No email Subject specified"); }
if (trim($body) == '') { return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__, "Email message was blank"); }
// Connect list ($config['smtp.host'], $port) = array_merge(explode(':', $config['smtp.host']), array(25)); if (!($socket = fsockopen($config['smtp.host'], $port, $errno, $errstr, 20))) { return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__, "Could not connect to smtp host : $errno : $errstr"); }
// Wait for reply $ret = server_parse($socket, "220"); if ($ret) { return $ret; }
// Do we want to use AUTH?, send RFC2554 EHLO, else send RFC821 HELO if (!empty($config['smtp.username']) && !empty($config['smtp.password'])) { fputs($socket, "EHLO " . $config['smtp.host'] . "\r\n"); $ret = server_parse($socket, "250"); if ($ret) { return $ret; }
fputs($socket, "AUTH LOGIN\r\n"); $ret = server_parse($socket, "334"); if ($ret) { return $ret; }
fputs($socket, base64_encode($config['smtp.username']) . "\r\n"); $ret = server_parse($socket, "334"); if ($ret) { return $ret; }
fputs($socket, $config['smtp.password'] . "\r\n"); // Already encoded $ret = server_parse($socket, "235"); if ($ret) { return $ret; } } else { fputs($socket, "HELO " . $config['smtp.host'] . "\r\n"); $ret = server_parse($socket, "250"); if ($ret) { return $ret; } }
// From this point onward most server response codes should be 250 // Specify who the mail is from.... fputs($socket, "MAIL FROM: <" . $config['smtp.from'] . ">\r\n"); $ret = server_parse($socket, "250"); if ($ret) { return $ret; }
// Add an additional bit of error checking to the To field. $to = (trim($to) == '') ? 'Undisclosed-recipients:;' : trim($to); if (preg_match('#[^ ]+\@[^ ]+#', $to)) { fputs($socket, "RCPT TO: <$to>\r\n"); $ret = server_parse($socket, "250"); if ($ret) { return $ret; } }
// Ok now do the CC and BCC fields... foreach (array_merge($cc, $bcc) as $address) { $address = trim($address); if (preg_match('#[^ ]+\@[^ ]+#', $address)) { fputs($socket, "RCPT TO: <$address>\r\n"); $ret = server_parse($socket, "250"); if ($ret) { return $ret; } } }
// Ok now we tell the server we are ready to start sending data fputs($socket, "DATA\r\n");
// This is the last response code we look for until the end of the message. $ret = server_parse($socket, "354"); if ($ret) { return $ret; }
// Send the Subject Line... fputs($socket, "Subject: $subject\r\n");
// Now the To Header. fputs($socket, "To: $to\r\n");
// Now any custom headers.... if (isset($headers)) { fputs($socket, "$headers\r\n"); }
// Ok now we are ready for the message... fputs($socket, "\r\n$body\r\n");
// Ok the all the ingredients are mixed in let's cook this puppy... fputs($socket, ".\r\n"); $ret = server_parse($socket, "250"); if ($ret) { return $ret; }
// Now tell the server we are done and close the socket... fputs($socket, "QUIT\r\n"); fclose($socket);
return null; } ?>
|