How to show send email message immediately in laravel 4.2 - php

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

Related

Laravel, how to record into DB if email wasn't delivered?

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

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 4 sending mail after returning data from controller

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.

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.

I am using Amazon SES. How will I track the bounce email messages using PHP?

I am using Amazon SES service. But I couldn't understand how will I track the bounce email messages using PHP and keep store those email logs in database. I have a reference link of Amazon blog, but the solution given there is on C#(http://sesblog.amazon.com/post/TxJE1JNZ6T9JXK/Handling-Bounces-and-Complaints).
Need help and assistance.
Thank you.
Create a SNS topic for bounces and complaints and link it to your SES (go to view details tab - Edit config - link the respective SNS complains & bounces topics).
Make sure to subscribe the SNS topic you created either to your mail ID or http/s based on your requirement. Whenever there is bounce or complaint is tagged for the SES message you will receive a JSON data which can later to processed as per your need.
A very helpful AWS webinar follows here: https://www.youtube.com/watch?v=n3Fr0bCsIvo
STEPS TO FOLLOW
Create SNS Topic
Create subscription
Confirm subscription
Code
class AmazonController extends Controller
{
public function handleBounceOrComplaint(Request $request)
{
Log::info($request->json()->all());
$data = $request->json()->all();
if($request->json('Type') == 'SubscriptionConfirmation')
Log::info("SubscriptionConfirmation came at: ".$data['Timestamp']);
if($request->json('Type') == 'Notification'){
$message = $request->json('Message');
switch($message['notificationType']){
case 'Bounce':
$bounce = $message['bounce'];
foreach ($bounce['bouncedRecipients'] as $bouncedRecipient){
$emailAddress = $bouncedRecipient['emailAddress'];
$emailRecord = WrongEmail::firstOrCreate(['email' => $emailAddress, 'problem_type' => 'Bounce']);
if($emailRecord){
$emailRecord->increment('repeated_attempts',1);
}
}
break;
case 'Complaint':
$complaint = $message['complaint'];
foreach($complaint['complainedRecipients'] as $complainedRecipient){
$emailAddress = $complainedRecipient['emailAddress'];
$emailRecord = WrongEmail::firstOrCreate(['email' => $emailAddress, 'problem_type' => 'Complaint']);
if($emailRecord){
$emailRecord->increment('repeated_attempts',1);
}
}
break;
default:
// Do Nothing
break;
}
}
return Response::json(['status' => 200, "message" => 'success']);
}
}

Categories