-1

我有这个 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))

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

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

4

4 回答 4

3

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

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

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

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

演示!

于 2013-10-17T11:52:58.557 回答
3

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

像这样:

<?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;
    }
}
?>
于 2013-10-17T11:47:55.900 回答
1

你可以这样做

$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);
于 2013-10-17T11:47:12.617 回答
0

这行得通吗?

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

$sResult = ($bValid) ? 'Both are valid' : 'One of them are not';
于 2013-10-17T11:48:34.973 回答