Sending mail to multiple Users in Laravel 5.2 - php

below is my code to send a mail to multiple users.
$email_id = User::select('email_id')->get()->pluck('email_id');
Mail::send('mail', [], function($message) use ($email_id)
{
$message->to($email_id)->subject('Welcome!!!');
});
I m getting the values in $email_id as
["xyz#abc.com","abc#abc.com","qwerty#abc.com"]
With this I get error of
Illegal Offset Type.
But when I write explicitly as
$email_id = ["xyz#abc.com","abc#abc.com","qwerty#abc.com"];
then I am able to send mail to multiple users.
Why is it not working for
$email_id= User::select('email_id')->get()->pluck('email_id');
and is working fine for
$email_id = ["xyz#abc.com","abc#abc.com","qwerty#abc.com"];
Any help would be grateful.

If we want to send only one email at a time. then we can use this code
$email_id = User::select('email_id')->get()->pluck('email_id');
Mail::send('test', array('user' => $email_id) , function ($message) {
$message->from('from#example.com'), 'From Example Name');
$message->to('xyz#gmail.com')->subject('Welcome!!!');
})
If we want to send an email to multiple users , then we can use this code
$email_id = User::select('email')->get()->pluck('email')->toArray();
Mail::send('test', array('user' => $email_id) , function ($message) use
($email_id) { $message->from('from#example.com'), 'From Example Name');
$message->to($email_id)->subject('Welcome!!!');
});

Simply append
->toArray()
function to the code.
$email_id= User::select('email_id')->get()->pluck('email_id')->toArray();
Note: sending mails this way may create bottleneck on the server and eventually force all mails to be delivered to spam/junk folder (if it ever gets delivered). To avoid this, write a function that will queue all mails. Refer to https://laravel.com/docs/5.1/mail#queueing-mail for better clarification.

Related

What is the best option to send emails in laravel with different email template

We are currently using laravel Event listener to send emails for laravel. Basically this is a slot booking option, so sometimes we have to send emails to sender and sometimes we have to send to receiver and sometimes we have to send emails other partners of the slots. In the current case we are using a single Event Listner to send different emails fir the different actions users taking on the slot like cancel meeting, add one more member etc. But generally in the case the email templates would be different only the dunamic variables we need to change.
But in the new case we have to send 4 or 5 emails to different users with different email templates and different contents on a single action. If we plan this in a single event listner, how we can handle this?
$event_id=$event->user['XXXXX'];//event id
$slot_type=$event->user['XXXXX'];//slot type
$notification_type=$event->user['XXXXX']; //slot type
$scheduler_slot_info_ids=$event->user['XXXX'];
$data = $schedulerHelper->getOnetoOneNotificationContents($scheduler_slot_info_ids,$event_id,$slot_type);
$action_trigger_by=$event->user['XXXXX'];
//$data['subject'] = 'CARVRE SEVEN|MEETING CONFIRMED';
$data['subject'] = $event->user['XXXX'];
// $data['template'] = 'emailtemplates.scheduler.oneToOneMeetingConfirmed';
$data['template'] = $event->user['XXXX'];
$invitee_id=Crypt::encryptString($data['XXXX']);
$crypt_event_id=Crypt::encryptString($event_id);
$data['link'] = url('XXXX');
$data['email_admin'] = env('FROM_EMAIL');
$data['mail_from_name'] = env('MAIL_FROM_NAME');
// $data['receiver_email'] = 'XXXXXXX';//$invitee['email'];
//Calling mail helper function
MailHelper::sendMail($data);
Make either a table or hardcoded array with template renderers, then have those renderers render a twig/blade/php template based upon the variables you're supplying and all other variables you'd need for feeding into the mailer.
Then just loop through all your receiving candidates and render the appropriate emails with the correct renderer.
You'll have to make a few utility classes and all to accomplish this, but once you get it up and sorted it will be easy to manage and expand with more templates.
Just a rough outline of what I'd use
protected $renderers = [
'templateA' => '\Foo\Bar\BazEmailRender',
'templateB' => '\Foo\Bar\BbyEmailRender',
'templateC' => '\Foo\Bar\BcxEmailRender',
];
public function getTemplate($name)
{
if(array_key_exists($name, $this->renderers)) {
$clazz = $this->renderers[$name];
return new $clazz();
}
return null;
}
public function handleEmails($list, $action)
{
$mailer = $this->getMailer();
foreach($list as $receiver) {
if(($template = $this->getTemplate($receiver->getFormat()))) {
$template->setVars([
'action' => $action,
'action_name' => $action->getName(),
'action_time' => $action->created_at,
// etc...
]);
$mailer->send($receiver->email, $template->getSubject(), $template->getEmailBody());
}
}
}

CAKEPHP: Sending an email to two users in easy way

Need Help!
Using CakePHP, I need to send the same email to two users, I referred this thing in forum, they all suggesting to declare a array of emails, via for loop could achieve it. But I want to make it simple that, don't wanna go for loop, how to add a one more email account over there. For (e.g. abc#account.com) need to send the same email
What I am doing to send an email for one user is......(Below Codes)
$emailadmin->template('learn_payment', 'default')
->to([$this->request->session()->read('Auth.User.email') => Configure::read('app_title')])
->from([Configure::read('support_email') => Configure::read('app_title')])
->subject(sprintf('You have subscribed a learning on %s', Configure::read('app_title')))
->emailFormat('both')
->send();
Thanks in Advance!
you can use two ways:
$mails = array();
foreach($users as $user)
{
$mails[] = 'email1#email1.com';
$mails[] = 'email2#email2.com';
}
$emailadmin->template('learn_payment', 'default')
->to($mails)
->from([Configure::read('support_email') => Configure::read('app_title')])
->subject(sprintf('You have subscribed a learning on %s', Configure::read('app_title')))
->emailFormat('both')
->send();
Or this way:
$emailadmin = new CakeEmail();
foreach($users as $user) {
$emailadmin->addTo($user['User']['email']);
}
$emailadmin->template('learn_payment', 'default')
->from([Configure::read('support_email') => Configure::read('app_title')])
->subject(sprintf('You have subscribed a learning on %s', Configure::read('app_title')))
->emailFormat('both')
->send();

How to send mail to multiple recipients in Yii2 mailer

This code is working for a single recipient but not for multiple recipients.How should I add multiple recipient into send() function in here.
//find price
$price = MDealPrice::model()->find("id=:id", array(':id' => $id));
// find deal
$deal = MDeal::model()->find("id=:id", array(':id' => $price->dealId));
//send email
app()->mailer->send($sendEmailAdd, 'orderAuthorizeEmail', array('deal' => $deal));
I tried like this also, But not worked.
//send email
app()->mailer->send(array('email1#domain.com','email2#domain.com'), 'orderAuthorizeEmail', array('deal' => $deal));
Thanks in advance!
Please try with below solution it will work
app()->mailer->send('email1#domain.com,email2#domain.com', 'orderAuthorizeEmail', array('deal' => $deal));
just add your email ids comma separated string in send function instead to pass array of email ids.

Passing form input data to the mail view - Laravel

I am making a form which will send an email.
Currently its a generic blade form which points to /admin/newemail
I have my route and for testing the mail is sent from that route:
Route::get('admin/newemail', function()
{
$email = 'email#hotmail.com';
$data = Input::all();
Mail::send('emails.newemail', $data, function($message) use ($email){
// $message details
});
});
And then to trial this I tried in my view: (there is a field name of 'subject' in my form)
echo Input::get("subject");
I actually have two issues. (I am using the log driver)
1) The email is not showing in the log, its just showing [] []
2) The data is not showing neither and its blank.
If I simply have:
echo "hello!";
Then the log will output hello, likewise If i change my mail data variable to an array:
$data = array('test' => 'test');
Then in the view:
echo $test;
That also works. But I want it to take my inputs from my form.
Here, Try this:
Mail::send('emails.welcome', array('key' => 'value'), function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
Here, key will be any name which you want to give and value will be the data you want to assign, it could be form data or could be from database.
Now, after doing that create a file in your views/emails as welcome.blade.php and to fetch the value passed through mail function use:
{{ $key }}
Above is the blade format to represent data.
For more info on mail, visit this, and for blade templates go here.
See, if that helps you.
Mail::send('emails.welcome', Input::all(), function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
and in your view you have
{{Input::get("subject")}}
This is what i use myself and it works perfectly.

Laravel - Mail fails when email address is in variable, works when hardcoded

Im using Laravel class Mail to send emails to customer:
$item = DB::table('questions')->find($id);
var_dump($item->email);
// send mail to customer
Mail::queue('emails.email', $data, function($message) {
$message->to($item->email)->subject('Odpoveď od SCSPPIMKA');
$message->sender('mailer#scspimka.sk');
});
When I hardcode email address in $message->to('example#email.com') everything works fine, but when I use email address in variable: $message->to($item->email) I get error:
Undefined variable: item' in /data/www/scsppimka.local/laravel/vendor/jeremeamia/SuperClosure/src/Jeremeamia/SuperClosure/SerializableClosure.php(99) : eval()'d code:2
Vardump of $item->email shows string with correct e-mail address. What can cause this problem?
You need use use to take $item variable from current context to the context of the anonymous function:
Mail::queue('emails.email', $data, function($message) use ($item) {
$message->to($item->email)->subject('Odpoveď od SCSPPIMKA');
$message->sender('mailer#scspimka.sk');
});

Categories