PHP使用explode验证多个电子邮件地址

时间:2013-10-17 11:44:50

标签: php email

我有这个PHP函数将电子邮件发送到MySQL数据库中的地址:

require_once "/usr/local/lib/php/Mail.php";

                $from = $email_from;
                $to = $email_to;
                $subject = $email_subject;
                $body = $email_body;

                $host = "mail.domain.co.uk";
                $username = "sending@domain.co.uk";
                $password = "*********";

                $headers = array ('From' => $from,
                  'To' => $to,
                  'Cc' => $cc,
                  'Subject' => $subject,
                  'Content-type' => 'text/html');
                $smtp = Mail::factory('smtp',
                  array ('host' => $host,
                 'auth' => true,
                 'username' => $username,
                 'password' => $password));

                 $rec = $to.', '.$cc;

                $mail = $smtp->send($rec, $headers, $body);

每个电子邮件地址都用逗号分隔:

mail@domain.com,mail2@domain2.co.uk

我尝试过使用:

if(filter_var($email_to, FILTER_VALIDATE_EMAIL))

但这仅适用于一个电子邮件地址,如何验证以逗号分隔的多个?

如何验证$to$cc中的逗号分隔值?

4 个答案:

答案 0 :(得分:2)

您需要遍历每封电子邮件并单独验证它们。

像这样:

<?php
$emails = 'mail@domain.com,mail2@domain2.co.uk';

$explode = explode(',',$emails); // Explodes the emails by the comma
$valid = true;

// Loop through each email and validate it
foreach($explode as $email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $valid = false;
    }
}
?>

答案 1 :(得分:2)

您可以将array_filter()与回调一起使用,如下所示:

$validEmails = array_filter($emails, function($val) {
    return (bool) filter_var($val, FILTER_VALIDATE_EMAIL)); 
});

要将有效的电子邮件作为逗号分隔字符串,您可以使用implode()

$validEmailString = implode(',',$validEmails);

Demo!

答案 2 :(得分:0)

你可以这样做

$valid = true;

foreach(explode(",", $email_to) as $email){
   if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
       { 
         $valid = false;
       }
}

所有这些都与您的代码一起

require_once "/usr/local/lib/php/Mail.php";

                $from = $email_from;
                $to = $email_to;
                $subject = $email_subject;
                $body = $email_body;


    $valid = true;

    foreach(explode(",", $to) as $email){
       if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
           { 
             $valid = false;
           }
    }


if(!$valid) {

echo "Bad address"; exit; 

}

                $host = "mail.domain.co.uk";
                $username = "sending@domain.co.uk";
                $password = "*********";

                $headers = array ('From' => $from,
                  'To' => $to,
                  'Cc' => $cc,
                  'Subject' => $subject,
                  'Content-type' => 'text/html');
                $smtp = Mail::factory('smtp',
                  array ('host' => $host,
                 'auth' => true,
                 'username' => $username,
                 'password' => $password));

                 $rec = $to.', '.$cc;

                $mail = $smtp->send($rec, $headers, $body);

答案 3 :(得分:0)

这有用吗?

foreach(explode(',', $sEmailAddresses) AS $sEmailAddress){
   $bValid |= filter_var($sEmailAddress, FILTER_VALIDATE_EMAIL)
}

$sResult = ($bValid) ? 'Both are valid' : 'One of them are not';