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!
Related
I have a function to send email to customer for every new registration. and my client want to record any unsuccessfull email sent (i.e if email address is wrong).
i try using try-cacth and mail::failure.
try{
Mail::send('mail.mail', compact(''), function ($message) use ($) {
$message->from();
$message->subject();
$message->to();
});
}
catch(\Swift_TransportException $e){
}
mail:failures
if (Mail::failures()) {
echo ('error');
}
but this working if network failure, such as email host is down. how to record/get info if email wasnt delivered because email address/domain not found.
i get this info in email if email wasnt delivered. but how to record it the ddetails into database.
Address not found
Your message wasn't delivered to asna#asmas.cl because the domain asmas.cl couldn't be found. Check for typos or unnecessary spaces and try again.
look my below code gets a json resposnse after sending an sms, your system should also get a response json after sending the email. The json carrys a set of data. check the particular data response you wish for and update it accordingly on your table.
$responseJson = json_decode($response, true);
// data returned --> [{"status":"200","response":"success","message_id":82682342,"recipient":"4113122"}]
$status = ($responseJson[0])['status'];
$responseDescripition = ($responseJson[0])['response'];
Log::info('API status >>>>> ' . $status);
Log::info('API Response Description >>>>> ' . $responseDescripition);
if (($responseJson[0])) {
DB::table('mess')->where('id', $request->input('template'))
->update([
'sentstatus' => '1',
'status' => $status,
'responsecode' => $responseDescripition,
'sent_on' => \Carbon\Carbon::now(),
]);
}
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');
});
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 a function to send a email for registered users. This is how i am checking if an email should be sent or not.
$email=$details->email;
$subject = 'Looking for blood donor';
$status=Mail::send('emails.welcome', $data, function($message)
use($subject,$email){
$message->from('no-reply#bloodlink.com', 'Blood Link');
$message->bcc('no-reply#bloodlink.com');
$message->to($email)->subject($subject);
});
I am using if for check email sent or not but it not work..
if($status)
{
return Response::json(array('status'=>'success',
'data'=>("Your email has been sent successfully")
), 200);
}else{
return Response::json(array('status'=>'error',
'data'=>("something went wrong..!!")
), 200);
}
The Mail::send() method doesn't return anything.
You can use the Mail::failures() (introduced in 4.1 I think) method to get an array of failed recipients, in your code it would look something like this.
Mail::send('emails.users.reset', compact('user', 'code'), function($m) use ($user)
{
$m->to($user->email)->subject('Activate Your Account');
});
if(count(Mail::failures()) > 0){
$errors = 'Failed to send password reset email, please try again.';
}
Have a look into failure method, here
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 ?