The code below is the method for sending the feedback message by Laravel. I want to create the variables which values are html fields names. However, the $email_fieldName is not accessibleinside the Mail::send .../
public function sendFeedback(Request $request) {
$email_fieldName = 'email';
// ...
if ($request ->isMethod('post')) {
// ...
$inputedData = $request->all();
// $email_fieldName is accessible here
$result = Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData) {
// $email_fieldName is not accessible here
$message->from($inputedData[$email_fieldName], $inputedData[$name_fieldName]);
// ...
});
}
}
I tried below simulation fiddle and it works. What wrong with the variable access in the code above?
$nameKey = "name";
$testBool = true;
if ($testBool) {
$array= array("name"=>"Alex", "age"=>22, "student" => true);
echo($array[$nameKey]);
}
The Mail function can't find the $email_fieldName as you haven't mention it inside use()
Change
$result = Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData) {
with.
$result = Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData, $email_fieldName) {
That's because you are in the anonymous callback of the Mail::send method. $email_fielName is not in that scope.
The same way you have use ($inputedData), you should should add $email_fieldName :
Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData, $email_fieldName) {
(And probably $name_fieldName too given your code)
Related
I've some problem with email attach generate pdf, hope you can give me some advice, please kindly help.
Here is the Controller:
public function kirim(Request $request){
$keluhan = keluhan::findOrFail($request->id);
$tindak = DB::table('tindakans')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('tindakans.id, id_keluhan, perbaikan_sementara, revisi_dokumen, target_verifikasi, ttd_tanggung1,
ttd_tanggung2'))->get();
$analisa = DB::table('analisas')
->join('tindakans','tindakans.id','=','analisas.id_tindakan')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('id_tindakan, analisa, tindakan, pic, tanggal_pelaksanaan'))->get();
$pdf = \PDF::loadView('laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
//return $pdf->stream();
$data = array(
'email_address'=>$request->email_address,
'cc'=>$request->cc,
'subject'=>$request->subject,
'keterangantambahan'=>$request->keterangantambahan
);
Mail::send('laporan.kirim', $data, function($message) use($data) {
$message->from('christian7andrew#gmail.com', 'PuraBox');
$message->to($data['email_address']);
if($data['cc'] != null){
$message->cc($data['cc']);
}
$message->subject($data['subject']);
$message->Attach($pdf);
});
return redirect('/');
}
How can I attach $pdf?
For future Googlers:
Since Laravel 5.1 you can use attachData() method to directly attach generated file without the need for saving it.
Source:
Manual - section Raw Data Attachments
The attachData method accepts the raw data bytes as its first argument, the name of the file as its second argument, and an array of options as its third argument:
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attachData($this->pdf, 'name.pdf', [
'mime' => 'application/pdf',
]);
}
You are using:
$message->Attach($pdf);
To add attachments to an e-mail, use the attach method on the $message object passed to your Closure. The attach method accepts the full path to the file as its first argument:
Source
You should use the attach method (without the capitalize letter), and in the parameter, you need to pass the path where the pdf is, not the generated pdf. like the docs says.
$message->attach($pathToFile);
In the end, will be something like that:
Mail::send('laporan.kirim', $data, function($message) use($data) {
$message->from('christian7andrew#gmail.com', 'PuraBox');
$message->to($data['email_address']);
if($data['cc'] != null){
$message->cc($data['cc']);
}
$message->subject($data['subject']);
//Full path with the pdf name
$message->attach('foo/bar/mypdfname.pdf');
});
I don't know which library you are using for PDF generation, but I guess there should be an API something like this,
$pdf = \PDF::loadView('laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
$path = storage_path('app/public/pdf/')."example.pdf";
$pdf->save($path);
return $path;
And then you can use the path to attach to email.
Hope this helps.
it's very simple just add your blade in the loadView() method and pass it's pdf instance into callback
public function index()
{
$data["email"] = "contact#cdlcell.com";
$data["title"] = "cdlcell";
$data["body"] = "Demo";
$pdf = PDF::loadView('emails.myTestMail', $data);
Mail::send('emails.myTestMail', $data, function($message)use($data, $pdf) {
$message->to($data["email"], $data["email"])
->subject($data["title"])
->attachData($pdf->output(), "text.pdf");
});
dd('Mail sent successfully');
}
May be you can do something as
Mail::send('laporan.kirim', $data, function($message) use($data) {
$message->from('christian7andrew#gmail.com', 'PuraBox');
$message->to($data['email_address']);
if($data['cc'] != null){
$message->cc($data['cc']);
}
$message->subject($data['subject']);
$message->Attach($pdf->output(),"name.pdf");
});
return redirect('/');
}
this may help you out
I think Im close, I just need to pass the $excelFile in Mail but it keeps saying its undefined, yet when I pass $truckstop_post it goes through but that data is not formatted correctly bc it didnt go through the Excel::create yet. How do I get the result of \Excel::create into the Mail::send??
public function truckstopPost()
{
$type = 'csv';
$truckstop_post = Loadlist::select('pick_city', 'pick_state', 'delivery_city', 'delivery_state', 'trailer_type', 'pick_date', 'load_type', 'length', 'width', 'height', 'weight', 'offer_money', 'special_instructions', 'company_contact', 'contact_phone')->where('urgency', 'OPEN')->orderBy('id', 'desc')->get();
$excelFile = \Excel::create('itransys', function($excel) use ($truckstop_post) {
$excel->sheet('mySheet', function($sheet) use ($truckstop_post)
{
$sheet->fromArray($truckstop_post);
});
$info = Load::find(8500);
$info = ['info'=>$info];
Mail::send(['html'=>'email.invoice_email_body'], $info, function($message) use ($info, $excelFile){
$message->to('mike#gmail.com')->subject('subject');
$message->from('mike#gmail.com', \Auth::user()->name);
$message->attachData($excelFile, 'Invoice.csv');
});
});
return back()->with('status', 'You Posted Truckstop!');
}
This is what the results look like if I pass $truckstop_post into attachData() but of course thats not a nicely formatted csv file
You can do it like so
use Maatwebsite\Excel\Excel as BaseExcel;
use Maatwebsite\Excel\Facades\Excel;
...
$filename = "my_file.csv";
$attachment = Excel::raw(
new PurchaseOrderLinesExport($this->data),
BaseExcel::CSV
);
$subject = "Purchase Order"
return $this->from($this->employee->email)
->subject("Purchase Order)
->view('emails.view')
->attachData($attachment, $filename);
Excel::raw() method creates/writes a temporary file and then gets its contents and delete after deleting that file.
Second Solution would be like this if you're using laravel:
public function build()
{
return $this->markdown('emails.report')
->attach(
Excel::download(
new AuditReport($this->audit),
'report.xlsx'
)->getFile(), ['as' => 'report.xlsx']
);
}
Excel::download() returns a BinaryFileResponse that's why it doesn't work directly, but you can grab the file.
public function post()
{
$type = 'csv';
$create_excel = List::select('pick_city', 'pick_state', 'delivery_city', 'delivery_state')->where('urgency', 'OPEN')->orderBy('id', 'desc')->get()->toArray();
$excelFile = \Excel::create('itrans', function($excel) use ($create_excel) {
$excel->sheet('mySheet', function($sheet) use ($create_excel)
{
$sheet->fromArray($create_excel);
});
})->download($type);
Mail::send(['html'=>'email.email_body'] function($message) {
$message->to('example#gmail.com')
->subject('Email Subject Line');
$message->from('daniel#twbb.com', 'Daniel');
$message->attachData($excelFile, 'Invoice.csv');
});
return $excelFile;
}
I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......
I have a problem here.. i try to send email to multiple recepients. The recepients are from my database which name of table is subscribes the message error is like this
ErrorException in SimpleMessage.php line 297:
Illegal offset type
public function store_job(Request $request)
{
$this->validate($request, ['posisi' => 'required','persyaratan' => 'required','tanggung_jawab' => 'required']);
$tambah = new jobs(); //kita buat objek yang terhubung ke table JOBS
$tambah->posisi = $request['posisi'];
$tambah->persyaratan = $request['persyaratan'];
$tambah->tanggung_jawab = $request['tanggung_jawab'];
$tambah->kategori = $request['kategori'];
$tambah->save();
$anu = DB::table('subscribes')->select('email');
$data = array ('email'=>$anu);
Mail::send('emails.news', $data, function ($message) use ($request, $data) {
$message->from('stevanajja#gmail.com',$request->email);
$message->to($data['email'])->subject($request->posisi);;
});
return redirect()->to('/panel_admin/opportune');
}
please help as fast as possibble.. because i am a student, this is my homework for examination.
Here what I'am doing is declaring a new array $emails to store all email from database. By iterating the retrieved object anu, I am pushing the email to $emails and passing it to the to property of the mail.
$anu = DB::table('subscribes')->select('email')->get();
$emails=[];
foreach($anu as $a){
$emails[]=$a->email;
}
Mail::send('emails.news', $emails, function ($message) use ($request, $emails) {
$message->from('stevanajja#gmail.com',$request->email);
$message->to($emails)->subject($request->posisi);;
});
Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.
This question should about match your effort.
$email_ids= DB::table('users')->pluck('email_id');
$name = DB::table('users')-> pluck('name');
foreach ($email_ids as $email_id ) {
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}
I want to send a mail to respective users with their names in email. I get $email_ids and $name in array. but when I run this code , I get error as
htmlentities() expects parameter 1 to be string, array given
When I replace 'user' => $name with 'user' => $email_id . I runs successfully.
When I send mail it should be like
Hello , $name(name of user)
Thank You
Your code should be:-
$users = DB::table('users')->get(); // get all users
foreach ($users as $user ) {
$name = $user->name; // get user's name
$email_id = $user->email_id; // get user's email
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}
Note:- You can get specific columns from DB table via below query,
$users = DB::table('users')->select('name', 'email_id')->get();
Laravel shows htmlentities() error when there is an error in view file.
In your case you are getting name and email in different-different arrays but you only foreach $email_ids not $name In this line
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
in this $name is array of names return by query and you are printing it as string that's why it is giving that error.
To achieve what you want change your code like this
$users = DB::table('users')->select('name')->addSelect('email_id')->get()->toArray();
foreach ($users _ids as $user ) {
Mail::send('mail', ['user' => $user->name ] , function ($message) use ($user->email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($user->email_id)->subject("Welcome");
});
}