2

a 有一个 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 中的逗号分隔值?

4

5 回答 5

4

尝试这个:

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:50:27.140 回答
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
}

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

于 2013-10-17T11:20:55.303 回答
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
}
于 2013-10-17T11:14:44.567 回答
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
  }
}
于 2013-10-17T11:15:14.687 回答
0

首先使用逗号作为分隔符拆分电子邮件地址并分别验证。

于 2013-10-17T11:14:47.613 回答