Hi everyone i'm just trying to queue my email with attachment in laravel project, but it does not seems to work. when i try to send an attachment without queue my mail it is working absolutely fine, but when i put my email in queue it just stop working. email is sent but attachment doesn't.
here is my code for email in controller
Mail::to($request->email)->send(new ContactUsEmail($contact_us));
and below code is from my mail job
class ContactUsEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$extension = explode('.',$this->data->attachment)[1];
return $this->subject($this->data->topic)->attach(public_path('/storage/attachments/contact-us/' . $this->data->attachment),
[
'as' => $this->data->attachment,
'mime' => 'application/'. $extension
])->markdown('mail.contact-us' ,[
'subject' => $this->data->subject,
'name' => $this->data->name,
'email' => $this->data->email,
'description' => $this->data->description,
]);
}
}
please let me know if i'm doing something wrong or missing something.
i appreciate your response
Related
I already edited my .env to QUEUE_CONNECTION=database and tried to replace from send to queue. I also tried to add implements ShouldQueue to my CertificatEmail but not working. I don't know what's missing.
SendMail Controller
public function sendEmail(Request $request)
{
$users = StudentApplicants::whereIn("id", $request->ids)->get();
foreach ($users as $key => $user) {
$data = [
'fname' => $user->users->first_name,
'mname' => $user->users->middle_name,
'lname' => $user->users->last_name,
'gwa' => $user->gwa,
'sy' => $user->school_year
];
$qrcode = base64_encode(QrCode::format('svg')->color(128, 0, 0)->size(200)->errorCorrection('H')->generate($user->users->stud_num));
$pdf = app('dompdf.wrapper');
$pdf->loadView('admin.send-awardees-certificates.certificate', $data, array('qrcode' => $qrcode));
$pdf->setPaper('A4', 'landscape');
Mail::to(config('mail.notification_recipient'))->queue(new CertificateEmail($user, $pdf));
}
return response()->json(['success' => 'Send email successfully. Refresh the page']);
}
CertificateEmail from mail
class CertificateEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $pdf;
public $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user, $pdf)
{
$this->pdf = $pdf;
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
// return $this->view('view.name');
return $this->from(env('info#gmail.com'))
->subject('Certificate from ABC EFG')
->view('email.certificate-email')
->attachData($this->pdf->output(), 'stock_report.pdf');
}
}
Run these two queries in command
php artisan queue:table # creates a table for queued jobs
php artisan migrate # migrates the table
Then run
php artisan queue:work
In from env I think it doesn't write right please review it should be USER_EMAIL or something like that
I would like to know how to include an attachment when sending an email using laravel and markdown.
This is the class InvoiceEmail extends Mailable
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('tes#test.com')->subject('Order')->markdown('emails.invoiceEmail')->with('data',$this->data);
}
In the controller called OrderController I send the email:
$customerPDF = 'file.pdf';
$data = array(
'name' => $request->vendor_name,
'company' => $request->company,
'vat'=> $request->vat,
'category' => $request->category,
'url' => Route('vendor.reg.enable.account',$enableCode)
);
Mail::to($request->email)->send(new InvoiceEmail($data));
My question is: how can I attach the customerPDF?
Try to add attach method call to InvoiceEmail#build
public function build()
{
return $this->from('tes#test.com')
->subject('Order')
->markdown('emails.invoiceEmail')
->with('data',$this->data)
->attach(asset($this->data->pdf_file), ['mime' => 'application/pdf']);
}
I need to replace mail function with Laravel Mail function. I have created Mailable InvoiceEmail to send the mail
/* Attachment File */
$filename = "invoice".$request->id.$language.".pdf";
$path = "core/storage/app/pdf/";
$file = $path.$filename;
$data = [
'subject' => $subject,
'body' => $body
];
Mail::to($mail_to)->send(new InvoiceEmail($data));
InvoiceEmail Class with build function generate the email
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('mail.invoice')
->with('body',$this->data['body'])
->subject($this->data['subject']);
}
How do I attach the pdf file with this email?
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('mail.invoice')
->with('body',$this->data['body'])
->subject($this->data['subject']);
->attachFromStorage('your_location', 'your_pdf.pdf', [
'mime' => 'application/pdf',
]);
}
I am trying to send SMS OTP message using following code but the message is not being send. Below i have added my complete code. I am trying to send Message and email at same time message have send to register user but Otp SMS are not send. My TextLocal Account is transactional and i also have created SMS Template with that Template i am trying to send message .
My TextLocal Otp Template:
Use %%|OTP^{"inputtype" : "text", "maxlength" : "8"}%% as your login OTP.
OTP is confidential, Delish2go never call you for asking OTP. It valid for next 10 mins. Do not disclose OTP to anyone.
Notification page Code
Notifications/verifyEmailNotification.php
<?php
namespace App\Notifications;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\Textlocal\TextlocalChannel;
use NotificationChannels\Textlocal\TextlocalMessage;
class verifyEmailNotification extends Notification
{
use Queueable;
public $user;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail', TextlocalChannel::class];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('Please Verify Your account Email')
->action('Verify Account', route('verify', $this->user->token))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toTextlocal($notifiable)
{
return (new TextlocalMessage())
//Required
// To send sms via your Textlocal transactional account
//or promotional() to sent via Textlocal promotional account
->transactional()
//Required
//When sending through Textlocal transactional account, the content must conform to one of your approved templates.
->content("Use" . $this->user->code . "as your login OTP. OTP is confidential, Delish2go never call you for asking OTP. It valid for next 10 mins. Do not disclose OTP to anyone.");
}
public function toArray($notifiable)
{
return [
//
];
}
}
My RegisterController.php
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'token' => str_random(25),
'phone' => $data['phone'],
'code' => substr(str_shuffle("0123456789"), 0, 5),
]);
$user->sendVerificationEmail();
return $user;
}
My User.php Model
<?php
namespace App;
use App\Notifications\verifyEmailNotification;
use App\Notifications\SendBlackFridaySaleAnnouncement;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'token', 'id_name', 'email_id', 'phone', 'pass', 'code',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function verified()
{
return $this->token === null;
}
public function sendVerificationEmail()
{
$this->notify(new verifyEmailNotification($this));
}
}
Here is my Config/services.php
'textlocal' => [
'url' => 'https://api/textlocal.in/send' //or 'https://api.textlocal.in/send/ - for India
],
'transactional' => [
'apiKey' => env('TEXTLOCAL_TRANSACTIONAL_KEY'),
'from' => env('TEXTLOCAL_TRANSACTIONAL_FROM', 'TXTLCL')
],
My .env file
TEXTLOCAL_TRANSACTIONAL_KEY= My Api Key
TEXTLOCAL_TRANSACTIONAL_FROM=TXTLCL
My Composer.json
"require": {
"thinkstudeo/textlocal-notification-channel": "^1.0"
},
Please Help me, I have tried many things but my problem have not solved.
Have you tried using the Laravel SMS API plugin? It has built-in notification channel support as well. Here's a sample config for TextLocal which you can use with the plugin.
'textlocal' => [
'method' => 'POST',
'url' => 'https://api.textlocal.in/send/?',
'params' => [
'send_to_param_name' => 'numbers',
'msg_param_name' => 'message',
'others' => [
'apikey' => 'YOUR_API_KEY',
'sender' => 'YOUR_SENDER_NAME',
],
],
'json' => true,
'add_code' => false, //(If the numbers saved in DB do not have country code, change to true)
]
Hope it helps!
I think I miss something really simple here, but I have a script like this:
\Mail::to( User::all() )
->send( new NotificationEmail($notification) );
class NotificationEmail extends Mailable {
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #param Notification $notification
*
*/
public function __construct( Notification $notification ) {
$this->notification = $notification;
}
/**
* Build the message.
*
* #return $this
*/
public function build() {
$notification = $this->notification;
return $this
->from( [
'address' => \env( 'MAIL_DEFAULT_SENDER' ),
'name' => \env( 'APP_NAME' )
] )
->view( 'email.notification.ready' );
}
}
Now I'd like the email message to start with something like
Dear {firstname of the user}
But I have no idea how to get the firstname of user who is going to get that email. Is there any way to figure that out?
It is not a recommended way to send a email to all users, because who receive the email can see all the recipients and, they receive the same message that you cannot customize to add first name of the user.
You need to create separate Mailable to each user and queue all the Mailable. Sending email to all users separately is time-consuming task, workers are need to process the queue in background.
$users = User::all();
foreach ($users as $user) {
Mail::to($user)->queue(new NotificationEmail($notification, $user));
}
And now you can pass the $user instance, and first name of the user is available on the view:
public function __construct( Notification $notification , User $user) {
$this->notification = $notification;
$this->user = $user;
}
public function build() {
$notification = $this->notification;
return $this
->from( [
'address' => \env( 'MAIL_DEFAULT_SENDER' ),
'name' => \env( 'APP_NAME' )
] )
->view( 'email.notification.ready' , [
'user' => $this->user
]);
}