Sent mail to multiple address using Laravel 4 queue - php

I'm using Laravel 4 , tying to send mail to multiple user using Queueing Mail , my code looks like -
$mailuserlist=DB::table('table')
->join('some_table')
->select('some_thing')
->where('somecondition'))->get();
Mail::queue('mail_template', $data, function($message) use ($mailuserlist)
{
$message->from('test#desto.co.in', 'Mail Notification');
foreach ($mailuserlist as $value) {
$message->to($value['email'],$value['firstname'].' '.$value['lastname']);
}
$message->subject('Testing mail');
});
..it's not at all working . How can i send ail to multiple address ??

It should be possible in two ways as we can see in the source code framework/src/Illuminate/Mail/Message.php:
Chaining
Using array
Chaining:
->to($address1, $name1)->to($address2, $name2)->to($address3, $name3)...
Using array of addresses:
->to(array($address1,$address2,$address3,...), array($name1,$name2,$name3,...))

Queueing Mail doesn't seem to support sending 1 mail to multiple user. I think you should queue 1 mail for each of your recipients:
$mailuserlist=DB::table('table')
->join('some_table')
->select('some_thing')
->where('somecondition'))->get();
foreach ($mailuserlist as $mailuser) {
Mail::queue('mail_template', $data, function($message) use ($mailuser) {
$message
->from('test#desto.co.in', 'Mail Notification')
->to($mailuser['email'],
$mailuser['firstname'].' '.$mailuser['lastname'])
->subject('Testing mail');
});
}

You can get all emails to send
$users = User::select('email')->get()->toArray();
And delete the key of array for get an array only with the emails
$emails = array_pluck($users, 'email');
And then run the Mail::queue
Mail::queue('mail.your_view', [], function($message) use ($emails) {
$message->from('test#from.com', 'Mail Notification');
$message->to($emails);
$message->subject('Your Subject');
});

**
Sending multiple users
**
$users = UsersGroup::where(['groups_id' => $group->id])->get();
if(!$users->count()) {
Mail::send('emails.groups.delete-group', [
'group' => $group->title,
], function ($message) use ($group, $users) {
$message->from(Config::get('mail.from.address'), Config::get('mail.from.name'))
->subject(Lang::get('groups.group_deleted'));
foreach ($users as $user) {
$message = $message->to($user->users->email);
}
});
}

Related

How can I iterate the message of a Mail::send Laravel function?

I am trying to iterate and send message to mail in array of emails using Laravel Mail::send function
I searched for same problem and found the code below reference from Radmation here https://stackoverflow.com/a/39625789.
$emails = ['tester#blahdomain.com', 'anotheremail#blahdomian.com'];
Mail::send('emails.lead', ['name' => $name, 'email' => $email,
'phone' => $phone], function ($message) use ($request, $emails)
{
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
//$message->to( $request->input('email') );
$message->to( $emails);
//Add a subject
$message->subject("New Email From Your site");
});
I am wondering the second paramater for iteration usage, so i can message each email with dynamic message of their name.
$emails = ['tester#blahdomain.com', 'anotheremail#blahdomian.com'];
foreach($emails as $currentRecipient){
$customtMsg = //create here a custom msg
Mail::send(['text' => 'view'], $customtMsg, function ($message) use ($request, $currentRecipient)
{
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
$message->to($currentRecipient);
//Add a subject
$message->subject("New Email From Your site");
});
}
Please check usage here
You could put emails in associative array, for example:
$emails = [
'tester#blahdomain.com' => 'tester',
'anotheremail#blahdomian.com' => 'anotheremail'
];
And then iterate over key=>value pairs, like:
foreach($emails as $email=>$name){
Mail::send('emails.lead', ['name' => $name, 'email' => $email], function ($message) use ($email, $name){
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
$message->to($email, $name);
$message->subject("New Email From Your site");
});
}
If you want to send same mail to multiple recipients at once, you could also pass an array of email=>name pairs to the to method:
$message->to($emails)
But I don't think it is possible to customize an email content individualy with that approach. Also in that case, all of the email addresses are visible to every recipient.

how to pass dynamic content(content which is coming from database) to send mail in laravel 5.4

I m new to laravel.i have table email_template and want to send mail to user when user forgot password.i m fetching content dynamically from database but i dont know how to pass it to mail function in laravel.
Mail::send($posts['email_template'], ['USER' =>$post['user] ], function($message)
{
$message->from('test#gmail.com')->subject('Welcome to laravel');
$message->to('test8#gmail.com');
});
where $posts['email_template'] is a content which i want to send and user is a variable which i want to replace in content
Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
$message->from('test#gmail.com', 'Your Application');
$message->to('test8#gmail.com', $user->name)->subject('Welcome to laravel');
});
emails.template is your view - template.blade.php file - /resources/views/emails/template.blade.php
Now, in your view i.e emails.template, you can do:
{{ $user->name }}, {{ $data->address }}
You can Define
ADMIN_EMAIL and CC_EMAIL in the constant file in the config folder
$emailData = array(
'name'=>'toName',
'toEmail'=>$request->email
);
$this->sendEmail($emailData);
Email Function
function sendEmail($emailData){
$this->adminEmail = config('constant.ADMIN_EMAIL');
$this->ccEmail = config('constant.CC_EMAIL');
$this->toEmail = $emailData['toEmail'];
$this->emailTemplate = $emailData['emailTemplate'];
$data['emailInfo'] = array(
'name'=>$emailData['name']
);
Mail::send('emails.yourTemplate', $data, function ($message) {
//$message->attach($pathToFile);
$message->from($this->adminEmail, 'Laravel Email Test');
$message->to($this->toEmail)->cc($this->ccEmail);
});
}

How to pass emails array to mail function in laravel?

$emails is an array but only shows me one email.
Also how to pass the email array into info_on_hand view?
$emails=$request['emails'][$i];
Mail::send('emails.info_on_hand', $data, function ($message) use($emails) {
$message->from('us#example.com', 'Name Company');
foreach($emails as $email):
$message->to($emails)->subject('Status: '.Input::get('desc').' With VIN# '.Input::get('vin').' is '.Input::get('status').'At Name');
endforeach;
});
So to send and pass the array of emails to the view and send it to them is like this
Guessing when you return just $emails it looks like this:
['test#example.com', 'test2#example.com','test3#example.com']
If so use this
$emails = $request['emails'];
Mail::send('emails.info_on_hand', ['emails' => $emails], function ($m) use ($emails) {
$m->from('youremail#eample.com', 'Name');
$m->to($emails);
$m->subject('Your Subject');
});
Another thing is that with the New GDPR laws don't think this is allowed?

Mail in laravel , i want to send email with multiple recepients

I have a problem here.. i try to send email to multiple recepients. The recepients are from my database which name of table is subscribes the message error is like this
ErrorException in SimpleMessage.php line 297:
Illegal offset type
public function store_job(Request $request)
{
$this->validate($request, ['posisi' => 'required','persyaratan' => 'required','tanggung_jawab' => 'required']);
$tambah = new jobs(); //kita buat objek yang terhubung ke table JOBS
$tambah->posisi = $request['posisi'];
$tambah->persyaratan = $request['persyaratan'];
$tambah->tanggung_jawab = $request['tanggung_jawab'];
$tambah->kategori = $request['kategori'];
$tambah->save();
$anu = DB::table('subscribes')->select('email');
$data = array ('email'=>$anu);
Mail::send('emails.news', $data, function ($message) use ($request, $data) {
$message->from('stevanajja#gmail.com',$request->email);
$message->to($data['email'])->subject($request->posisi);;
});
return redirect()->to('/panel_admin/opportune');
}
please help as fast as possibble.. because i am a student, this is my homework for examination.
Here what I'am doing is declaring a new array $emails to store all email from database. By iterating the retrieved object anu, I am pushing the email to $emails and passing it to the to property of the mail.
$anu = DB::table('subscribes')->select('email')->get();
$emails=[];
foreach($anu as $a){
$emails[]=$a->email;
}
Mail::send('emails.news', $emails, function ($message) use ($request, $emails) {
$message->from('stevanajja#gmail.com',$request->email);
$message->to($emails)->subject($request->posisi);;
});
Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.
This question should about match your effort.

how to send text mail in laravel

i want to send text not view by laravel mail , as i use simple form wth textarea to write the content of message , how to seend text or how to convert this text to view ....... thanks
public function sendmail(){
$sendmessage=Input::get('message');
//$messageView=View::make('messageview')->with('message',$sendmessage);
Mail::send($sendmessage, array('name'=>'hossam'),function($message){
$title=Input::get('title');
$mail=Input::get('mailsender');
$message->from($mail,'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($title);
You can create an email view where the only thing it sends is the contents of a variable:
app/views/emails/nonview.blade.php
{{ $contents }}
Then, you can use this view whenever you want to send an email that doesn't have a view:
public function sendmail() {
$from = Input::get('mailsender');
$subject = Input::get('title');
// 'contents' key in array matches variable name used in view
$data = array(
'contents' => Input::get('message')
);
Mail::send('emails.nonview', $data, function($message) use ($from, $subject) {
$message->from($from, 'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($subject);
});
}

Categories