0

I want to create a small script to get every mail with specifics domain name

Here is my code

// $_POST
$email_list_explode = explode("\n",$_POST['email_list']);
$ndd_accepted = explode(",",$_POST['ndd']);

// ARRAY OF GOOD EMAIL
$email_good = array();

foreach ($email_list_explode as $email_list_result){

  // GETTING EMAIL DOMAIN
  $domain_name = substr(strrchr($email_list_result, "@"), 1);
  
  // CHECK IF THE EMAIL DOMAIN IS IN THE ARRAY OF ACCEPTED DOMAIN NAME
  if (in_array($domain_name, $ndd_accepted)){

    // PUSHING GOOD EMAIL IN THE ARRAY OF GOOD EMAIL
    array_push($email_good, $email_list_result);
  }
}
  
// PRINTING ARRAY OF GOOD EMAIL      
print_r($email_good);

Example of $_POST

$_POST['email_list'] = [email protected]
                       [email protected]
$_POST['ndd'] = gmail.com,orange.fr;

My $email_good array should be [email protected]

4
  • So, var_dump($domain_name, $ndd_accepted) and check what is in these variables. Commented Jan 29, 2022 at 18:18
  • in_array does work as expected. You need to filter out everything after @ and then check with in_array. Commented Jan 29, 2022 at 18:20
  • $domain_name = substr(strrchr($email_list_result, "@"), 1); that already filter everything after @ ... Commented Jan 29, 2022 at 18:54
  • string(10) "orange.fr " array(2) { [0]=> string(9) "gmail.com" [1]=> string(9) "orange.fr" } string(6) "sfr.fr" array(2) { [0]=> string(9) "gmail.com" [1]=> string(9) "orange.fr" } Commented Jan 29, 2022 at 18:55

1 Answer 1

1

your problem is in explode with \n you must explode with PHP_EOL like this

<?php
$_POST['email_list'] = "[email protected]
[email protected]";
$_POST['ndd'] = "gmail.com,orange.fr";

$email_list_explode = explode(PHP_EOL,$_POST['email_list']);
$ndd_accepted = explode(",",$_POST['ndd']);
// ARRAY OF GOOD EMAIL
$email_good = array();
foreach ($email_list_explode as $email_list_result){

  // GETTING EMAIL DOMAIN
$domain_name = explode("@",$email_list_result)[1];

// CHECK IF THE EMAIL DOMAIN IS IN THE ARRAY OF ACCEPTED DOMAIN NAME
  if (in_array($domain_name, $ndd_accepted,true)){

// PUSHING GOOD EMAIL IN THE ARRAY OF GOOD EMAIL
    array_push($email_good, $email_list_result);
  }
}

// PRINTING ARRAY OF GOOD EMAIL      
print_r($email_good);


?>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks you! was PHP_EOL :)
yes . good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.