Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
6 kaklik 1
<?php
2
/*************************
3
  Coppermine Photo Gallery
4
  ************************
5
  Copyright (c) 2003-2005 Coppermine Dev Team
6
  v1.1 originaly written by Gregory DEMAR
7
 
8
  This program is free software; you can redistribute it and/or modify
9
  it under the terms of the GNU General Public License as published by
10
  the Free Software Foundation; either version 2 of the License, or
11
  (at your option) any later version.
12
  ********************************************
13
  Coppermine version: 1.3.3
14
  $Source: /cvsroot/coppermine/stable/include/mailer.inc.php,v $
15
  $Revision: 1.6 $
16
  $Author: gaugau $
17
  $Date: 2005/04/19 03:17:11 $
18
**********************************************/
19
 
20
$CONFIG['smtp_host'] = '';
21
$CONFIG['smtp_username'] = '';
22
$CONFIG['smtp_password'] = '';
23
// Custom mail function
24
function cpg_mail($to, $subject, $msg_body, $type = 'text/plain', $sender_name = '', $sender_email = '')
25
{
26
    global $CONFIG;
27
    global $lang_charset;
28
 
29
    if ($sender_name == '') $sender_name = $CONFIG['gallery_name'];
30
    if ($sender_email == '') $sender_email = $CONFIG['gallery_admin_email'];
31
 
32
    $charset = $CONFIG['charset'] == 'language file' ? $lang_charset : $CONFIG['charset'];
33
 
34
    $extra_headers = "From: $sender_name <$sender_email>\n" . "MIME-Version: 1.0\n" . "Content-type: $type; charset=" . $charset . "\n" . "Content-transfer-encoding: 8bit\n" . "Date: " . gmdate('D, d M Y H:i:s', time()) . " UT\n" ."X-Priority: 3 (Normal)\n" ."X-MSMail-Priority: Normal\n" . "X-Mailer: Coppermine Photo Gallery\n" ."Importance: Normal";
35
    // Fix any bare linefeeds in the message to make it RFC821 Compliant.
36
 
37
    $message = preg_replace("/(?<!\r)\n/si", "\r\n", $msg_body);
38
 
39
    if (empty($CONFIG['smtp_host'])) {
40
        return mail($to, $subject, $msg_body, $extra_headers);
41
    } else {
42
        return smtp_mail($to, $subject, $msg_body, $extra_headers);
43
    }
44
}
45
 
46
/**
47
 * smtp.php
48
 *                        -------------------
49
 *    begin                : Wed May 09 2001
50
 *    copyright            : (C) 2001 The phpBB Group
51
 *    email                : support@phpbb.com
52
 *
53
 *    $Id: mailer.inc.php,v 1.6 2005/04/19 03:17:11 gaugau Exp $
54
 */
55
 
56
/**
57
 * This program is free software; you can redistribute it and/or modify
58
 *    it under the terms of the GNU General Public License as published by
59
 *    the Free Software Foundation; either version 2 of the License, or
60
 *    (at your option) any later version.
61
 */
62
 
63
// This function has been modified as provided
64
// by SirSir to allow multiline responses when
65
// using SMTP Extensions
66
 
67
function server_parse($socket, $response)
68
{
69
    while (substr($server_response, 3, 1) != ' ') {
70
        if (!($server_response = fgets($socket, 256))) {
71
            cpg_die(ERROR, "Couldn't get mail server response codes", __FILE__, __LINE__);
72
        }
73
    }
74
 
75
    if (!(substr($server_response, 0, 3) == $response)) {
76
        cpg_die(ERROR, "Ran into problems sending Mail. Response: $server_response", "", __FILE__, __LINE__);
77
    }
78
}
79
 
80
/**
81
 * Function:                 smtpmail
82
 *         Description:         This is a functional replacement for php's builtin mail
83
 *                                                 function, that uses smtp.
84
 *         Usage:                        The usage for this function is identical to that of php's
85
 *                                                 built in mail function.
86
 */
87
function smtp_mail($mail_to, $subject, $message, $headers = "")
88
{
89
    // For now I'm using an array based $smtp_vars to hold the smtp server
90
    // info, but it should probably change to $CONFIG...
91
    // then the relevant info would be $CONFIG['smtp_host'] and
92
    // $CONFIG['smtp_port'].
93
    global $CONFIG;
94
 
95
    // Fix any bare linefeeds in the message to make it RFC821 Compliant.
96
 
97
    $message = preg_replace("/(?<!\r)\n/si", "\r\n", $message);
98
 
99
    if ($headers != "") {
100
        if (is_array($headers)) {
101
            if (sizeof($headers) > 1) {
102
                $headers = join("\n", $headers);
103
            } else {
104
                $headers = $headers[0];
105
            }
106
        }
107
        $headers = chop($headers);
108
 
109
        // Make sure there are no bare linefeeds in the headers
110
 
111
        $headers = preg_replace("/(?<!\r)\n/si", "\r\n", $headers);
112
 
113
        // Ok this is rather confusing all things considered,
114
        // but we have to grab bcc and cc headers and treat them differently
115
        // Something we really didn't take into consideration originally
116
 
117
        $header_array = explode("\n", $headers);
118
        @reset($header_array);
119
        $headers = "";
120
        while (list(, $header) = each($header_array)) {
121
            if (preg_match("/^cc:/si", $header)) {
122
                $cc = preg_replace("/^cc:(.*)/si", "\\1", $header);
123
            } else if (preg_match("/^bcc:/si", $header)) {
124
                $bcc = preg_replace("/^bcc:(.*)/si", "\\1", $header);
125
                $header = "";
126
            }
127
            $headers .= $header . "\n";
128
        }
129
        $headers = chop($headers);
130
        $cc = explode(",", $cc);
131
        $bcc = explode(",", $bcc);
132
    }
133
    if (trim($mail_to) == "") {
134
        cpg_die(ERROR, "No email address specified", __FILE__, __LINE__);
135
    }
136
    if (trim($subject) == "") {
137
        cpg_die(ERROR, "No email Subject specified", __FILE__, __LINE__);
138
    }
139
    if (trim($message) == "") {
140
        cpg_die(ERROR, "Email message was blank", __FILE__, __LINE__);
141
    }
142
    $mail_to_array = explode(",", $mail_to);
143
 
144
    // Ok we have error checked as much as we can to this point let's get on
145
    // it already.
146
 
147
    if (!$socket = fsockopen($CONFIG['smtp_host'], 25, $errno, $errstr, 20)) {
148
        cpg_die(ERROR, "Could not connect to smtp host : $errno : $errstr", __FILE__, __LINE__);
149
    }
150
    server_parse($socket, "220");
151
 
152
    if (!empty($CONFIG['smtp_username']) && !empty($CONFIG['smtp_password'])) {
153
        // Send the RFC2554 specified EHLO.
154
        // This improved as provided by SirSir to accomodate
155
        // both SMTP AND ESMTP capable servers
156
        fputs($socket, "EHLO " . $CONFIG['smtp_host'] . "\r\n");
157
        server_parse($socket, "250");
158
 
159
        fputs($socket, "AUTH LOGIN\r\n");
160
        server_parse($socket, "334");
161
        fputs($socket, base64_encode($CONFIG['smtp_username']) . "\r\n");
162
        server_parse($socket, "334");
163
        fputs($socket, base64_encode($CONFIG['smtp_password']) . "\r\n");
164
        server_parse($socket, "235");
165
    } else {
166
        // Send the RFC821 specified HELO.
167
        fputs($socket, "HELO " . $CONFIG['smtp_host'] . "\r\n");
168
        server_parse($socket, "250");
169
    }
170
    // From this point onward most server response codes should be 250
171
    // Specify who the mail is from....
172
    fputs($socket, "MAIL FROM: <" . $CONFIG['gallery_admin_email'] . ">\r\n");
173
    server_parse($socket, "250");
174
    // Specify each user to send to and build to header.
175
    $to_header = "To: ";
176
    @reset($mail_to_array);
177
    while (list(, $mail_to_address) = each($mail_to_array)) {
178
 
179
        // Add an additional bit of error checking to the To field.
180
 
181
        $mail_to_address = trim($mail_to_address);
182
        if (preg_match('/[^ ]+\@[^ ]+/', $mail_to_address)) {
183
            fputs($socket, "RCPT TO: <$mail_to_address>\r\n");
184
            server_parse($socket, "250");
185
        }
186
        $to_header .= (($mail_to_address != '') ? ', ' : '') . "<$mail_to_address>";
187
    }
188
    // Ok now do the CC and BCC fields...
189
    @reset($bcc);
190
    while (list(, $bcc_address) = each($bcc)) {
191
 
192
        // Add an additional bit of error checking to bcc header...
193
 
194
        $bcc_address = trim($bcc_address);
195
        if (preg_match('/[^ ]+\@[^ ]+/', $bcc_address)) {
196
            fputs($socket, "RCPT TO: <$bcc_address>\r\n");
197
            server_parse($socket, "250");
198
        }
199
    }
200
    @reset($cc);
201
    while (list(, $cc_address) = each($cc)) {
202
 
203
        // Add an additional bit of error checking to cc header
204
 
205
        $cc_address = trim($cc_address);
206
        if (preg_match('/[^ ]+\@[^ ]+/', $cc_address)) {
207
            fputs($socket, "RCPT TO: <$cc_address>\r\n");
208
            server_parse($socket, "250");
209
        }
210
    }
211
    // Ok now we tell the server we are ready to start sending data
212
    fputs($socket, "DATA\r\n");
213
    // This is the last response code we look for until the end of the message.
214
    server_parse($socket, "354");
215
    // Send the Subject Line...
216
    fputs($socket, "Subject: $subject\r\n");
217
    // Now the To Header.
218
    fputs($socket, "$to_header\r\n");
219
    // Now any custom headers....
220
    fputs($socket, "$headers\r\n\r\n");
221
    // Ok now we are ready for the message...
222
    fputs($socket, "$message\r\n");
223
    // Ok the all the ingredients are mixed in let's cook this puppy...
224
    fputs($socket, ".\r\n");
225
    server_parse($socket, "250");
226
    // Now tell the server we are done and close the socket...
227
    fputs($socket, "QUIT\r\n");
228
    fclose($socket);
229
 
230
    return true;
231
}
232
 
233
?>