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
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)
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.
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,
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.
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; }
42 #FIXME should allow many more characters here
43 $subject = ereg_replace("[^a-zA-Z _'-]", '_', $subject);
45 $headers = "From: $from";
47 $headers .= "\r\nCC: $cc";
50 $headers .= "\r\nBCC: $bcc";
52 if(mail($to, $subject, $message, $headers)) {
61 # This function probably isn't useful appart from writing functions like email() above.
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) {
71 if(ereg('<.*>$', $addr) !== false) {
73 list($name, $email) = split('<', $addr);
75 $email = substr($email, 0, -1); # get rid of the '>' at the end
78 $name = ereg_replace('@.*', '', $addr);
81 if(!valid_email($email)) {
85 #FIXME should allow many more characters here
86 $name = ereg_replace("[^a-zA-Z _'-]", '_', $name);
88 return $name . ' <' . $email . '>';
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;