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');
});
Related
I want to send email to multiple user email in PHP automatically by setting a date by the user I mean the user choose a date and hour from input date and I save it on a database then I want to send an email on that date is there any way to PHP do this job automatically because I do this job manually by myself and the number of users is going up and maybe we must send emails in any seconds of a day and I want to do this job in Laravel 5.8 framework when I success on it.
The code I use in a PHP file and run this manually like this:
(I get $UsersEmail array from a database by selecting the date and hour and it's like the array I write in codes).
$UsersEmail = array(
0 => 'user1#example.com',
1 => 'user2#site.com',
2 => 'user3#test.com',
3 => 'user4#somesite.com',
4 => 'user5#anohtersite.com',
);
$AllEmail = '';
foreach($UsersEmail as $user){
$AllEmail .= $user.', ';
}
$to = $AllEmail;
$subject = 'Send Email';
$message = 'This is text message';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = 'From: My site <info#mysite.com>';
mail($to, $subject, $message, implode("\r\n", $headers));
and I want to do this job in Laravel5.8 framework in the future
My PHP version is 7.2.7 and Laravel version I want to use in the future is 5.8.
You can schedule a command that runs maybe every minute or once every hour, which checks whether any mails are at an appropriate time to be sent. In your table, you would have a flag that tells whether the mail was sent in order not to spam the user or whatever logic you want here. Once you get users, dispatch a job.
SendUserMail Command
public function handle()
{
$users = User::where('mail_sent', false)->whereDate('mail_send_date', '<=', Carbon::now())->get();
ProcessUserMails::dispatch($users);
}
In Kernel you have to register the command
protected function schedule(Schedule $schedule)
{
$schedule->command('send-user-mail')->hourly();
}
ProcessUserMails Job
public function handle()
{
foreach ($this->users as $user) {
$user->notify(new SendMailNotification($user));
$user->update(['mail_sent', true);
}
}
SendMailNotification
public function toMail($notifiable)
{
return (new UserMail($this->user))->to($notifiable->email);
}
UserMail
public function build()
{
return $this->subject('User Custom Email')
->markdown('emails.user.custom_mail');
}
This can be a starting point for you. However, be sure to check Laravel docs about creating commands and notifications as I have only included code snippets.
I am trying to determine how to send an email to a group of recipients (as an array) through Laravel's Mail facade, but I would like the emails sent as individual emails as opposed to one bulk email in which all recipients' emails will be viewable.
I have an array of email addresses, $attendees, to whom the email is sent once validation passes. One way I could resolve this is to send to $attendees as 'bcc' vs. 'to,' but ideally I would like the email to be sent 'to' each recipient.
First and foremost, this following block of code DOES work, and is successful at emailing the array of $attendees -- but again, they all receive the email together and are able to view the collection of email addresses/emails of other recipients in the 'to' field when they receive the email, which I do not want to be the case.
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::to(URL::previous())
->withErrors($validator)
->with('subject', $mailDetails['subject'])
->with('body', $body)
->with('eventId', $eventId);
} else {
Mail::send('emails.event_attendees', $mailBody, function($m) use ($mailDetails) {
$m->from('noreply#helpinghabit.com', $mailDetails['orgName']);
$m->to($mailDetails['attendees']);
$m->bcc($mailDetails['sender']);
$m->subject($mailDetails['subject']);
});
return redirect()->back()->with('message', "Your email has been sent!");
};
I tried to accomplish this using a foreach loop like so:
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::to(URL::previous())
->withErrors($validator)
->with('subject', $mailDetails['subject'])
->with('body', $body)
->with('eventId', $eventId);
} else {
foreach ($attendees as $k) {
Mail::send('emails.event_attendees', $mailBody, function($m) use ($mailDetails) {
$m->from('noreply#helpinghabit.com', $mailDetails['orgName']);
$m->to($attendees[$k]);
$m->bcc($mailDetails['sender']);
$m->subject($mailDetails['subject']);
});
};
return redirect()->back()->with('message', "Your email has been sent!");
};
... but this returns an empty response error and tells me that no data was sent.
Any ideas? Thanks in advance for your help!
I have a function in my controller which saves messages from users, first it saves message into database than it sends email to that user's mail address and than it returns json response to the sender.
Problem: sometimes it takes too long for email to be sent, and the sender has to wait long time for the response, or sometimes email is not even sent (due to some smpt problems etc.) and it triggers an error, however I don't really care that much if email is sent or not, most important that message is saved to the database.
What I'm trying to achieve:
I want to save message to the database, ->
immediately after that send response to the sender, ->
and only after run Mail::send();
so run Mail::send() after controller returns json to the sender, so the sender will receive a positive response regardless of how Mail::send() performs
$message = new MessageDB;
$message->listing_id = e(Input::get('listing_id'));
$message->user_id = $listing->User->id;
$message->name = e(Input::get('name'));
$message->mobile = e(Input::get('mobile'));
$message->message = e(Input::get('message'));
if ($message->save()) {
Mail::send('emails.message', ['user' => 'Jon Doe','message' => $message], function($m) use($listing){
$listing_agent = $listing->Agent;
if ($listing->agent == null) {
$mail_to = $listing->User->email;
$name = '';
}else{
$mail_to = $listing->Agent->email;
$name = $listing->Agent->first_name.' '.$listing->Agent->second_name;
}
$m->to($mail_to)->subject('new message from company.com');
});
return ['success' => 1, 'message' => 'messasge has been sent'];
You can use Mail Queueing functions of Laravel. Here's the Link
Mail::queue('emails.welcome', $data, function($message) {});Queue mail for background Sending.
Mail::later(5, 'emails.welcome', $data, function($message){});Define Seconds after which mail will be sent.
I have tried a lot to show send message immediately,but it takes time.
I think queue is not working properly.
In app/config/queue.php I am using
'default' => 'sync'
Please help me out.I am confused what to so now.It is taking time to show success message but I want immediate success message
$tasktime = new Tasktime();
$tasktime->TaskTitle = Input::get('tasktitle');
$tasktime->Description_Task = Input::get('taskdescribe');
$tasktime->Estimated_Time = $case;
$tasktime->Task_Status = Input::get('status');
$tasktime->Priority_Task = Input::get('priority');
$tasktime->Assignee_Id = Input::get('Assignee_Id');
$tasktime->cat_id = Input::get('taskcategories');
$tasktime->Task_DueDate = Input::get('duedate');
$tasktime->Task_created_by = Auth::user()->firstname;
$tasktime->Created_User_Id = Auth::user()->id;
$tasktime->tasktype = Input::get('tasktype');
$tasktime->unsc=$cnt;
$tasktime->save();
// send mail to assignee id
$assigneeUser = User::find(Input::get('Assignee_Id'));
Mail::send(array('html'=>'emails.send'),array('TaskTitle' => Input::get('tasktitle'), 'Priority_Task' => Input::get('priority')), function ($message) use ($assigneeUser) {
$message->to($assigneeUser->email)->subject('verify');
});
return Redirect::to('toggle')->with('message', 'Email has been sent to assignee related to work');
Queuing message means you are not sending it instantly. This process is done in background. To send the email instantly you can use:
Mail::send(array('html.view', 'text.view'), $data, $callback);
REF: laravel 4.2 mail doc
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.