Below a function in my custom module, it creates a receipt and then sends an e-mail to the user.
I use views for the receipt and the e-mail.
If views are “no the way to do it”, how to do it otherwise?
function send_receipt(&$transaction)
{
// EE settings to allow PDF output
$this->EE =& get_instance();
$this->EE->load->helper('url');
$this->EE->load->_ci_view_path = PATH_THIRD.'crm/views/';
require_once(PATH_THIRD."module/includes/dompdf/dompdf_config.inc.php");
$transaction->invoice_number = $this->new_invoice_number($transaction->transaction_id);
$view['transaction'] =& $transaction;
$html = $this->EE->load->view('invoice',$view,TRUE);
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
$file = str_replace('ee/',"receipts/receipt_".$transaction->invoice_number.".pdf",BASEPATH);
if(file_exists($file))
unlink($file);
file_put_contents($file, $pdf);
$email_text = $this->EE->load->view('messages/receipt',$view,TRUE);
$this->EE->load->library('email');
$this->EE->email->wordwrap = true;
$this->EE->email->from('[email protected]', 'Company name');
$this->EE->email->to($transaction->email);
$this->EE->email->bcc('[email protected]');
$this->EE->email->subject('Receipt');
$this->EE->email->message($email_text);
$this->email->attach($file);
$this->EE->email->Send();
$this->EE->load->_ci_view_path = APPPATH.'views/';
return $file;
}Thanks for your fast replies!
Ok, here’s a simple example
// the second and third parameter aren't necessary, but I've used it here
// so that the syntax is consistent with the rest of the module code (->TMPL)
$this->EE->load->library('Template', NULL, 'TMPL');
$this->EE->TMPL->fetch_and_parse('invoice', 'email');
$email_text = $this->EE->TMPL->parse_globals($this->EE->TMPL->final_template);
// at this point, the 'email' template from the 'invoice' template group has been parsed, along with
// any ExpressionEngine tags and global variables on the template
// parse the variables for this transaction - using parse_variables_row() in this example
// since that appears to match the intent of your data
$email_text = $this->EE->TMPL->parse_variables_row($email_text, $transaction);
// and now it's ready to emailThanks, definitely going to use that.
Only argument against using this is that users can possibly destroy the templates, but that’s always a risk.
In the end I still think using views from a module can be very useful, any chance this is going to be supported in the future (maybe already in the 2.1 release, haven’t tested that)?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.