Queuing mail with attachData in Laravel 5.1 / OctoberCMS - laravel

The following IS working when I use Mail::send
$email = 'my#email.com';
$name = 'My Name';
$invoice = InvoicePdf::generate($invoice_id); // generates PDF as raw data
Mail::send('mail.template', null, function($message) use ($name, $email, $invoice) {
$message->to($email, $name);
$message->subject('Thank you for your order!');
$message->attachData($invoicePdf, 'invoice.pdf', ['mime' => 'application/pdf']);
});
It works fine and an email is generated with the correct PDF attachment.
However, if I change Mail::send to Mail::queue then I receive the following error:
Unable to JSON encode payload. Error code: 5
/var/www/html/october/vendor/laravel/framework/src/Illuminate/Queue/Queue.php
line 90
If I take the $message->attachData(); line out then it works even with Mail::queue so it seems like the raw data from the attachment is causing issues with the queue but there's nothing in the relevant October or Laravel docs about how to deal with this.

May be its because $invoicePdf data is raw data of PDF file and php can not process that data (attachData) when saving to database.
hmm, alternative you can generate file and then just attach file path to mail and then add to queue.
// generate tempfile name
$temp_file = tempnam(sys_get_temp_dir(), 'inv');
// this pdf is generated by renatio plugin but you can
// use your data and save it to disk
PDF::loadTemplate('renatio::invoice')
->save($temp_file);
Mail::queue('mail.template', null, function($message) use ($name, $email, $temp_file) {
$message->to($email, $name);
$message->subject('Thank you for your order!');
$message->attach($temp_file, ['as' => 'Your_Invoice.pdf', 'mime' => 'application/pdf']);
});
it should work.
#Joseph pointed that in Laravel 5.5 there is mailable which can be used. #Joseph pointed this solution and it seems working so you can also use this solution if your laravel version is >= 5.5
https://laracasts.com/discuss/channels/laravel/sending-email-with-a-pdf-attachment
Thanks #Joseph

Related

Importing generated csv from Laravel into Google Sheets

I'm trying to import an automatically generated csv into Google Sheets using this as cell value:
=importData("https://example.com/reports/some-report")
When I try importData on a static file on the server everything works as expected (like =importData("https://example.com/storage/some-static-report.csv") )
..but when I generate the file on the fly () I get a #N/A in the cell; "Resource at url not found." error.
This is the Laravel code to generate the csv:
public function downloadReportsAsCSV(Request $request) {
$list = [];
$list[] = ['timestamp', 'id', 'value'];
// ... fill the list with data here
$callback = function() use ($list) {
$out = fopen('php://output', 'w+');
// Write CSV lines
foreach ($list as $line) {
fputcsv($out, $line);
}
fclose($out);
};
$name = 'somereport.csv';
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename='. $name,
];
return response()->stream($callback, 200, $headers);
}
The route to this method is public so authentication is not a problem in this case.
When I call https://example.com/reports/some-report in a browser it downloads the file (as somereport.csv), but somehow Google Sheeds can't handle it the way I expect it to.
Any ideas on how to make this work?
It seems to be working after all, it's just that Google Sheets apparently needed quit some time before updating the field (at least a couple of minutes).
If anyone has any idea how to trigger Google Sheets to update the data immediately I'd sure like to know.

Laravel 5.1 Mail::send doesn't work - gives 500 internal server error

I took an example from the Laravel 5.1 documentation for Mail and replaced it with my send and receiver email ids.
Mail::raw works in the controller and if I use Mail::send in tinker it works. However, if I use Mail::send in the controller it doesn't work.
Everything is set up as described on the Laravel 5.1 mail page. https://laravel.com/docs/5.1/mail#sending-mail . I have also cleared app cache, config cache and view cache.
public function sendEmailReminder(Request $request, $id)
{
$user = User::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
}
The error could be from the email you are trying to send from, in order to send the email successfully this email hello#app.com has to be a valid email address and the one register as your MAIL_USERNAME
It was a permissions issue for the storage/frameworks directory. Once I changed the permissions it worked fine..

AJAX Request getContent Laravel 5.1

$content = $request->getContent();
return \Response::json([
'success' => $content
]);
I don't know how to format it to json in laravel 5.1. I want to use the data but if I use $request->input('name') it is null, it's actually in the controller. If you have another way to get the data it would be much appreciated.
you would have to parse the response in the client-side:
JSON.parse(ajaxResponse)
right now you're only getting a string back from the server

PDF as mail attachment - Laravel

I want to create a PDF using the barryvdh/laravel-dompdf package and send this with an email as attachment.
The code I have now is:
$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
Mail::queue('emails.factuur', array('factuur' => $factuur), function($message) use ($pdf)
{
$message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
$message->attach($pdf->output());
});
But now I get the following error:
Serialization of 'Closure' is not allowed
You can only send serializable entities to the queue. This includes Eloquent models etc. But not the PDF view instance. So you will probably need to do the following:
Mail::queue('emails.factuur', array('factuur' => $factuur), function($message)
{
$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
$message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
$message->attach($pdf->output());
});

Laravel 4 Mail function not working properly

I'm currently working on a web application which requires users to verify before they are able to use their account.
I'm using Cartalyst's Sentry to register the users, and sending the email using the built in Mail function, but whenever I register I get the following error:
Argument 1 passed to Illuminate\Mail\Mailer::__construct() must be an instance of
Illuminate\View\Environment, instance of Illuminate\View\Factory given,
called in
/var/www/vendor/laravel/framework/src/Illuminate/Mail/MailServiceProvider.php
on line 34 and defined
I can't figure out what causes this.
At the top of my code I included "use Mail" otherwise I would get another error:
Class '\Services\Account\Mail' not found
Code
// Create the user
$user = $this->sentry->register(array(
'email' => e($input['email']),
'password' => e($input['password'])
));
$activationCode = $user->getActivationCode();
$data = array(
'activation_code' => $activationCode,
'email' => e($input['email']),
'company_name' => e($input['partnerable_name'])
);
// Email the activation code to the user
Mail::send('emails.auth.activate', $data, function($message) use ($input)
{
$message->to(e($input['email']), e($input['partnerable_name']))
->subject('Activate your account');
});
Anybody got an idea what the solution for this error is?
Thanks in advance,
Kibo
Remove /bootstrap/compiled.php I think it will work for you.
You need to remove this from your Mail::send call. The function should be the third parameter so I'm not sure what you're trying to do here -- the $input['email'] field will already be available within the function due to your "use ($input)"
$email = e($input['email']

Resources