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

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

本文介绍了PHP验证电子邮件地址以逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我尝试使用:

  if(filter_var email_to,FILTER_VALIDATE_EMAIL))

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



喜欢:

  [email protected][email protected] 

这是我的电子邮件功能:

  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 [email protected];
$ 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中的逗号分隔值?

解决方案

尝试这样:

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

$ sResult =($ bValid)? 两个都有效:其中一个不是;


a have a PHP Mail function which sends emails to addresses from a MySQL Database.

In the database, the email addresses are separated by a comma when there are multiple.

i have tried using:

if(filter_var($email_to, FILTER_VALIDATE_EMAIL))

but this only works for one email address, how can i validate multiple separated by a comma?

like:

[email protected],[email protected]

here is my email function:

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 = "[email protected]";
            $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);

how can i validate the comma separated values in the $to and $cc?

解决方案

Try this:

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

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

这篇关于PHP验证电子邮件地址以逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:02