How to attach Mpdf output as email attachment with Codeigniter? - codeigniter

In my controller generating pdf layout and then attaching it as Pdf and with a custom filename. Here is the code
$this->load->library('mpdf');
$mpdf = new Mpdf('L', 'A4', 0, 'Trebuchet MS', 6, 6, 9, 9, 3, 3, 'L');
ob_start();
echo $this->generateFoodReport($inspectionId);
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML(utf8_encode($html));
$content = chunk_split(base64_encode($mpdf->Output('', 'S')));
$filename = "inspection-report.pdf";
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($fromMail, $fromName);
$this->email->to($mailTo);
$this->email->subject($subject);
$this->email->attach($content):
$this->email->send();
How to attach the file with email.

Send plain PDF buffer to the mail class.
$content = $mpdf->Output('', 'S');
$this->email->attach($content, 'attachment', $filename, 'application/pdf');
See Email Class Codeigniter documentation.

Try below code
$this->load->library('mpdf');
$mpdf = new Mpdf('L', 'A4', 0, 'Trebuchet MS', 6, 6, 9, 9, 3, 3, 'L');
ob_start();
echo $this->generateFoodReport($inspectionId);
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML(utf8_encode($html));
$content = chunk_split(base64_encode($mpdf->Output('', 'S')));
$filename = "inspection-report.pdf";
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from($fromMail, $fromName);
$this->email->to($mailTo);
$this->email->subject($subject);
$this->email->attach($content, 'attachment', $filename, 'application/pdf');
$this->email->send();
For more please check Codeigniter Email Class documentation

Related

Send email to create and attach PDF in CodeIgniter

The output email gets sent but does not arrive i my email. I have added dompdf library. When I removed the code that creates the pdf then the mail was sent.
My code:
<?php
$this->load->library('dompdf_gen');
$this->dompdf->load_html($body);
$this->dompdf->render();
$output = $this->dompdf->output(APPPATH . 'Brochure.pdf', 'F');
$email = $this->input->post('email');
$subject = "some text";
$message = $body;
$this->sendEmail($email, $subject, $message);
$config = Array(
'mailtype' => 'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('ul#ul.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach(APPPATH . 'Brochure.pdf');
if ($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
You must be save attachment as a file, so use file_put_contents to save pdf to a file. Set permission to 777 to accessible by public:
$this->load->library('dompdf_gen');
$this->dompdf->load_html($body);
$this->dompdf->render();
$output = $dompdf->output();
file_put_contents(APPPATH.'Brochure.pdf', $output);
chmod(APPPATH.'Brochure.pdf', 777);
$email=$this->input->post('email');
$subject="some text";
$message=$body;
$this->sendEmail($email,$subject,$message);
$config = Array(
'mailtype' => 'html' );
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('ul#ul.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach(APPPATH.'Brochure.pdf');
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
Use this library to convert html to pdf which I have modified for CI3.
https://github.com/shyamshingadiya/HTML2PDF-CI3
Please check below mentioned solution
//Load the library
$this->load->library('html2pdf');
//Set folder to save PDF to
$this->html2pdf->folder('./uploads/pdfs/');
//Set the filename to save/download as
$this->html2pdf->filename('new.pdf');
//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');
for Sending this particular file in email.
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email PDF Test');
$this->email->message('Testing the email a freshly created PDF');
$this->email->attach($path_to_pdf_file);
$this->email->send();
Let me know if it not work.

Email Attachment not working in codeigniter

Mail function with attachment file is not working in codeigniter, i m not receiving any mail with attachment.. before i received the mail without attachment but after i used "$_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;", i didnt receive any mail.. kindly help me.. Here my coding is
$image_name = "some_name";
$content = "some content";
$subject = "Subject Name";
$this->load->helper('email');
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from('abc#gmail.com');
$this->email->to('xxx#gmail.com');
$this->email->subject($subject);
$this->email->message($content);
$attched_file = $_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;
$this->email->attach($attched_file);
$this->email->send();
You should use config parameter for email library, and set 'mailtype' to 'html'(Default Value is 'text'). If it still fails, you should use the print_debugger() function to see why.
$config = Array(
'mailtype' => 'html'
);
$image_name = "some_name";
$content = "some content";
$subject = "Subject Name";
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('abc#gmail.com');
$this->email->to('xxx#gmail.com');
$this->email->subject($subject);
$this->email->message($content);
$attched_file = $_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;
$this->email->attach($attched_file);
if($this->email->send()){ echo 'Email send.';}
else {show_error($this->email->print_debugger()); }

how to send email using codeigniter

today i have sending email using code igniter but not work properly .so please any one help me.
this is my controller page code here;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {enter code here
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
// $this->load->library('pagination');
$this->load->library('session','form_validation');
}
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'siva#gmail.com', // here goes your mail
'smtp_pass' => 'mygamilpassword', // here goes your mail password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$message = 'hiiiiii';
$this->load->library('email', $config);
$this->email->set_newline("rn");
$this->email->from('siva#gmail.com'); // here goes your mail
$this->email->to('sam#gmail.com');// here goes your mail
$this->email->subject('Resume from JobsBuddy');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
} $this->sendMail;
}
and in my localhost open php.extension->php.openssl enable and also the port number change for 465, and smtp:smtp.gmail.com this are all enable it.
but not working help me.?
Try this code. This is the working code:
function sendMail()
{
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.googlemail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "siva#gmail.com";
$config['smtp_pass'] = "mygamilpassword";
$message = "Your email body text goes here";
$config['mailtype'] = "html";
$ci = & get_instance();
$ci->load->library('email', $config);
$ci->email->set_newline("\r\n");
$ci->email->from("siva#gmail.com");
$ci->email->to("siva#gmail.com");
$ci->email->subject("Resume from JobsBuddy");
$ci->email->message($message);
$ci->email->send();
echo $this->email->print_debugger();
}
Try this code. This is the working code and allow-less-secure-apps-access-gmail-account setting open
public function send_mail() {
ini_set('SMTP', "server.com");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "yourmail#gmail.com");
$this->load->library('email');
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'yourmail#gmail.com';
$config['smtp_pass'] = 'yourpassword';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$from_email = "frommailid#gmail.com";
$to_email = "tomailid#gmail.com";
//Load email library
$this->load->library('email');
$this->email->from($from_email, 'Identification');
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library');
//Send mail
if($this->email->send())
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
else
show_error($this->email->print_debugger());
$this->session->set_flashdata("email_sent","You have encountered an error");
$this->load->view('contact_email_form');
}

undefined variable during send mail with attachment in laravel

Undefined variable: pdf_show whenever I try to send mail with $pdf_show attachment.
$pdf = View::make('site.bill', compact('invoice_bill'))->render();
$pdf_show = PDF::load($pdf, 'A4', 'portrait')->output();
$msg = Config::get('ashram.delivered');
Mail::send('blank', array('msg' => Config::get('ashram.delivered'),'id'=>$id,'is_approve'=>$is_approve), function($message){
$message->to(Request::segment(3), Input::get('name'))->cc('subhankarbhattacharjee56#yahoo.in')->subject('Delivery')//;
->attach($pdf_show , "Tax Invoice");
});
Try adding use($pdf_show) to your mail function
$pdf = View::make('site.bill', compact('invoice_bill'))->render();
$pdf_show = PDF::load($pdf, 'A4', 'portrait')->output();
$msg = Config::get('ashram.delivered');
Mail::send('blank', array('msg' => Config::get('ashram.delivered'),'id'=>$id,'is_approve'=>$is_approve), function($message) use ($pdf_show) {
$message->to(Request::segment(3), Input::get('name'))->cc('subhankarbhattacharjee56#yahoo.in')->subject('Delivery')
->attachData($pdf_show , "Tax Invoice");
});

How to send an email with content from a View in codeigniter

I want to send an email to user from my application with the content of the email loaded from a view . This is the code i've tried out till now:
$toemail = "user#email.id";
$subject = "Mail Subject is here";
$mesg = $this->load->view('template/email');
$this->load->library('email');
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();
You need to call $this->load->library('email'); within the controller as well for the email in CI to work.
Also , in your code : $fromemail is not initialized.
You need to have SMTP support on your server.
$config should be declared as an array before assigning values and keys.
Working Code:
$this->load->library('email');
$fromemail="ad#c.com";
$toemail = "user#email.id";
$subject = "Mail Subject is here";
$data=array();
// $mesg = $this->load->view('template/email',$data,true);
// or
$mesg = $this->load->view('template/email','',true);
$config=array(
'charset'=>'utf-8',
'wordwrap'=> TRUE,
'mailtype' => 'html'
);
$this->email->initialize($config);
$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();
Edit:
$mesg = $this->load->view('template/email',true); should be having the true as pointed out by lycanian. By setting it as true , it doesn't send data to the output stream but it will return as a string.
Edit:
$this->load->view(); need a second parameter with data or empty like $mesg = $this->load->view(view,data,true);, if not it wont work
This line $mesg = $this->load->view('template/email',true); should be like this
$mesg = $this->load->view('template/email','',true);
with the single quotes before the value true, and it will work perfectly
Email template send In codeigniter we need to put and meta tag before sending email
$this->data['data'] = $data;
$message = $this->load->view('emailer/create-account', $this->data, TRUE);
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from($email, $name);
$this->email->to('emailaddres#mail.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
U will try it!! it's working for mine after many errors facing
$subject = 'New message.';
$config = Array(
'protocol' => 'sendmail',
'smtp_host' => 'Your smtp host',
'smtp_port' => 465,
'smtp_user' => 'webmail',
'smtp_pass' => 'webmail pass',
'smtp_timeout' => '4',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from('from mail address', 'Company name ');
$data = array(
'message'=> $this->input->post('message')
);
$this->email->to($toEmail);
$this->email->subject($subject);
$body = $this->load->view('email/sendmail.php',$data,TRUE);
$this->email->message($body);
$this->email->send();

Resources