File: mailerc.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (250 lines) | stat: -rw-r--r-- 6,871 bytes parent folder | download | duplicates (3)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
////////////////////////////////////////////////////
// mailerc - phpmailer client
//
// Version 0.07, Created 2001-01-03
//
// Client application for sending outgoing 
// messages from a file. 
//
// Author: Brent R. Matzelle <bmatzelle@yahoo.com>
//
// License: LGPL, see LICENSE
////////////////////////////////////////////////////

require("class.phpmailer.php");

// Gather global server arg variables
if(!isset($_SERVER))
    $_SERVER = $HTTP_SERVER_VARS;
$cargv = $_SERVER["argv"];
$cargc = $_SERVER["argc"];

define("PROG_VERSION", "0.01");
define("PROG_NAME", $cargv[0]);
@set_time_limit(0); // unlimited

// Ignore warning messages
error_reporting(E_ERROR);

/**
 * mailerc - mailerc extension class
 * @author Brent R. Matzelle
 */
class mailerc extends phpmailer
{
    /**
     * Send an email from a file created with the
     * SendToQueue() function.
     * @public
     * @returns bool
     */
    function SendFromFile($filePath) {
        $qarray = array();
        $to_list = array();
        $header = "";
        $body = "";

        // Make sure is there and accessible
        if(!is_file($filePath))
        {
            $this->error_handler(sprintf("Cannot access: %s", $filePath));
            return false;
        }
        
        // upon getting header do not forget to gather the 
        // server info (date, recieved())
        $qarray = file($filePath);
        
        if(count($qarray) < 1)
        {
            $this->error_handler("Invalid queue file");
            return false;
        }

        // Create the header and the body (just set header)
        $header = $this->received();
        $header .= sprintf("Date: %s\r\n", $this->rfc_date());
        
        $msg_start = 0;
        for($i = 0; $i < count($qarray); $i++)
        {
            if($qarray[$i] == "----END PQM HEADER----\r\n")
            {
                $msg_start = $i + 1;
                break;
            }
        }
        
        for($i = $msg_start; $i < count($qarray); $i++)
            $body .= $qarray[$i];

        $this->Mailer = $this->qvar($qarray, "Mailer");
        if($this->Mailer == "sendmail")
        {
            $this->Sendmail = $this->qvar($qarray, "Sendmail");
            $this->Sender   = $this->qvar($qarray, "Sender");

            if(!$this->sendmail_send($header, $body))
                return false;
        }
        elseif($this->Mailer == "mail")
        {
            $this->Sender  = $this->qvar($qarray, "Sender");
            $this->Subject = $this->qvar($qarray, "Subject");

            $to_list = explode(";", $this->qvar($qarray, "to"));
            for($i = 0; $i < count($to_list); $i++)
                $this->AddAddress($to_list[0], "");

            // This might not work because of not sending 
            // both a header and body.
            if(!$this->mail_send($header, $body))
                return false;
        }
        elseif($this->Mailer == "smtp")
        {
            $this->Host     = $this->qvar($qarray, "Host");
            $this->Port     = $this->qvar($qarray, "Port");
            $this->Helo     = $this->qvar($qarray, "Helo");
            $this->Timeout  = $this->qvar($qarray, "Timeout");
            $this->SMTPAuth = (int)$this->qvar($qarray, "SMTPAuth");
            $this->Username = $this->qvar($qarray, "Username");
            $this->Password = $this->qvar($qarray, "Password");
            $this->From     = $this->qvar($qarray, "From");

            $to_addr = $this->qvar($qarray, "to");
            if(!empty($to_addr))
            {
                $to_list = explode(";", $to_addr);
                for($i = 0; $i < count($to_list); $i++)
                    $this->AddAddress($to_list[0], "");
            }

            $to_addr = $this->qvar($qarray, "cc");
            if(!empty($to_addr))
            {
                $to_list = explode(";", $to_addr);
                for($i = 0; $i < count($to_list); $i++)
                    $this->AddCC($to_list[0], "");
            }

            $to_addr = $this->qvar($qarray, "bcc");
            if(!empty($to_addr))
            {
                $to_list = explode(";", $to_addr);
                for($i = 0; $i < count($to_list); $i++)
                    $this->AddBCC($to_list[0], "");
            }

            if(!$this->smtp_send($header, $body))
                return false;
        }
        else
        {
            $this->error_handler(sprintf("%s mailer is not supported", $this->Mailer));
            return false;
        }
        
        return true;
    }
    
    /**
     * Return the given queue variable from the pqm header file. Returns 
     * an empty string if the data was not found.
     * @private
     * @return string
     */
    function qvar($qarray, $data) {
        $i = 0;
        $pqm_marker = "----END PQM HEADER----\n";

        while($qarray[$i] != $pqm_marker)
        {
            $item = explode(": ", $qarray[$i]);
            //echo $item[0] . "\n"; // debug
            if($item[0] == $data)
                return rtrim($item[1]);
            $i++;
        }

        return ""; // failure
    }
}

/**
 * Print out the program version information.
 * @private
 * @returns void
 */
function print_version()
{
   printf("mailerc %s - phpmailer client\n\n" .
     "This program is distributed in the hope that it will be useful,\n" .
     "but WITHOUT ANY WARRANTY; without even the implied warranty of \n" .
     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n" .
     "GNU Lesser General Public License for more details.\n\n" .
     "Written by: Brent R. Matzelle\n", PROG_VERSION);
}

/*
  Print out the help message to the console.
  @private
  @returns void
 */
function print_help()
{
  printf("mailerc %s, phpmailer queue daemon.\n", PROG_VERSION);
  printf("Usage: %s [OPTION] [FILE]\n", PROG_NAME);
  printf("
Options:
  -h,  --help                            print this help.
  -V,  --version                         print version information.
  -s,  --send                            send [FILE]\n");
}

/**
 * Sends a given message from a pqm (Phpmailer Queue Message) file.
 * @private
 * @returns bool
 */
function send_message($filePath)
{
    // Open the file and read the header contents and set 
    // another message.  Then run the phpmailer send file.
    $mail = new mailerc();
    if(!$mail->SendFromFile($filePath))
        printf("error: %s\n", $mail->ErrorInfo);
    else
        printf("success: sent\n");
}

/*
  Pseudo main()
 */
if($cargc < 1)
{
    print_version();
    exit;
}

switch($cargv[1])
{
    case "-h":
    case "--help":
        print_help();
        break;
    case "-V":
    case "--version":
        print_version();
        break;
    case "-s":
    case "--send":
        send_message($cargv[2]);
        break;
    default:
        print_help();
}

return 0;  // return success
?>