3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
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)
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
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
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.
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)
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; }
46 #FIXME should allow many more characters here
47 $subject = ereg_replace("[^a-zA-Z0-9 _'.:-]", '_', $subject);
49 $headers = "From: $from";
51 $headers .= "\r\nReply-To: $reply_to";
54 $headers .= "\r\nCC: $cc";
57 $headers .= "\r\nBCC: $bcc";
59 #header('Content-Type: text/plain');
60 #print_r(array($to, $subject, $message, $headers));
62 if(mail($to, $subject, $message, $headers)) {
71 # This function probably isn't useful appart from writing functions like email() above.
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) {
81 if(ereg('<.*>$', $addr) !== false) {
83 $div = strrpos($addr, '<');
84 $name = substr($addr, 0 , $div);
86 $email = substr($addr, $div + 1, -1);
89 $name = ereg_replace('@.*', '', $addr);
92 if(!valid_email($email)) {
96 #FIXME should allow many more characters here
97 $name = ereg_replace("[^a-zA-Z0-9 _'.-]", '_', $name);
99 return $name . ' <' . $email . '>';
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;