passing query data to send in mail laravel - 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

Related

How can I modify this code to write to Laravel controller

My problem is that when I send a post from an rfid card via esp8266, the data is an rfid number, but the data can't appear because the code I gave has not been written into Laravel.
This PHP code :
<?php
if(isset($_POST['uid'])) {
$uid = $_POST["uid"];
$sql = mysqli_query($dbconnect, "INSERT INTO tb_entry VALUES ('$uid')");
}
?>
How can I write this code into Laravel controller?
it should be like this
if ($request->has('uid')) {
DB::table('tb_entry')->insert([
'uuid' => $request->uuid
]);
}
create a controller: php artisan make:controller MyController
add a method to the controller like public function test(Request $request) {}
rewrite your code a little bit and pasted in:
public function test(Request $request) {
if (null !== $request->post('uid')) {
$uid = $request->post('uid');
// Note: you can use Laravel model instead plain mysqli query
// ** Beware of SQL injection vulnerabilities! **
$sql = mysqli_query($dbconnect, "INSERT INTO tb_entry VALUES ('$uid')");
}
}

How to use parameter from function to create an URL? Laravel Routing

I'm sending an URL hashed and when i get it i have to show a view on Laravel, so i have those functions on the controller and also some routes:
This are my routes:
Route::post('/sendLink', 'Payment\PaymentController#getPaymentLink');
Route::get('/payment?hash={link}', 'Payment\PaymentController#show');
And this are the functions i have on my controller:
public function getPaymentLink (Request $request){
$budgetId = $request['url.com/payment/payment?hash'];
$link = Crypt::decryptString($budgetId);
Log::debug($link);
//here to the show view i wanna send the link with the id hashed, thats why i dont call show($link)
$view = $this->show($budgetId);
}
public function show($link) {
$config = [
'base_uri' => config('payment.base_uri'), ];
$client = new Client($config);
$banking_entity = $client->get('url')->getBody()->getContents();
$array = json_decode($banking_entity, true);
return view('payment.payment-data')->with('banking_entity', $array);
}
And this is getting a "Page not found" message error.
What i want to to is that when i the client clicks on the link i send him that has this format "url.com/payment/payment?hash=fjadshkfjahsdkfhasdkjha", trigger the getPaymentLink function so i can get de decrypt from that hash and also show him the view .
there is no need to ?hash={link} in get route
it's query params and it will received with $request
like:
$request->hash
// or
$request->get('hash')
You need to define route like this:
Route::get('/payment/{hash}', 'Payment\PaymentController#show');
You can now simply use it in your Controller method like below:
<?php
public function getPaymentLink (Request $request,$hash){
$budgetId = $hash;
// further code goes here
}

Get recipient name on view in laravel mail

I have a set of recipients. I am able to send mail to all of them. But how to get their name on the view. To be specific how to get $user value in my view(emails.test).
Mail::send('emails.test', ['data' => $data], function ($message) use ($data) {
foreach($data['users'] as $user) {
$message->to($user->email, $name = $user->firstName . ' ' . $user->lastName);
}
$message->subject('test');
});
Is there any way to access $user value in my view? I can access $data in my view. $data['users'] is an array of users. I need particular/current User's name in the view.
My view(emails.test)
<div>Dear {{$user->firstName}},</div>
How are you?....
But user is undefined here.
Thanks in advance.
Debabrata
from the docs
The send method accepts three arguments. First, the name of a view
that contains the e-mail message. Secondly, an array of data you wish
to pass to the view. Lastly, a Closure callback which receives a
message instance, allowing you to customize the recipients, subject,
and other aspects of the mail message
as you can see the second arguments its the data you send to the view
so in your view you can use the $data array just like you did inside the closure:
#foreach($data['users'] as $user) {
{{$user->username}}
}

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']);
});

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());
});

Resources