# returns 0 on success function email($from, $to, $subject, $message, $cc = '', $bcc = '') { if(($from = email_header($from)) === false) { return 1; } if(($to = email_header($to)) === false) { return 2; } if(($cc = email_header($cc)) === false) { return 3; } if(($bcc = email_header($bcc)) === false) { return 4; } if($from == '') { return 1; } if($to == '') { return 2; } #FIXME should allow many more characters here $subject = ereg_replace("[^a-zA-Z _'.-]", '_', $subject); $headers = "From: $from"; if($cc) { $headers .= "\r\nCC: $cc"; } if($bcc) { $headers .= "\r\nBCC: $bcc"; } if(mail($to, $subject, $message, $headers)) { return 0; } else { return 5; } } # This function probably isn't useful appart from writing functions like email() above. # addr can be in these formats: # 1) me@foo.com 2) Me Who 3) # returns false, or a valid format 2 above, except if input is an empty string, it'll return an empty string function email_header($addr) { if($addr == '') { return ''; } if(ereg('<.*>$', $addr) !== false) { # format 2 list($name, $email) = split('<', $addr); $name = rtrim($name); $email = substr($email, 0, -1); # get rid of the '>' at the end } else { $email = $addr; $name = ereg_replace('@.*', '', $addr); } if(!valid_email($email)) { return false; } #FIXME should allow many more characters here $name = ereg_replace("[^a-zA-Z _'-]", '_', $name); return $name . ' <' . $email . '>'; } # return true if e-mail is formatted like a valid email address function valid_email($email) { return ereg('^[0-9a-zA-Z_~.-]+@[0-9a-zA-Z.-]+\.[a-z]+$', $email) !== false; }