Skip to content

Commit 04de515

Browse files
author
Benjamin Vison
committed
Adding validation to email addresses
1 parent 98df130 commit 04de515

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Service/Mailer.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,31 +316,34 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
316316

317317
if(is_array($to)) {
318318
foreach($to as $recipient) {
319-
if($recipient != "") {
319+
if($recipient != "" && filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
320320
$message->addTo($recipient);
321321
}
322322
}
323-
} else {
323+
} elseif($to != "" && filter_var($to, FILTER_VALIDATE_EMAIL)) {
324324
$message->setTo($to);
325+
} else {
326+
$this->container->get('logger')->log(\Monolog\Logger::CRITICAL, "Invalid recipient: ".json_encode($to));
327+
return false;
325328
}
326329

327330
if(is_array($bcc)) {
328331
foreach($bcc as $recipient) {
329-
if($recipient != "") {
332+
if($recipient != "" && filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
330333
$message->addBcc($recipient);
331334
}
332335
}
333-
} else {
336+
} elseif($bcc != "" && filter_var($bcc, FILTER_VALIDATE_EMAIL)) {
334337
$message->setBcc($bcc);
335338
}
336339

337340
if(is_array($cc)) {
338341
foreach($cc as $recipient) {
339-
if($recipient != "") {
342+
if($recipient != "" && filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
340343
$message->addCc($recipient);
341344
}
342345
}
343-
} else {
346+
} elseif($cc != "" && filter_var($cc, FILTER_VALIDATE_EMAIL)) {
344347
$message->setCc($cc);
345348
}
346349

@@ -366,7 +369,6 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
366369

367370
if (!$smtp) {
368371
$this->container->get('logger')->log(\Monolog\Logger::WARNING, "Unable to find an SMTP configuration with the UniqueID of {$from}");
369-
370372
return false;
371373
}
372374

0 commit comments

Comments
 (0)