# returns 0 on success function email($from, $to, $subject, $message, $reply_to = '', $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 = preg_replace("|[^a-z0-9 _/#'.:&,-]|i", '_', $subject); $headers = "From: $from"; if($reply_to) { $headers .= "\r\nReply-To: $reply_to"; } if($cc) { $headers .= "\r\nCC: $cc"; } if($bcc) { $headers .= "\r\nBCC: $bcc"; } $headers .= "\r\nContent-type: text/plain; charset=UTF-8"; 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(preg_match('|<.*>$|', $addr) === 1) { # format 2 $div = strrpos($addr, '<'); $name = substr($addr, 0 , $div); $name = rtrim($name); $email = substr($addr, $div + 1, -1); } else { $email = $addr; $name = preg_replace('|@.*|', '', $addr); } if(!valid_email($email)) { return false; } #FIXME should allow many more characters here $name = preg_replace("|[^a-z0-9 _/'.-]|i", '_', $name); return $name . ' <' . $email . '>'; } # return true if e-mail is formatted like a valid email address function valid_email($email) { return preg_match('|^[0-9a-zA-Z_~.+-]+@[0-9a-zA-Z.-]+\.[a-z]+$|', $email) === 1; }