Laravel mailJob not showing up in telescope - laravel

I send email with MailJob class. But i dont see any job in telescope jobs section. How can i fix that?
Here is my LoginListener class which i send email when user login
public function handle(Login $event)
{
//return dd($event->user->email);
dispatch(new SendEmailJob($event->user->email,'Xoşgəldiniz','Salam .' .$event->user->name .'xoşgəldiniz....'))->onQueue('mails');;
}
Here is my my WelcomeEmail mail class
public function __construct($title, $content)
{
$this->title = $title;
$this->content = $content;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('email.welcome');
}
}

Related

How to add PDF attachment in queue mails In Laravel?

I was able to send an email but when I put the attachData() in the UserMail there was an error. I think because of the parameter $this->pdf that should be declared in UserEmailJob, and I don't know how to fix it.
UserEmailJob
public $details;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new UserEmail();
Mail::to($this->details['email'])->send($email);
}
UserEmail - mail class
I'm having error with this line $this->pdf->output()
public function __construct()
{
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('info#gmail.com', 'Mailtrap')
->subject('Stock Report - Laravel Tutorial')
->view('emails.userEmail')
->attachData($this->pdf->output(), 'stock_report.pdf');
}
UserController
public function mailClass()
{
$users = User::all();
$data['name'] = 'Hi';
$pdf = app('dompdf.wrapper');
$pdf->loadView('cert', $data);
$pdf->setPaper('A4', 'landscape');
$details = ['email' => 'abc#gamil.com'];
UserEmailJob::dispatch($details);
return response()->json(['status' => 'success', 'message' => 'Report has been sent successfully.']);
}
Because $this->pdf is not defined in your class.
try this:
private $pdf;
public function __construct($pdf) {
$this->pdf = $pdf;
}
public function build(){
return $this->from('info#gmail.com', 'Mailtrap')
->subject('Stock Report - Laravel Tutorial')
->view('emails.userEmail')
->attach($this->pdf);
}
and in your UserController
UserEmailJob::dispatch($details, $pdf->output());
So modify it
public $details;
public $pdfStream;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($details, $pdfStream)
{
$this->details = $details;
$this->pdfStream = $pdfStream;
}
public function handle()
{
$email = new UserEmail($this->pdfStream);
Mail::to($this->details['email'])->send($email);
}
EDIT: it is probably not recommended to stream the pdf via json
UserEmailJob
public $details;
public $data;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($details, $data)
{
$this->details = $details;
$this->data = $data;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new UserEmail($this->data);
Mail::to($this->details['email'])->send($email);
}
UserEmail
private $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$pdf = app('dompdf.wrapper');
$pdf->loadView('cert', $this->data);
$pdf->setPaper('A4', 'landscape');
return $this->from('info#gmail.com', 'Mailtrap')
->subject('Stock Report - Laravel Tutorial')
->view('emails.userEmail')
->attachData($pdf->output(), 'stock_report.pdf');
}
UserController
public function mailClass()
{
$users = User::all();
$data['name'] = 'Hi';
$details = ['email' => 'abc#gamil.com'];
UserEmailJob::dispatch($details, $data);
return response()->json(['status' => 'success', 'message' => 'Report has been sent successfully.']);
}

send multiple emails using custom job in laravel

I tried to create a job in order to send emails to all users in the database I have done everything and connected successfully with Mailtrip but still have a problem : (Failed) when implementing the command:
PHP artisan queue:work
this is my ProductEmail class:
class ProductMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $product;
public $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(Product $product, User $user)
{
$this->product = $product;
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject($this->product->name)
->view('email.product');
}
}
and i have created the view for it>>>>
and here the job class
class NotifyUsersForProduct implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public $product;
public function __construct(Product $product)
{
$this->$product = $product;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$users = User::all();
$users->map(function(User $user){
Mail::to($user)->send(new ProductMail($this->product, $user));
});
}
}
and here i have use it
try {
$product = Product::create([
'name' => $request->input('name'),
'price' => $request->input('price'),
'quantity' => $request->input('quantity'),
'user_id' => Auth::id(),
]);
NotifyUsersForProduct::dispatch($product);
the error message:
public function __construct(Product $product)
{
$this->$product = $product;
}
This should be:
public function __construct(Product $product)
{
$this->product = $product;
}
Remove $
and also i recommend to do this but not necessary:
public function handle()
{
$users = User::all();
foreach($users as $user){
Mail::to($user)->send(new ProductMail($this->product, $user));
});
}
Hope it helps.

Laravel - cannot send message without a sender address

I have trouble with sending multiple jobs in Laravel.
Sometimes it working all right.
Sometimes I got this message cannot send message without a sender address in failed_job table and just 1 one worked.
Sometimes both not working.
And that's in my local but on the server it not working at all.
I'm trying to clear cache, config, everything but it just not work at all.
And I don't think it because my setting on .env cause it not always got errors.
Just sometimes got errors sometimes work.
The most common error is 1 working and 1 not.
It just running 1 job and 1 job will throw fail in failed_job table
JOBS
class SendContactContentEmail implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $data;
public $emails;
public $tries = 5;
/**
* Create a new job instance.
*
* #param array $_data
*/
public function __construct(
$_data
)
{
$this->data = $_data;
$this->emails = config('email.admin');
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$content = new ContactContentEmail($this->data);
if (!empty($this->emails)){
foreach ($this->emails as $email){
Mail::to($email)->send($content);
}
}
}
}
class SendContactEmail implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $email_address;
public $tries = 5;
/**
* Create a new job instance.
*
* #param string $_email_address
*/
public function __construct(
string $_email_address
)
{
$this->email_address = $_email_address;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new ContactEmail();
Mail::to($this->email_address)->send($email);
}
}
Mails
class ContactContentEmail extends Mailable
{
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($_data)
{
$this->data = $_data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('contact.emails.contact_content')->with([
'data' => $this->data
]);
}
}
class ContactEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('contact.emails.contact');
}
}
Controller
public function send(ContactFormRequest $request){
$data = $request->validated();
dispatch(new SendContactContentEmail($data));
dispatch(new SendContactEmail($data['mail_address']));
}
Config Email
return [
'admin' => [
'kuriyama' => 'xxxx#gmail.com',
'negishi' => 'aaaa#gmail.com',
]
];
ENV
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=contact#xxx.com.vn
MAIL_PASSWORD=xxxxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=xxx#xxx.com
MAIL_FROM_NAME="${APP_NAME}"
Can you call this command?
php artisan config:cache

laravel notification does not work for me

I want to send an email to user with a message as below:
MessageController.php:
public function store(Request $request)
{
$user_id = auth()->user()->id;
$message = new Message;
$message->title = $request->title;
$message->body = $request->body;
$message->offer_id = $request->offer_id;
$message->user_id = $user_id;
$message->with_profile = $request->employeeProfile;
$message->save();
//return response()->json($message); -> this gives correct message
$offer = Offer::where('id', '=', $request->offer_id)->first();
//here I'm trying to get user Id, basing on offer (I knot I should use other way, but I'll correct it later
$user = User::where('id', '=', $offer->user_id)->first();
$user->notify(new OfferMessage($message)); -> this gives error "Undefined variable: message"
return response()->json(['created' => true], 201);
}
The problem is that it gives me an error: "Undefined variable: message", while I'm reciving correct message when I uncoment "return response()->json($message);"
What am I doing wrong here?
edit:
My Message.php class: (I don't have OfferMessage class)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use App\Notifications\OfferMessage;
class Message extends Model
{
use Notifiable;
}
edit2: Notifications/OfferMessage.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Message;
class OfferMessage extends Notification
{
use Queueable;
public $message;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('You've got new message: '.$message->offer_id)
->line($message->title)
->line($message->body)
->line('Sent by:'.$message->user_id)
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Change your OfferMessage Class toMail method
Change $message TO $this->message
public function toMail($notifiable)
{
return (new MailMessage)
->line("You've got new message: ".$this->message->offer_id)
->line($this->message->title)
->line($this->message->body)
->line('Sent by:'.$this->message->user_id)
->line('Thank you for using our application!');
}

Laravel 5.3 How to show Username in Notifications Email

I am trying to add the user's first name in the notification emails. At the moment, Laravel notification emails starts like:
Hello,
And I want to change it to:
Hello Donald,
Right now, I have a set-up like this. This example is for a Password Reset Notification email:
User Model:
public function sendPasswordResetNotification($token)
{
$this->notify(new PasswordReset($token));
}
App\Notifications\PasswordReset:
class PasswordReset extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Is the User Model automatically binded with the Notification Class? How can I add the username in the view?
The $notifiable variable passed to toMail() is User model.
Call to needed User model attribute, easy:
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello '. $notifiable->username)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
Try this:
User Model:
public function sendPasswordResetNotification($token) {
return $this->notify(new PasswordReset($token, $this->username));
}
App\Notifications\PasswordReset:
class PasswordReset extends Notification
{
use Queueable;
public $username;
public function __construct($token, $username)
{
$this->username = $username;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello '.$this->username.',')
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
}
You have to edit toMail function in App\Notifications\PasswordReset to set greeting as you want.
public function toMail($notifiable) {
return (new MailMessage)
->greeting('Hello '. $this->username)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
Update
To set $username, have to define a variable & setter method in App\Notifications\PasswordReset.
protected $username = null;
public function setName($name) {
$this->username = $name;
}
When you initialize App\Notifications\PasswordReset, you can set the name.
In User model update the function as below.
public function sendPasswordResetNotification($token) {
$resetNotification = new ResetPasswordNotification($token);
$resetNotification->setName($this->name);
$this->notify($resetNotification);
}

Resources