JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
initial import (from revision 1199 + email.php)
[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
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23
24 # This function will SAFELY send e-mail (ie you can pass parameters to it
25 # that you got from a form and not worry about header injection.) Weird
26 # characters are stripped from the $subject and from the real names, but e-mail
27 # addresses are not modified at all. If an e-mail address is invalid this
28 # function will return non-zero.
29
30 # You cannot pass more than one address to any parameter
31 # address fields (from, to, cc, bcc) can be in either of these formats:
32 # 1) me@foo.com  2) Me Who <me@foo.com>
33 # returns 0 on success
34 function email($from, $to, $subject, $message, $cc = '', $bcc = '') {
35         if(($from = email_header($from)) === false) { return 1; }
36         if(($to   = email_header($to))   === false) { return 2; }
37         if(($cc   = email_header($cc))   === false) { return 3; }
38         if(($bcc  = email_header($bcc))  === false) { return 4; }
39         if($from == '') { return 1; }
40         if($to   == '') { return 2; }
41
42         #FIXME should allow many more characters here
43         $subject = ereg_replace("[^a-zA-Z _'-]", '_', $subject);
44
45         $headers = "From: $from";
46         if($cc) {
47                 $headers .= "\r\nCC: $cc";
48         }
49         if($bcc) {
50                 $headers .= "\r\nBCC: $bcc";
51         }
52         if(mail($to, $subject, $message, $headers)) {
53                 return 0;
54         } else {
55                 return 5;
56         }
57 }
58         
59
60
61 # This function probably isn't useful appart from writing functions like email() above.
62
63 # addr can be in these formats:
64 # 1) me@foo.com  2) Me Who <me@foo.com>  3)
65 # returns false, or a valid format 2 above, except if input is an empty string, it'll return an empty string
66 function email_header($addr) {
67         if($addr == '') {
68                 return '';
69         }
70
71         if(ereg('<.*>$', $addr) !== false) {
72                 # format 2
73                 list($name, $email) = split('<', $addr);
74                 $name = rtrim($name);
75                 $email = substr($email, 0, -1); # get rid of the '>' at the end
76         } else {
77                 $email = $addr;
78                 $name = ereg_replace('@.*', '', $addr);
79         }
80
81         if(!valid_email($email)) {
82                 return false;
83         }
84
85         #FIXME should allow many more characters here
86         $name = ereg_replace("[^a-zA-Z _'-]", '_', $name);
87
88         return $name . ' <' . $email . '>';
89 }
90         
91
92
93 # return true if e-mail is formatted like a valid email address
94 function valid_email($email) {
95         return ereg('^[0-9a-zA-Z_~.-]+@[0-9a-zA-Z.-]+\.[a-z]+$', $email) !== false;
96 }