html to pdf convert when send mail - codeigniter

How to convert a HTML file to PDF in CODEIGNITER when sending a mail?
$this->load->library('html2pdf');
$this->html2pdf->folder('./assets/pdfs/');
$this->html2pdf->filename('email_test.pdf');
$this->html2pdf->paper('a4', 'portrait');
$data = array(
'title' => 'PDF Created',
'message' => 'Hello World!'
);
//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));
//Check that the PDF was created before we send it
$path = $this->html2pdf->create('save');
I use this code for converting.

maybe can you try with TCPDF, that always worked for me :) TCPDF INTEGRATION CI

Related

Send attachment with Laravel SMTP and save to local storage

I read a similar topic in the forum solved, but it did not work successfully in me. Where am I making a mistake?
View
<input type="file" class="" name="document">
Send
Mail::send([], [], function ($message) use ($request) {
$message->to($request->to);
$message->subject($request->subject);
$message->setBody($request->message);
$data = $request->document;
$message->attach($data['document']->getRealPath(), array(
'as' => $data['document']->getClientOriginalName(),
'mime' => $data['document']->getMimeType()));
I want to send the attachment in this way, but it does not. Do I need to upload and then send it first?
There is a whole section about files in requests at official laravel's docs.
https://laravel.com/docs/7.x/requests#files
https://laravel.com/api/7.x/Illuminate/Http/UploadedFile.html
So, according to the above docs, the file you upload is (temporarly) saved when processing the request - you do not need to save it yourself.
$document_path = $request->document->path();
$message->attach($document_path, array(
'as' => $request->document->getClientOriginalName(),
'mime' => $request->document->getMimeType())
);

How to get Template Variables inside sendMessage() function in Magento 2?

I am trying to send transactional emails from Magento through Listrack. I have override the default sendMessage function using Magento plugin method. Now I have to fetch template variables and process the data inside this function. I tried a lot and was not able to fetch any details regarding the template. Can anyone please help me to resolve this?
sendMessage function is located in Transport.php file.
Replace the "Email Template Name" with your Template Name
$templateId = Mage::getModel('core/email_template')->loadByCode('Email Template Name')->getId();
$customerEmail = 'your mail Id';
$customerName = 'your name';
$senderName=Mage::getStoreConfig('trans_email/ident_general/name');
$senderEmail= Mage::getStoreConfig('trans_email/ident_general/email');
$sender = Array('name' => $senderName,'email' => $senderEmail);
$vars = array(
'customerName' => $customerName,
'date' => $date,
'url'=>$url,
'number'=>$number,
'barcode'=>$serial_barcode,
);
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $customerEmail, $customerName, $vars, null);
$translate->setTranslateInline(true);

Generate PDF from view to send email attaching the PDF file without saving it into disk Laravel 5

I am working on sending email function. Firstly, I want to generate .pdf file from my view. Then I want to attach the generated .pdf file by email without saving it into disk. I use below in my controller:
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf");
And I get this error: "Call to a member function attachData() on null"
If I use below without attachment, it works well:
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name));
Please advise.
I think you need to attach it to the message, not to the mailer.
$pdf = PDF::loadView('getpdf', $data);
$message = new Mysendmail($post_title, $full_name);
$message->attachData($pdf->output(), "newfilename.pdf");
Mail::to($to_email)->send($message);
Just use 'mime' => 'application/pdf', at last of your code. Simple!
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf"), [
'mime' => 'application/pdf',
]);
Just use 'mime' => 'application/pdf', at last of your code. Simple!
`
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf"), [
'mime' => 'application/pdf',
]);
`

How to make attachment in laravel email

i try to send email in laravel 5.2 and its works.
my problem is how to convert a view with its data to PDF then attach it to email
i try this
$datastd['fname']=$printqu->std_fname;
$datastd['mname']=$printqu->std_mname;
$datastd['lname']=$printqu->std_lname;
$datastd['email']=$printqu->std_email;
$datastd['email']=$printqu->std_email;
$datastd['orgname']=$printqu->name;
$datastd['depname']=$printqu->Dep_Name;
Mail::send('email.train_form',['datastd'=>$datastd], function($mail) use ($datastd){
$mail->to($datastd['email'],$datastd['fname'],$datastd['mname'],$datastd['lname'],$datastd['orgname'],$datastd['depname'])->attachData($datastd, 'printPreviewm.pdf')->from('everyone#gmail.com')->subject('Training Forms');
});
the error is time out
please i need your help in this
Edit: Sorry, your question is based on Laravel 5.2 and my answer is based on Laravel 5.4. As of generating a PDF can still be done with the DOMPDF package, and the docs for attaching it to a mail can be found in the official Laravel docs here
Generating a PDF based on a view template can be easily done by using a package such as DOMPDF made by Barryvdh
Generating a PDF would look something like this
$view = View::make('any.view', compact('variable'));
$contents = $view->render();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($contents);
$output = $pdf->output();
Storage::put('/folder/your-file.pdf', $output);
Attaching a document to a mail is pretty simple in Laravel (5.4) [docs]
// file location
$file = storage_path('app/folder/your-file.pdf');
// return mail with an attachment
return $this->view('emails.confirm')
->from('me#stackoverflow.com', 'From')->subject('New mail')
->with([
'name' => $this->data['name'],
])->attach($file, [
'as' => 'File name',
'mime' => 'application/pdf',
]);
i try the following
$view = View::make('printPreview', compact('printqu','printqu2'));
$contents = $view->render();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($contents);
$output = $pdf->output();
Storage::put('app/folderletter/your-file.pdf', $output);
$datastd['fname']=$printqu->std_fname;
$datastd['mname']=$printqu->std_mname;
$datastd['lname']=$printqu->std_lname;
$datastd['email']=$printqu->std_email;
$datastd['email']=$printqu->std_email;
$datastd['orgname']=$printqu->name;
$datastd['depname']=$printqu->Dep_Name;
$file = storage_path('app/folderletter/your-file.pdf');
Mail::send('email.train_form',['datastd'=>$datastd], function($mail) use ($datastd,$file){
//$pdf = PDF::loadView('printPreviewm',['datastd'=>$datastd]);
$mail->to($datastd['email'],$datastd['fname'],$datastd['mname'],$datastd['lname'],$datastd['orgname'],$datastd['depname'])
->from('everyone#gmail.com')->subject('Training Forms')
->attach('app/folderletter/your-file.pdf', [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);
});
i saves the document in Storeag
but also the same
time out!!!!
so the message not sent.
thanks all of you.. i solve it. it was something like this
$view = View::make('printPreview', compact('printqu','printqu2'));
$contents = $view->render();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($contents);
$output = $pdf->output();
Storage::put('app/folderletter/your-file.pdf', $output);
$datastd['fname']=$printqu->std_fname;
$datastd['mname']=$printqu->std_mname;
$datastd['lname']=$printqu->std_lname;
$datastd['email']=$printqu->std_email;
$datastd['email']=$printqu->std_email;
$datastd['orgname']=$printqu->name;
$datastd['depname']=$printqu->Dep_Name;
//$file= public_path(). "/app/".$sestest3->dep_path;
$file = storage_path(). "/app/app/folderletter/your-file.pdf";
Mail::send('email.train_form',['datastd'=>$datastd], function($mail) use ($datastd,$file){
//$pdf = PDF::loadView('printPreviewm',['datastd'=>$datastd]);
$mail->to($datastd['email'],$datastd['fname'],$datastd['mname'],$datastd['lname'],$datastd['orgname'],$datastd['depname'])
->from('everyone#gmail.com')->subject('Training Forms')
->attach($file, [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);
});

Codeigniter - two upload fields in one form

I'm trying to upload an image and an mp3 in Codeigniter, from a form with two file input fields. The image field is called userfile, the mp3 field is called trackfile.
In my model I have this code (modified):
$image_resource = $this->library_model->upload_image($_FILES);
$data = array(
'name' => $this->input->post('name'),
'imageresource' => $image_resource
);
$this->db->insert('table_name',$data);
$insert_id = $this->db->insert_id();
if ($_FILES && $_FILES['trackfile']['name'] !== "") {
$config = array(
'allowed_types' => 'mp3',
'upload_path' => './assets/samples/'
);
$this->load->library('upload',$config);
//echo 'about to upload';
if ($this->upload->do_upload('trackfile')) {
$file_data = $this->upload->data();
//echo 'uploaded: <pre>'; print_r($file_data); echo '</pre>'; die();
$track_data['trackfile'] = $file_data['file_name'];
$this->db->where('id', $insert_id);
return $this->db->update('table_name',$track_data);
} else {
echo 'Failed: ';
echo $this->upload->display_errors();
echo 'FILE DATA: <pre>'; print_r($_FILES['trackfile']); echo '</pre>';
}
}
My problem is that this produces the error "The filetype you are attempting to upload is not allowed." I've checked the mp3 type, it's audio/mpeg, which is in the mimes.php file.
This only happens when I'm also uploading an image. If I leave the image field blank, the mp3 uploads fine. Any help much appreciated.
EDIT
According to the CI docs, the allowed_types portion of the config is a pipe delimited list of mime types, not file extensions. For example, mp3 have the mime type 'audio/mpeg'. So your allowed_types config should look like this:
$config = array(
'allowed_types' => 'audo/mpeg|audio/x-wav|audio/x-aiff|application/ogg',
'upload_path' => './assets/samples/'
);
If that doesn't work try to initialize the library as mentioned here.

Resources