PHP验证用逗号分隔的电子邮件地址

时间:2013-10-17 11:11:51

标签: php mysql email

具有PHP Mail功能,可以将电子邮件发送到MySQL数据库中的地址。

在数据库中,当有多个时,电子邮件地址用逗号分隔。

我尝试过使用:

if(filter_var($email_to, FILTER_VALIDATE_EMAIL))

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

像:

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

这是我的电子邮件功能:

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);

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

5 个答案:

答案 0 :(得分:3)

试试这个:

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

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

答案 1 :(得分:2)

功能:

function getValidEmails($email_list) {
    $valid_emails = array();
    if (-1 !== strpos($email_list, ',')) {
        $email_list = explode(',', $email_list);
        array_walk_recursive($email_list, function(&$email) {
            $email = filter_var(trim($email), FILTER_VALIDATE_EMAIL);
            if (false !== $email) {
                $valid_emails[] = $email;
            }
        });
    } else {
        $email = filter_var(trim($email_list), FILTER_VALIDATE_EMAIL);
        if (false !== $email) {
            $valid_emails[] = $email;
        }
    }
    return $valid_emails;
}

用法:

$valid_emails = getValidEmails('a@b.com,bbb@bbb,ccc@ddd.com');
if (sizeof($valid_emails))
{
    $to      = implode(',', $valid_emails);
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion() ."\r\n" .
        'MIME-Version: 1.0' . "\r\n" .
        'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    mail($to, $subject, $message, $headers);
}
else {
    // validation failed for email list
}

此功能适用于单个电子邮件以及许多,以逗号分隔。

答案 2 :(得分:1)

你可以尝试

$emails = explode(",", $email_to);
$sendEmail = TRUE;

foreach($emails as $email) {
   if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $sendEmail = FALSE;
       continue;
   }
}

if($sendEmail === TRUE) {
    //send email
}

答案 3 :(得分:1)

您需要先将地址分开,然后逐个验证。

使用explode()获取一系列地址

// assuming $emails = 'mail@domain.com,mail2@domain2.co.uk'
$emails_arr = explode( ',', $emails ); // use comma as separator

现在遍历数组并应用当前逻辑

foreach( $emails_arr as $email )
{
  if(filter_var($email, FILTER_VALIDATE_EMAIL))
  {
    // do something
  }
}

答案 4 :(得分:0)

首先使用逗号作为分隔符拆分电子邮件地址,然后单独验证每个地址。​​