How can we check the Country Specific Domain's email through Regex - php

I would like to know the proper regex which allows me to remove specific email domains from the list. Example: gmail.com, hotmail.com, .co.in, aol.com, yahoo.com, .gov, .edu, etc.
I just want to filter the results through PHP Regex.

I'm not entirely sure what you're after, but if you're trying to get rid of certain domains from the email address:
$email = preg_replace(array(
"/gmail.com/",
"/hotmail.com/",
"/.co.in/",
"/aol.com/",
"/yahoo.com/"), "", $email);
Or if you're trying to get rid of the domain you could do
$email = preg_replace("/#.*?$/", "", $email);

Do you mean you want something like this:
//$yourFilterList can be array containing gmail.com, hotmail.com , etc
foreach($emails as $key => $email) {
$checkFor = array_pop(explode("#", $email));
if(in_array($checkFor, $yourFilterList)) {
unset($email[$key]);
}
}
//Again do you mean sorting
sort($emails);

Related

How to check email id's with specific domain from the array of email id's in PHP?

I've an array of email ids. I want to check each and every email id for it's domain.
Actually, I've to parse over this array whenever there is email id found with no '.edu' domain, error message should be thrown as 'Please enter valid .edu id' and further emails from the array should not be checked.
How should I achieve this in efficient and reliable way?
Following is my code of array which contains the email ids. The array could be empty, contain single element or multiple element. It should work for all of these scenarios with proper validation and error messages.
$aVals = $request_data;
$aVals['invite_emails'] = implode(', ', $aVals['invite_emails']);
$aVals['invite_emails'] contains the list of email ids received in request.
Please let me know if you need any further information regarding my requirement if it's not clear to you.
Thanks in advance.
You can do something like this,
Updated:
// consider $aVals['invite_emails'] being your array of email ids
// $aVals['invite_emails'] = array("rajdeep#mit.edu", "subhadeep#gmail.com");
if(!empty($aVals['invite_emails'])){ //checks if the array is empty
foreach($aVals['invite_emails'] as $email){ // loop through each email
$domains = explode(".",explode("#",$email)[1]); // extract the top level domains from the email address
if(!in_array("edu", $domains)){ // check if edu domain exists or not
echo "Please enter valid .edu id";
break; // further emails from the array will not be checked
}
}
}
As the email id is always composed by 3 chars, You can also do something like this:
foreach($aVals['invite_emails'] as $email){
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid email
if(substr($email, -3) != "edu") {
echo "Please enter valid .edu id";
}
}
}

PHP - email validation fails

From a multi-line text field I get several email addresses separated by new line. Field value is stored in database as a JSON string:
{"admin_emails":"email1#domain.com\r\nemail2#domain.com"}
Then I decode the emails using json_decode and explode:
$emailStr = json_decode($stringFromDb)->admin_emails;
$emails = explode("\r\n", str_replace("\r", "\r\n", $emailStr) );
next, for each email in table, I check if it's valid and do some stuff:
foreach( $emails as $email )
{
if( filter_var($email, FILTER_VALIDATE_EMAIL) ){
// do stuff with email
}
}
But I have following problem: only the first email is recognized by the filter_val as valid email address. All subsequent emails are filtered out. What causes that these emails are not recognized as valid e-mail addresses?
It looks like you're replacing "email1#domain.com\r\nemail2#domain.com" with "email1#domain.com\r\r\nemail2#domain.com" - In other words \r\r\n. So all emails after the first start with a line break and thus are invalid.
You could simply do without the str_replace() all together
Maximus is correct:
<?php
$s ='{"admin_emails":"email1#domain.com\r\nemail2#domain.com"}';
//Then I decode the emails using json_decode and explode:
$emailStr = json_decode($s)->admin_emails;
$emails = explode("\r\n", $emailStr);
//next, for each email in table, I check if it's valid and do some stuff:
foreach( $emails as $email ) {
if( filter_var($email, FILTER_VALIDATE_EMAIL) ){
echo $email." is valid\n\n";
// do stuff with email
}
}
This yields:
email1#domain.com is valid
email2#domain.com is valid

PHP change string parsed from email

Using a PHP script file to filter emails. Everything is working properly, but it seems that if I were to initiate a conversation between a cell phone (Verizon Wireless) and the SMTP server, Verizon changes the format of outgoing messages.
For example, I need to respond to an email using XXX#vtext.com but Verizon will respond with XXX#vzwpix.com which cannot be responded to. So I created the following code to try and remove and replace the #vzwpix.com but it still isn't sending the mail. I know the code is working however because if I change the $from in the mail function to XXX#vtext.com the code works and sends the message.
//Parse "from"
$from1 = explode ("\nFrom: ", $email);
$from2 = explode ("\n", $from1[1]);
if(strpos ($from2[0], '<') !== false)
{
$from3 = explode ('<', $from2[0]);
$from4 = explode ('>', $from3[1]);
$from = $from4[0];
}
else
{
$from = $from2[0];
}
if(strpos ($from[1], '#vzwpix.com') !== false) {
str_replace('#vzwpix.com', '#vtext.com', $from);
}
var_dump( mail( $from, "Hi", "What is the email:"));
I am using var_dump just for developmental purposes by the way.
I did some research and I think the best way to do this maybe is to explode the XXX#vzwpix.com at the # symbol, then run a str_replace('vzwpix.com','vtext.com', $from5); However, that didn't work and then I thought maybe implode it after running a str_replace?
If the bad domains are static, why don't you explode on 'vzwpix' and then implode on 'vtext'? This should replace each of the bad instances in you address string.
You forgot to get the result of your str_replace
$from_with_vtext=str_replace('#vzwpix.com', '#vtext.com', $from[1]);

PHP: Email Parsing - Multiple Recipients

I am piping email that get's sent to my server to a PHP script. The script parses the email so I can assign variables.
My problem is once in awhile somebody will include my email address to an email that's getting sent to multiple recipients and my script will only get the first one. I need it to find my email address then assign it to a variable.
Here is what the email array looks like: http://pastebin.com/0gdQsBYd
Using the example above I would need to get the 4th recipient: my_user_email#mydomain.com
Here is the code I am using to get the "To -> name" and "To -> address"
# Get the name and email of the recipient
$toName = $results['To'][0]['name'];
$toEmail = $results['To'][0]['address'];
I assume I need to do a foreach($results['To'] as $to) then a preg_match but I am not good with regular expressions to find the email I want.
Some help is appreciated. Thank you.
Instead of usin preg_match inside foreach loop you can use strstr like below
Supposing you are looking for my_user_email#mydomain.com use following code
foreach($results['To'] as $to)
{
// gets value occuring before the #, if you change the 3 rd parameter to false returns domain name
$user = strstr($to, '#', true) ;
if($user == 'my_user_email')
{
//your action code goes here
}
}
example:
<?php
$email = 'name#example.com';
$domain = strstr($email, '#');
echo $domain; // prints #example.com
$user = strstr($email, '#', true); // As of PHP 5.3.0
echo $user; // prints name
?>
Actually, you do not need to use a regexp at all. Instead you can use a PHP for statement that loops through your array of To addresses.
$count = count($root['To']);
for ($i=0; $i < $count; $i++) {
//Do something with your To array here using $root['To'][$i]['address']
}

PHP Foreach Loop mail()

My following script runs fine when a single email address is entered into the textarea, but once two are entered, no emails are sent. What am I doing wrong?
if($_POST['submit']=='Send Email') {
$email_addresses = explode(",\n", $_POST['email']);
foreach($email_addresses as $email_address){
$email_address = trim($email_address);
send_mail( 'noreply#noreply.com',
$email_address,
'Test Email',
"Hello This Email Is A Test");
}
}
var_dump($email_addresses) results in this
array(1) { [0]=> string(39) "email1#test.com email2#test.com" }
You're using the same variable name twice
foreach($email_addresses as $email_addresses)
so on the second loop, the source is overwritten
Edit:
please post the output of
var_dump($_POST['email']);
var_dump(explode(",\n", $_POST['email']));
It should be:
foreach($email_addresses as $email_address){
$email_address = trim($email_address);
send_mail( 'noreply#noreply.com',
$email_address,
'Test Email',
"Hello This Email Is A Test");
}
Also splitting using explode on ",\n" separator isn't a good idea (people can send ",\r\n" in some cases). Provide multiple fields or use preg_split().
If it doesn't work try var_dump($email_address); after the explode() function to get the information what exactly happens with the input (and so you can see that input is actually correct).
UPDATE: As you can clearly see there is no \n in $email_address. It depends on your HTML form.
For a quick fix just explode(', ', $email_addresses); Also - you missed , in your input, which you require to explode that string.
obviously a#b.com b#c.com is not a valid email, and my psychic powers tell me that this is not the correct way to do this. What if I enter something else than an email address? Try to sanitise the data you receive from user. You can not trust user input blindly.
foreach($email_addresses as $email_addresses){
Means that you are overwriting the source array ($email_addresses) with the first entry in the array, because they are the same variable name. Unfortunately, PHP throw an error on this, so you end up rewriting your array with the first email address, and then prematurely (to your needs) exiting the loop (though this is expected and logical behaviour).

Categories