I have a controller which mainly has two functions. First function adds the user to the database and sends an email (receipt.blade.php) to the user.The second function allows the user to get all the receipts for a particular email that a user enters.
Right now, if I directly go to the page where the user can enter the email and get all receipts, its working fine. But, if I try to do it through the process of adding a new user I get the error described in the title after I click submit to adding the user. However, it has added the user to the database, but shows the error because its sending the email which is the receipt.blade.php and has the undefined variable. My controller has these:
use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
The first Function is:
public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
'first_name'=> $data->first_name,
'last_name' => $data->last_name,
'email' => $data->email,
'video_count' => $video_count,
'amount_donated' => $data->amount_donated,
});
$mailer
->to($data->input('email'))
->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
($data->input('amount_donated'))));
return redirect()->action('PagesController#thank1');
}
My mailing file(mailable) is:
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $first_name, $last_name, $amount_donated;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($first_name,$last_name,$amount_donated)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->amount_donated = $amount_donated;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.receipt');
}
}
The second function is:
public function getUserReceipts(Request $data){
$email = $data->email;
$donor = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donor'));
}
The receipt file simply contains:
#foreach($donor as $value)
{{ $value->first_name }}
{{ $value->last_name }}
{{ $value->amount_donated }}
#endforeach
The error I'm getting seems to be because donor in the receipt file is
undefined and I'm not sure how to fix it as it was passed with compact
in the second function.
Would really appreciate the help.
From your last question i can see the problem. The issue is that you're reusing the same view which takes two different set of data. When used with the method getUserReceipts, it generates receipts for multiple donors. But when you send the mail you're using the view as the mail content. In that case you need to use the properites set in the mailable class. The ideal case would be to create a new view for sending a single email receipt and handle it like so.
Change your mailable class to this. With this you can use all the properties of the donor in your view.
<?php
namespace App\Mail;
use App\Donor;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $donor;
public function __construct(Donor $donor)
{
$this->donor = $donor;
}
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.singlereceipt');
}
}
Change the way you send the email to
\Mail::to($data->input('email'))->send(new \App\Mail\MyMail($donor));
Create a new view singlereceipt.blade.php and use the following code instead.
{{ $donor->first_name }}
{{ $donor->last_name }}
{{ $donor->amount_donated }}
You have an error in sending data to view. If you send data using with() function than syntax should be:
return view('emails.receipt')->with('donor', $donor);
See Passing Data To Views
Related
I am trying to pass the value from the request, which is stored as an array, to an email in html format before sending.
$data = array('email' => $request->get('email'), 'name' => $request->get('name'));
Mail::send('emails.email', ['data' => $data], function ($message) use ($data) {
$message->subject('Hello world!');
$message->to($data['email'], $data['name']);
});
This is my email html format file in email.blade.php
<h2>HELLO YOU HAVE A NEW EVENT!</h2>
<h3>TO {{$name}}</h3>
<h4>See more details .... Events</h4>
But it appears that the html file does not receive the variable ($name) that was sent
How to pass data (in array format) to email html?
I tried sending without the $ name variable. It appears that there is no problem. Everything goes smoothly But I really need to use variables Please help me
I can use $name if use this code
$data['name'] = "Guest";
Mail::send('emails.email', $data, function ($message) {
$message->to('email#gmail.com', 'name')
->subject('topic');
});
Why?
Mail::send('emails.email', ['data' => $data], function ($message) use ($data) {
$message->subject('Hello world!');
$message->to($data['email'], $data['name']);
});
You're passing a data variable, not name (['data' => $data]). So get the name from that array:
<h2>HELLO YOU HAVE A NEW EVENT!</h2>
<h3>TO {{ $data['name'] }}</h3>
Or pass the $data variable directly so that you will have access to all its values as separate variables:
Mail::send('emails.email', $data, function ($message) use ($data) {
$message->subject('Hello world!');
$message->to($data['email'], $data['name']);
});
first you have to declare the variable in your mail class like this:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MailForm extends Mailable
{
use Queueable, SerializesModels;
public $fullname;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($fullname)
{
//
$this->fullname = $fullname;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('sendmail');
}
}
then in your view you can now call the variable
{{ $fullname }}
dont forget to call the mail class and mail facades in your controller like:
use App\Mail\MailForm;
use Illuminate\Support\Facades\Mail;
I'm trying to send an email using laravel however I keep getting the cannot access empty property error whenever I run my code.
I've done my research on the error and it seems to be usually caused by using a $ before the property name, example $this->$username instead of $this->username. However, that isn't the case in my code.
I can't really tell what's causing it nor do I have great experience in Laravel
Here's my mailable class:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class VerificationMail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$data2 = ['companyName' => $this->data['name'], 'verificationCode' => $this->data['verificationCode']];
return $this->from('noreply#REMOVED.com')
->$this->view('emails.verification', $data2);
}
}
My view is saved in resources/views/emails/verification.blade.php
I saw also that this error can sometimes be caused by using $message as variable name inside the views, however that isn't the case with me. I tried loading the view with a normal route without any mail sending involved and it loaded normally.
Can anyone spot it? Thanks.
You have error here:
return $this->from('noreply#REMOVED.com')
->$this->view('emails.verification', $data2);
Use following instead: (remove second $this->)
return $this->from('noreply#REMOVED.com')
->view('emails.verification', $data2);
This is my first time using events in Laravel/Lumen.
I am actually using Lumen and I am trying to dispatch an instance of Mailable when a new user signs up in order to send an email in the background.
I believe I have set it up right, but I keep getting this error...
Type error: Argument 1 passed to Illuminate\Mail\Mailable::queue() must implement interface Illuminate\Contracts\Queue\Factory, instance of Illuminate\Queue\DatabaseQueue given
I can't actually see within the error message itself where the issue is coming from e.g. there is no line numbers.
However, this is my code...
AuthenticationContoller.php
$this->dispatch(new NewUser($user));
NewUser.php
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Mail\Mailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewUser extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('test')->to('test#test.com', 'Test')
->from('test#test.com', 'test')->replyTo('test#test.com', 'test')
->subject('Welcome to the blog!');
}
}
I ran into the same issue. It seems like Lumen and Illuminate/Mailer don't work together all that well.
However I found quite an easy fix in a Github thread.
Basically you just have to create a new service provider in your app/Providers directory.
MailServiceprovider.php
<?php
namespace App\Providers;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\MailServiceProvider as BaseProvider;
class MailServiceProvider extends BaseProvider
{
/**
* Register the Illuminate mailer instance.
*
* #return void
*/
protected function registerIlluminateMailer()
{
$this->app->singleton('mailer', function ($app) {
$config = $app->make('config')->get('mail');
// Once we have create the mailer instance, we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new Mailer(
$app['view'], $app['swift.mailer'], $app['events']
);
// The trick
$mailer->setQueue($app['queue']);
// Next we will set all of the global addresses on this mailer, which allows
// for easy unification of all "from" addresses as well as easy debugging
// of sent messages since they get be sent into a single email address.
foreach (['from', 'reply_to', 'to'] as $type) {
$this->setGlobalAddress($mailer, $config, $type);
}
return $mailer;
});
$this->app->configure('mail');
$this->app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);
}
}
And then you just have to register this service provider in your bootstrap/app.php instead of the default one by adding the following line:
$app->register(\App\Providers\MailServiceProvider::class);
I have created an app using Laravel and uploaded it to the live server (shared hosting). I have programmed it such that emails are sent to clients together with email attachments generated via DOMPDF. The problem is that I receive a phishing error in Gmail inbox concerning the attachment. When I remove the image from the attachment the error disappears. Seems like am not inserting the image properly in the blade file. Please assist me on how to add the image via the controller then parse it to the view (that is sent as an attachment)?
~ Regards
PDF Controller that loads the PDF file
class PDFController extends Controller
{
//Loads the PDF document
public function getPDF(){
$pdf = \PDF::loadView('pdf.customer', ['format' => 'A5-L']);
return $pdf->stream('customer.pdf')->header('Content-Type','application/pdf');
}
}
Email blade
<div class="logo pull-right">
<?php $image_path = '/img/logo.png'; ?>
</div>
PagesController that sends the email
$pdf = PDF::loadView('pdf.customer', $data);
Mail::send('emails.feedback', $data, function($message) use ($data, $pdf){
$message->from('info#*************');
$message->to($data['email']);
$message->subject('Feedback');
//Attach output from PDF doc, customer.pdf is the name of the file attached
$message->attachData($pdf->output(),'customer.pdf');
});
If you are using Laravel inbuilt mailer then, Follow below structure
Crete mail class using
php artisan make:mail MailFileName
MailFileName.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MailFileName extends Mailable
{
use Queueable, SerializesModels;
public $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->from('from_email' ,'Title')->view('emails.email_ blade_file')->with(['data' => $this->data])->subject($subject)->attach($this->data['attachment_url']);
}
}
In view you are passing $data variable so you can use all this variable just like normal blade file.
View File - resource/views/emails/email_ blade_file.blade.php
In your controller where you want to call mail function add,
SomeController.php
use use App\Mail\MailFileName;
Inside function,
$data['subject'] = 'New message from';
$data['data'] = ['your data'];
Mail::to($to_email)->send(new MailFileName($data));
I'm able to get the the data for particular email after entering it. However this can only take place after getting into that page. But now, before I do that I need to enter the user to the database and send him an email. After entering the info and submitting it, I get an error that tells "undefined variable:donor". Nevertheless, the user still gets added into the database but I can't go to the next page and get an error because it also has a function together that sends an email(receipt.blade.php) which is giving the error because "donor" is undefined.
My controller has these:
use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
The function is as
public function getUserReceipts(Request $data){
$email = $data->email;
$donor = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donor'));
}
The receipt.blade.php page has the function suggested in one of the answers by sandeesh
#foreach($donor as $value)
{{ $value->first_name }}
{{ $value->last_name }}
{{ $value->amount_donated }}
#endforeach
the function that sends the email is:
public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
'first_name'=> $data->first_name,
'last_name' => $data->last_name,
'email' => $data->email,
'video_count' => $video_count,
'amount_donated' => $data->amount_donated,
});
$mailer
->to($data->input('email'))
->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
($data->input('amount_donated'))));
return redirect()->action('PagesController#thank1');
}
My mail.php is as follows:
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $first_name, $last_name, $amount_donated;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($first_name,$last_name,$amount_donated)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->amount_donated = $amount_donated;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.receipt');
}
}
Add at the top
use DB;
Or
$donor = Illuminate\Support\Facades\DB::table..
Either use the alias and import it or the full path.This might also happen because your aliases in config/app.php are out of whack.
Replace use Illuminate\Support\Facades\DB; with use DB;
Here's your main problem. You return the view within the loop. This causes the view to be rendered for the first donor and the rest gets ignored. You need to pass the donors to the view and then loop in the view to display them. Do this.
Controller
public function getUserReceipts(Request $data)
{
$email = $data->email;
$donors = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donors'));
}
In your view
#foreach($donors as $donor)
{{ $donor->first_name }}
{{ $donor->last_name }}
{{ $donor->amount_donated }}
#endforeach