Why giving undefined variable in sending Email attachment in Laravel? - laravel

I can't pass any variable to Email body from Controller. I have searched many solutions but no is matching with me.
I am using Mailable. But it is empty. I am not sure it is creating problem or not.
public function __construct(){}
public function build(){}
I have tested with dummy Email without passing variable. Email is sending successfully. So, I think there is no problem with configuration.
Function in controller:
public function mail()
{
$info = Invoice::find(65)->first();
// 65 is giving me value. I have checked with dd()
$data = [
'invoice' => $info->invoiceNumber
];
Mail::send(['text'=>'invoice.test'], $data, function($message) use($data){
$pdf = PDF::loadView('invoice.test');
$message->to('example#gmail.com','John Smith')->subject('Send Mail from Laravel');
$message->from('from#gmail.com','The Sender');
$message->attachData($pdf->output(), 'Invoice.pdf');
});
return 'Email was sent';
}
test.blade.php
<h1>It is working!! {{ $invoice }}</h1>
Data sending style is followed by:
Laravel Mail::send how to pass data to mail View

Try adding the $data with the PDF view:
$pdf = PDF::loadView('invoice.test', $data);
But if you want to add an email body content different with the PDF content, try this:
Mail::send([], $data, function($message) use($data){
$pdf = PDF::loadView('invoice.test', $data);
$message->to('example#gmail.com','John Smith')->subject('Send Mail from Laravel');
$message->from('from#gmail.com','The Sender');
$message->setBody('sample mail content');
$message->attachData($pdf->output(), 'Invoice.pdf');
});

Related

Cannot send mail including HTML tags in it using Laravel

I want to send an email with HTML tags inside it with Laravel for which I am using Mail::raw() method.
I don't want to send a view, just want to send data as text with few HTML tags in it.
I used to below code
\Mail::raw("<h1>Hello</h1>", function ($message) use ($receiversEmail, $subject, $body) {
$message->from("hello#user.com", 'A user');
$message->to($receiversEmail);
$message->subject($subject);
});
But the HTML tags are not getting converted and hence I got a mail that reads like
<h1>Hello</h1>
How can i achieve this?
What about this
\Mail::send([], [], function ($message) use ($receiversEmail, $subject, $body) {
$message->from("hello#user.com", 'A user');
$message->to($receiversEmail);
$message->subject($subject);
$message->setBody("<h1>Hello</h1>", 'text/html');
});
Use Mail::send instead.
$html = "<h1>Hello</h1>";
\Mail::send([], [], function ($message) use ($receiversEmail, $subject, $html) {
$message->from("hello#user.com", 'A user');
$message->to($receiversEmail);
$message->subject($subject);
$message->setBody($html, 'text/html');
});

passing query data to send in mail laravel

i have a query to return all the the data in visits table
$visits = Visit::get();
i want to pass the returned data in $data so that i can send it on mail and display the data in the email body
$data = array();
Mail::send('mails.mail', $data, function ($message) use ($host_email, $to_name) {
$message->from('', '');
$message->to($host_email);
$message->subject('Visitor arrived');
});
how do i do that
You can use Laravel structure, You can create a mail class with this command:
php artisan make:mail OrderShipped
it will generate a class in App\Mail path sample named OrderShipped in build method you can call a blade view for this mail and send data to it like:
public function build()
{
return $this->view('emails.orders.shipped')
->with([
'orderName' => $this->order->name,
'orderPrice' => $this->order->price,
]);
}
and finally use this to send email to user:
Mail::to($request->user())->send(new OrderShipped($order));
look here for full documentation:
laravel mail

How to add more parameter on controller laravel mail

I want to send email using Laravel and success with this code
Mail::to($email)->send(new SendMail($name))
Now I want to send more parameter like subject with this code, but didn't work.
No error message but email not being sent.
Mail::send(new SendMail($name function($message) use ($email){
$message->to($email)->subject('Verify your Account');
}));
Any suggestions? Thanks in advance!
You can use something like that
$template = 'view.email';
$paramets = ['data'=>$data];//Your to value to print in email
$email = 'to#email.com'// to email
$cc = 'cc#email.com' // or null
$toName = 'John Doe' // or null
$subject = 'Verify your Account';
Mail::send($template, $parameters, function($message) use($email,$cc,$toName,$subject) {
$message->from(env('sender'), env('nameSender'));
if($cc) $message->cc($cc);
$message->to($email, $toName)
->subject($subject);
});
I am using a generic method. So i can use this method to all emails that i need to send

Undefined variable inside Laravel view when using Mail

While trying to send verification email using Laravel 5.2, I get an error:
Undefined variable: confirmation_code (View:
C:\xampp\htdocs\laravel\resources\views\email\verify.blade.php)
My code looks like this:
Controller.php:
public function postSignup(Request $request){
$this->validate($request,[
'email'=>'required|unique:users|email',
'name'=>'required|max:50|min:3',
'password'=>'required|min:6',
'con-password'=>'required|same:password',
]);
$confirmation_code=['code'=>str_random(20)];
$name = $request->input('name');
Mail::send('email.verify',$confirmation_code,function($message)
use($request,$name){
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
User::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password'))
]);
return redirect()->back()->with('info','Congratulation you have been successfully registered.Please check your email for verification');
}
Mail.verify.blade.php:
<h2>Verify Your Email Address</h2>
<div>
Thanks for creating an account with the verification demo app.
Please follow the link below to verify your email address
{{ URL::to('register/verify/'.$confirmation_code) }}.<br/>
</div>
</body>
Try this:
Mail::send('email.verify', compact('confirmation_code'), function ($message) use($request, $name) {
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
The reason why it fails is that Laravel views accept an associative array as their data, so that it can turn them into variables using keys as variables names and match them to their corresponding values.
What compact does is turn your variable into an associative array, with the name of the variable as its key (sort of the opposite of what the Laravel view will do).

When i am trying to send mail from contactUS form getting this error using swiftmailer in Laravel 5.2

when i am trying to send Mail through Contact Us Form receiving this Error
"Address in mailbox given [] does not comply with RFC 2822, 3.6.2."
I try search to find solution but I cannot find one. I edited config/mail.php
public function sendContactInfo(ContactMeRequest $request)
{
$data = $request->only('name', 'email');
$emailto="******#gmail.com";
$data['messageLines'] = explode("\n", $request->get('message'));
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});
return back()
->withSuccess("Thank you for your message. It has been sent.");
}
with configuration file
i am following this tutorial
Laravel Send Mail
use $data['email']
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto,$data['email']) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});

Resources