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