PHP remove email from list of emails - php

I'm trying to send emails after a post creation but trying to omit sending email to the creator of the post.
I get demo3#demo.com, demo2#demo.com, demo4#demo.com by doing
$emails = rtrim($emails,', ');
I'm trying to remove demo3#demo.com
$owners_email = Session::get('user_email'); // which is demo3#demo.com
$emails = rtrim($emails,$owners_email);
but I get demo3#demo.com, demo2#demo.com, demo4

$emails = join(', ', array_diff(array_map('trim', explode(',', $emails)), [$owners_email]));
In detail, you can do these steps individually if you want:
explode(',', $emails): turn your email list into an array
array_map('trim', ...): remove whitespace from the emails
array_diff(..., [$owners_email]): remove the owner's email from the array
join(', ', ...): collapse the emails back into a list

How about this?
http://php.net/manual/en/function.str-replace.php
str_replace($owners_email.",","",$emails);

Related

How can send emails with comma separated

I have multiple emails in one column..
How can send email these email with comma separated?
smith1#gmail.com smith2#gmail.com
$emails = explode(',', $project->emails);
if (!empty($project->emails)) {
Mail::to($emails)->send(new ProjectMail( $project ));
}
coming this error
Swift_RfcComplianceException: Address in mailbox given [smith1#gmail.com smith2#gmail.com] does not comply with RFC 2822, 3.6.2.
First of all, you need to transform this string in an array, that is separated by the comma, using explode() function:
$mails = 'smith1#gmail.com smith2#gmail.com';
$explodedMails = explode(' ', $mails);
This will give you following structure:
=> [
"smith1#gmail.com",
"smith2#gmail.com",
]
Then, you send the email to all the given addresses in $explodedMails array:
Mail::to($explodedMails)->send(new ProjectMail( $project ));
This will send one email to all the addresses from the $explodedEmail array:
I think you can use str_replace and replace space between mail with comma
str_replace("",',',$yourEmailVar);
or you can explode your mails and use cc method for sending mails to all!
Use an explode function and instead of replacing with a comma,replace with the blank space since apparently in the variable mentioned by you,there does not seem to be any commas. Write the code as follows :
$mail_ids = 'smith1#gmail.com smith2#gmail.com';
$mailArray = explode(' ', $mail_ids);
The above explode function will give you an array in proper format like ['smith1#gmail.com','smith2#gmail.com'] and then you can use that array for your purpose of sending email ids.

Sendgrid - dynamic addTo list creation

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

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

How to assign a string to a PHP variable for use in email templates?

I'm working on a template for use in my email system. And i'll send mails to my customers.
This will be my create-email.php file :
and will have a textarea, i need some variables for use on it;
Hello {firstname} {lastname},
Your email address {email} has been added in to our newsletter list.
You can unsubscribe with using this link :
http://example.com/unsubscribe.php?memberid={memberid}&email={email}
This textarea will be POST'ed to send-email.php
and my problem is starting here. I want to replace the {firstname} {lastname} {email} {memberid} with my data's coming from my database.
How can i do this ?
Use str_replace with an array of values.
// $array can be set from a query perhaps?
$array = array('firstname' => 'Bob', 'lastname' => 'Dave',...);
// $template could also be a string which is a large block such as yours above.
$template = file_get_contents($template);
foreach($array as $key => $value) {
$template = str_replace("{$key}", $value, $template);
}
You can also do this with using two arrays, but I do not actually advocate this as its using more memory and is problematic when keeping track of what field matches what value.
// Array of fields to replace
$fields = array('firstname', 'lastname',...);
$values = array('bob', 'dave',...);
$template = file_get_contents($template);
$template = str_replace($fields, $values, $template);
This little Goldie is perfect for you :)
<?php
$email_content = $_POST['email_content'];
$search = array('{firstname}', '{lastname}', '{email}', '{memberid}');
$replace = array('Christian', 'Sany', 'email#email.com', '123123123');
$replaced_content = str_replace($search, $replace, $email_content);
?>

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

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);

Categories