Suppose we send trivial feedback and are going to make these fields dynamic:
- Sender's name
- email sender
- Headline
- message body
Will this PHP code be enough to protect us from all kinds of email injection?
//sanitizing email address
if ($email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
//encoding subj according to RFC and thus protecting it from all kinds of injections
$subject = "=?UTF-8?B?".base64_encode($_POST['subject'])."?=";
//encoding name for same reasons, and using sanitized email
$from = "From: =?UTF-8?B?".base64_encode($_POST['name'])."?= <$email>\r\n";
//protecting body as it mentioned in http://php.net/mail
$message = str_replace("\n.", "\n .", $_POST['text']);
mail('me@example.com',$subject,$message,$from);
}
I'm currently playing with type names "some@email.com, other@email.com,", but it seems that all available email clients handle it correctly
source
share