JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed file_run to use _main postfix, added enc_url_path(), enc_ddmmyyyhhmm(), allowed...
[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, $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-Z _'.:-]", '_', $subject);
48
49         $headers = "From: $from";
50         if($cc) {
51                 $headers .= "\r\nCC: $cc";
52         }
53         if($bcc) {
54                 $headers .= "\r\nBCC: $bcc";
55         }
56         if(mail($to, $subject, $message, $headers)) {
57                 return 0;
58         } else {
59                 return 5;
60         }
61 }
62         
63
64
65 # This function probably isn't useful appart from writing functions like email() above.
66
67 # addr can be in these formats:
68 # 1) me@foo.com  2) Me Who <me@foo.com>  3)
69 # returns false, or a valid format 2 above, except if input is an empty string, it'll return an empty string
70 function email_header($addr) {
71         if($addr == '') {
72                 return '';
73         }
74
75         if(ereg('<.*>$', $addr) !== false) {
76                 # format 2
77                 list($name, $email) = split('<', $addr);
78                 $name = rtrim($name);
79                 $email = substr($email, 0, -1); # get rid of the '>' at the end
80         } else {
81                 $email = $addr;
82                 $name = ereg_replace('@.*', '', $addr);
83         }
84
85         if(!valid_email($email)) {
86                 return false;
87         }
88
89         #FIXME should allow many more characters here
90         $name = ereg_replace("[^a-zA-Z _'-]", '_', $name);
91
92         return $name . ' <' . $email . '>';
93 }
94         
95
96
97 # return true if e-mail is formatted like a valid email address
98 function valid_email($email) {
99         return ereg('^[0-9a-zA-Z_~.-]+@[0-9a-zA-Z.-]+\.[a-z]+$', $email) !== false;
100 }