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
Related
$to="example#.com,example#.com"; // i want this email addresses one by one in $tto in mail function
$contacts = array("$to");
foreach($contacts as $contact)
{
$tto = $contact;
$subject="hey";
$body="Test";
$header="example#.com";
if(mail($tto,$subject,$body,$header))
{
echo "SEND";
}}
?>
my $to variable contains email addresses but i want it one by one in
mail function that it gets first email and send it ,then move on
second address, i have tried this code but this is not working
to change/split a string into an array we can use explode function:
in case they are always in comma-separated form:
$to = "example#domain.com,example2#domain.com";
$contacts = explode(",", $to);
array_walk($contacts, 'trim');
// The rest of your code ... (starting from 'foreach')
I'm trying to use sendgrid to send emails from my app. To add email addresses via php, according to the sendgrid docs, you use the following:
$email = new SendGrid\Email();
$email
->addTo("example#email.com")
where addTo is used for each email address. I would like like to generate the addTo list dynamically, for example I have a list of 3 emails in a php variable that I would like to send mails to:
$email_addresses = 'example#email1.com, example#email2.com. example#email3.com';
I've tried the following to echo out individual ->addTo properties per email address however it doesn't work:
$email = new SendGrid\Email();
$email
$string = $email_addresses;
$string = preg_replace('/\.$/', '', $string); //Remove dot at end if exists
$array = explode(', ', $string); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
echo '->addTo("'.$value.'")<br>';
}
Does anyone know what I'm doing wrong here?
There's a setTos method in sendgrid-php that handles arrays: https://github.com/sendgrid/sendgrid-php#settos
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";
}
}
}
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']
}
I use the filter_var PHP function to validate email address when a user signs up to my site.
I use this code from the post:
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
then later I do:
if(!$email) {
// return to the form
}
else {
// send registration info
}
now when I var_dump($email), I get the output:
string(23) "user."name"#example.com"
I would like to know why this does not return false. I think the double quotes are not acceptable, so why does PHP say it’s valid?
It is a valid email address :
A quoted string may exist as a dot separated entity within the
local-part or it may exist when the outermost quotes are the outermost
chars of the local-part (e.g. abc."defghi".xyz#example.com or
"abcdefghixyz"#example.com are allowed. abc"defghi"xyz#example.com is
not; neither is abc\"def\"ghi#example.com).
I had the same problem (see Dalmas on why it's valid) and here's how I fixed it:
filter_var($email, FILTER_SANITIZE_EMAIL);
eg:
$email = 'user."name"#example.com';
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
will output:
string(21) "user.name#example.com"
Then you can validate the email using your validation.
you can get more information on the php site