JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
email() takes reply-to parameter, added: db_count()
[wfpl.git] / email.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22
23 # This function will SAFELY send e-mail (ie you can pass parameters to it
24 # that you got from a form and not worry about header injection.) Weird
25 # characters are stripped from the $subject and from the real names, but e-mail
26 # addresses are not modified at all.
27
28 # RETURN values:
29 #     0: e-mail successfully accepted for delivery
30 #     1: badly formatted "from" address
31 #     2: badly formatted "to" address
32 #     5: message rejected by mail() (reason unknown)
33
34 # You cannot pass more than one address to any parameter
35 # address fields (from, to, cc, bcc) can be in either of these formats:
36 # 1) me@foo.com  2) Me Who <me@foo.com>
37 # returns 0 on success
38 function email($from, $to, $subject, $message, $reply_to = '', $cc = '', $bcc = '') {
39         if(($from = email_header($from)) === false) { return 1; }
40         if(($to   = email_header($to))   === false) { return 2; }
41         if(($cc   = email_header($cc))   === false) { return 3; }
42         if(($bcc  = email_header($bcc))  === false) { return 4; }
43         if($from == '') { return 1; }
44         if($to   == '') { return 2; }
45
46         #FIXME should allow many more characters here
47         $subject = ereg_replace("[^a-zA-Z0-9 _'.:-]", '_', $subject);
48
49         $headers = "From: $from";
50         if($reply_to) {
51                 $headers .= "\r\nReply-To: $reply_to";
52         }
53         if($cc) {
54                 $headers .= "\r\nCC: $cc";
55         }
56         if($bcc) {
57                 $headers .= "\r\nBCC: $bcc";
58         }
59         #header('Content-Type: text/plain');
60         #print_r(array($to, $subject, $message, $headers));
61         #exit();
62         if(mail($to, $subject, $message, $headers)) {
63                 return 0;
64         } else {
65                 return 5;
66         }
67 }
68         
69
70
71 # This function probably isn't useful appart from writing functions like email() above.
72
73 # addr can be in these formats:
74 # 1) me@foo.com  2) Me Who <me@foo.com>  3)
75 # returns false, or a valid format 2 above, except if input is an empty string, it'll return an empty string
76 function email_header($addr) {
77         if($addr == '') {
78                 return '';
79         }
80
81         if(ereg('<.*>$', $addr) !== false) {
82                 # format 2
83                 $div = strrpos($addr, '<');
84                 $name = substr($addr, 0 , $div);
85                 $name = rtrim($name);
86                 $email = substr($addr, $div + 1, -1);
87         } else {
88                 $email = $addr;
89                 $name = ereg_replace('@.*', '', $addr);
90         }
91
92         if(!valid_email($email)) {
93                 return false;
94         }
95
96         #FIXME should allow many more characters here
97         $name = ereg_replace("[^a-zA-Z0-9 _'.-]", '_', $name);
98
99         return $name . ' <' . $email . '>';
100 }
101         
102
103
104 # return true if e-mail is formatted like a valid email address
105 function valid_email($email) {
106         return ereg('^[0-9a-zA-Z_~.-]+@[0-9a-zA-Z.-]+\.[a-z]+$', $email) !== false;
107 }