Trying to send email to multiple users in Laravel 5.2 - php

I want to send email to multiple users. Here is my email sending code.
$email_id = User::select('email_id')->get();
Mail::send('test' , array('user' => $email_id) , function ($message) {
$message -> to('xyz#gmail.com') -> subject ('Welcome!!!');
});
I am getting array of email_id while printing $email_id and I pass $email_id to array. but it's not working.
Any help would be grateful.
Thank You.

Please check mention url you can use this
Laravel Mail::send() sending to multiple to or bcc addresses
https://codedump.io/share/7tL5qbeUwnKT/1/laravel-mailsend-sending-to-multiple-to-or-bcc-addresses

Try this:
$email_id = User::select('email_id')->get()->toArray();
Mail::send('test' , array('user' => $email_id) , function ($message) {
$message -> to('xyz#gmail.com') -> subject ('Welcome!!!');
});

Related

sending multiple emails using queue - laravel 7

I need to send bulk emails using Laravel queue and jobs. If I understood, my method this way should dispatch 1 job where all the emails are fetched and send it one by one going through the foreach loop, right? Somehow, only one email got send. And when I check the message, it appears the recipient message is in this format - "test2#gmail.com" <test1#gmail.com>. Only test1 email account received the email. I am not sure what causing it. Thank you for your help.
Controller
$body = $request->body;
$titleName = $request->subject;
$job = (new \App\Jobs\SendQueueEmail($body, $titleName))
->delay(now()->addSeconds(2));
dispatch($job);
Job
public function handle(Request $request)
{
$emailsAlumni = ['test1#gmail.com', 'test2#gmail.com'];
$date = Carbon::now()->format('d M Y');
$data = [
"body" => $this->body,
"date" => $date
];
foreach ($emailsAlumni as $email) {
Mail::send('main.admin.email.general', $data, function ($message) use ($email) {
$message->to($email);
$message->subject('title');
});
}
}
You don't need to loop whole Mail instance you can just try it as
Mail::send('main.admin.email.general', $data, function ($message) use ($emailsAlumni) {
$message->to($emailsAlumni);
$message->subject('title');
});

Laravel 5 sends email only to the first address in array

Here's the code I'm using to send an e-mail to multiple recipients:
$recipients = ['myoneemail#esomething.com', 'myother#esomething.com','myother2#esomething.com'];
Mail::send(['admin.emails.email.html', 'admin.emails.email.text'], ['content' => $request->input('message')],
function ($message) use ($request, $recipients) {
$message
->to($recipients)
->subject($request->input('subject'))
->replyTo('xxx#xxx.com', 'Me');
});
I'm using Mailtrap.io as a fake inbox to catch the e-mails. I only get the mail sent to "myoneemail#esomething.com", not the other ones.
But why ?

laravel 5 send email with html elements and attachment using ajax

I am stuck on the point where i want to send an email using ajax. Find code below.
$user = \Auth::user();
Mail::send('emails.reminder', ['user' => $user], function ($message) use ($user) {
$message->from('xyz#gmail.com', 'From');
$message->to($request['to'], $name = null);
// $message->cc($address, $name = null);
// $message->bcc($address, $name = null);
$message->replyTo('xyz#gmail.com', 'Sender');
$message->subject($request['subject']);
// $message->priority($level);
if(isset($request['attachment'])){
$message->attach($request['attachment'], $options = []);
}
// Attach a file from a raw $data string...
$message->attachData($request['message'], $name = 'Dummy name', $options = []);
// Get the underlying SwiftMailer message instance...
$message->getSwiftMessage();
});
return \Response::json(['response' => 200]);
Now i want to send this email using ajax and upon request complete i want to display message on same page that your message sent successfully.
i found the solution for this, but this is to send plain message directly using Mail::raw() function which is working perfectly. But i am not able to attach files here or some html codes.
Any suggestion in this regard.

Laravel mail cannot send from field

Im trying to send mail from laravel and when i add the dynamic from field i get this error:
"Expected response code 250 but got code "501", with message "501 A syntax error was encountered in command argument.."
this is the code:
$user = Input::get('user');
Mail::send('template.contact', $user , function($message) use ($user)
{
$email = $user['email'];
$message->from($email , 'name'); thats doesnt
//$message->from('us#example.com', 'Laravel'); that work
$message->to('test#gmail.com', 'contact us' )->subject($user['subject']);
});
and the user is coming from angular -
service:
this.sendConatctMail = function(data) {
return $http.post('send-contact-mail', {user: data});
}
and controller:
contactService.sendConatctMail($scope.user);
Here's one way you can solve this.
Let's assume the data on this.sendConatctMail = function(data) { is a object like this:
var data {
email: 'some#email.com',
// other field: values
}
Right before you post it, you should convert it into JSON string like this:
return $http.post('send-contact-mail', {user: JSON.stringify(data)});
Then on Laravel/PHP side, decode that back into an array and use it like this:
if (Input::has('user'))
{
// Decode json string
$user = #json_decode(Input::get('user'), true);
// Proceed if json decoding was success
if ($user)
{
// send email
Mail::send('template.contact', $user , function($message) use (&$user)
{
$message->from($user['email'], 'name')
->to('test#gmail.com', 'contact us')
->subject($user['subject']);
});
}
}
I don't know if this will help you, but whenever I send emails through Laravel, I need to alter the use(...) section a bit. You have:
$user = Input::get('user');
Mail::send('template.contact', $user , function($message) use ($user)
...
Try changing it to this and see what happens:
$user = Input::get('user');
Mail::send('template.contact', array('user' => $user), function($message) use (&$user) {
$message->from($user['email'], $user['name']);
$message->to('test#gmail.com', 'contact us')->subject($user['subject']);
}
2 changes I made:
array('user' => $user)
and
use(&$user)
I don't know if that will help you, but I have working emails on my application that look almost identical to yours, except for the &$variable instead of just $variable
Good luck!
The problem is with your $email = $user['email'];
try checking your $user['email']
if $message->from('us#example.com', 'Laravel'); this works
then
$email = $user['email'];
$message->from($email , 'name');
this must work...
I have tried the same without much trouble...
In my application I needed to pass the _token variable in all my ajax request any such problems??

Laravel Not sending Email

am having some sort of an issue with sending mails from my website with Laravel 4, I followed the on-line documentation but haven't successfully sent a mail with Laravel my code is as show below.
Mail::send('emails.contactmail', $data, function($message)
{
$name = Input::get('name');
$email = Input::get('email');
$message->from($email, $name);
$message->to('info#mysite.org', 'Info at My Site')
->subject('Website contact form');
});
I use following code for sending temporary password in case of forgot password
public function postForgotPassword(ForgotPasswordRequest $request){
$email=$request->email;
$objUser=DB::table('users')
->where('email','=',$email)
->select('email','id','first_name','last_name','user_group_id')
->first();
$string = str_random(15);
$pass=\Hash::make($string);
$objUser2=User::find($objUser->id);
$CURRENT_TIMESTAMP=new DateTime();
$objUser2->temporary_pass=$pass;
$objUser2->pass_status=1;
$objUser2->updated_at=$CURRENT_TIMESTAMP;
$objUser2->save();
$data=array("email"=>$email,"pass"=>$string,"first_name"=>$objUser->first_name,"last_name"=>$objUser->last_name);
$email=array("email"=>$email);
Mail::send('emails.forgot_password',$data, function($message) use($email) {
$message->to($email['email'],'MyProject')->subject('Password Recovery ');
});
return Redirect::to('auth/login')->with('flash_message','Check your email account for temporary password');
}
And I write email template in forgot_password.blade.php which is located in view.emails folder.

Categories