CAKEPHP: Sending an email to two users in easy way - php

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

Related

Laravel sending each e-mail multiple times

When a certain event is fired, my Laravel based app has to send exactly one transactional email to each user in a mailing list.
Here is the loop code:
$users = User::where('notifiable', 1)->get();
foreach($users as $user) {
$info = [
'email' => $user->email,
'name' => $user->name
];
$data = [
'message' => 'Sample text'
];
Mail::send(['emails.html.notification', 'emails.text.notification',], $data, function($message) use ($info) {
$message
->to($info['email'], $info['name'])
->subject('example')
->from('admin#example.com','Example');
});
}
Unfortunately, several users are receiving the same mail multiple times.
I can't figure out what's happening:
When redirecting the emails to log, I see exactly one mail for each user as expected;
Every other events trigger emails to be sent to a single user. However, no one has received multiple emails from these events.
The app is using Sendinblue as an external SMTP service.
Some hints I got:
The hourly mail quota was very low -> the email were received 3 times (and the queue on Sendinblue was immediately filled)
The hourly mail quota was raised 10x -> no more queues on Sendinblue, but now users are receiving the same email up to 7 times!
Apparently, queuing the email and setting a delayed event has solved the problem.
Now a new job is requested every 10 seconds.
$users = User::where('notifiable', 1)->get();
$counter = 0;
foreach($users as $user) {
$payload = [
'email' => $user->email,
'name' => $user->name,
'message' => 'Sample text'
];
dispatch(new SendNotification($payload))
->delay(now()->addSeconds($counter * 10));
$counter++;
}
Thanks for your support!

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

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.

Sending mail to multiple Users in Laravel 5.2

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.

Retrieve All User Lists using Aweber API

I am adding AWeber as an autoresponder in a web application. Using AWeber API, I am able to add a new subscriber to list with a known name which is in this case is firstlist:
$app = new MyApp();
$app->findSubscriber('whtever#aol.com');
$list = $app->findList('firstlist');
$subscriber = array(
'email' => 'someemail#gmail.com',
'name' => 'Name here'
);
$app->addSubscriber($subscriber, $list);
Function definition for findList() is:
function findList($listName) {
try {
$foundLists = $this->account->lists->find(array('name' => $listName));
return $foundLists[0];
}
catch(Exception $exc) {
print $exc;
}
}
As I am developing a public application, so I need to provide users an option to select from their available lists.
Please guide me how I can retrieve all the lists name.
You are returning $foundLists[0] so it will return single list. Try to return foundLists and check what it returns.
This may help you: https://labs.aweber.com/snippets/lists
In short, I pulled the lists by first finding the Aweber User Id so that I could use it in the URL https://api.aweber.com/1.0/accounts/<id>/lists
To find the User ID, I first got the account.
$this->aweber->getAccount($token['access'], $token['secret']);
Then, I retrieve the user's information.
$aweber_user = $this->aweber->loadFromUrl('https://api.aweber.com/1.0/accounts');
From that, I grabbed the user ID with...
$id = $aweber_user->data['entries'][0]['id'];
Once I had the user ID, I could then retrieve their lists with...
$lists = $this->aweber->loadFromUrl('https://api.aweber.com/1.0/accounts/'.$id.'/lists');
This example is more of a procedural approach, of course, I recommend utilizing classes.

Categories