Anybody knows how attach automatically auto-generated pdf using codeigniter and phpmailer then,please help
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->cc('another#another-example.com');
$this->email->bcc('them#their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
You are clearly missing a couple lines of code...
i have also included pdf's in email after generating this is my code as example:
$pdf = $this->generatePdf();
foreach ($pdf as $p)
$this->email->attach('./barcodes/' . $p . '.pdf');
now for some explenation:
$pdf = $this->generatePdf();
this is used to actually generate and store the pdf's, the result will be an array with the names of the actual pdf.
foreach ($pdf as $p)
$this->email->attach('./barcodes/' . $p . '.pdf');
is used to loop through the array of generated pdf names and to actually include them, codeigniters email library has an attach function which allows you to add an attachment.
Related
Following is a code to send a PDF with email:
$pdf = PDF::loadHTML($str_html)->setPaper('a4', 'portrait');
Mail::send('emails.mail', $data, function($message) use ($data,$pdf){
$message->from('noreply#...');
$message->to('...');
$message->subject('test test');
//Attach PDF doc
$message->attachData($pdf->output(),'invoice.pdf');
});
But my requirement is to send multiple PDFs with email, how can I do this?
$attachments = [
// first attachment
'/path/to/file1',
// second attachment
'/path/to/file2',
...
];
$pdf = PDF::loadHTML($str_html)->setPaper('a4', 'portrait');
Mail::send('emails.mail', $data, function($message) use ($data,$pdf){
$message->from('noreply#...');
$message->to('...');
$message->subject('test test');
//Attach PDF doc
foreach($attachments as $filePath){
$message->attach($filePath);
}
});
taibur rahman provides an answer in comment, new PDFs variables must be added in "use", then code will be correct.
Complete Code:
$pdf = PDF::loadHTML($str_html)->setPaper('a4', 'portrait');
$pdf2 = PDF::loadHTML($str_html2)->setPaper('a4', 'portrait');
Mail::send('emails.mail', $data, function($message) use ($data, $pdf, $pdf2){
$message->from('...#...');
$message->to(...#...);
$message->subject('test test');
$message->attachData($pdf->output(),'test.pdf');
$message->attachData($pdf2->output(),'test2.pdf');
});
I want to download the data of table in excel file
I have an application in Laravel and I want to add a functionality that download user data in .xls format
Route::get('users/download', function(){
$table = App\User::all();
$filename = "User.csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array('ID', 'User Name'));
foreach($table as $row) {
fputcsv($handle, array($row['id'], $row['username']));
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv'
);
return Response::download($filename, 'User.csv', $headers);
});
This code works fine for .csv files what changes are required for .xls file?
Take a look at Laravel Excel: https://github.com/Maatwebsite/Laravel-Excel
Its very easy to use with Laravel.
I am new to php and building my first project.
I am creating a auto response email which will be sent to user after form submission.
The auto response email need to have content as follows,
Logo image
Thank you for contacting us.
Here you can view our case study.(need to link pdf file to view**)
I manage to get text. but not able to add image and hyperlink.
tried using variable to store image url but code is visible instead of an image.
i request if someone Please guide me.to solve this.
<?php
$to = $_POST['email'];
$from = "info#company.com";
$headers = "From: company";
$subject = "Thank you for contacting us.";
//$img='<img src="http://www.http://company.com/images/logo.jpg"/>';
$linkedin='Linkedin';
$twitter='Twitter';
$message=
"Dear ".$firstname." Thank you for contacting us.
Here you can view our case study.
www.company.com/data/casestudy.pdf
www.company.com | info#company.com | Linkedin | Twitter
";
$mailsent = mail($to, $subject, $message, $headers);
?>
First, In $header add content type to HTML
$headers .= "Content-type:text/html;charset=UTF-8";
http://php.net/manual/en/function.mail.php
This will set mail Content type to HTML, Now you can add all Tags and link of HTML without using variables, So full code will be like this
<?php
$to = $_POST['email'];
$from = "info#company.com";
$headers = "From: company";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$subject = "Thank you for contacting us.";
$message="<img src='http://company.com/images/logo.jpg'/>
<br/>Dear ".$firstname." Thank you for contacting us.
Here you can view our case study.
<a href='www.example.com'>File Name </a>
www.company.com | info#company.com |<a href='www.linkedin.com'>Linkedin</a> |<a href='twitter.com'> Twitter</a>
";
$mailsent = mail($to, $subject, $message, $headers); ?>
And don't get confused in single-quotes and double-quotes used here also i assume your $firstname has some value.
I'm using below code to send email from my codeigniter based website's contact form, to do that i'm using below codes in my controller,
$entrydata['name']= $this->input->post('name');
$entrydata['email']= $this->input->post('email');
$entrydata['phone']= $this->input->post('phone');
$entrydata['message']= $this->input->post('message');$msg = 'Email has sent successfully';
$data['reset'] = TRUE;
$this->load->library('email');
$this->email->from(set_value('email'), set_value('name'));
$this->email->to('my#webs.com');
$this->email->subject("Get a quote enquiry");
$all = 'Name:' . set_value('name') ."\n". 'Email :' .' '. set_value('email') ."\n".'Phone :' .' '. set_value('phone') ."\n".'Message :' .' '. set_value('message'); $this->email->message($all);
$s=$this->email->send();
$data['message'] = $msg;
Did anyone know how to add custom email template that'll hold my conact form informations?
This is an example which i have done
$data['map_to']=$this->input->post('map_to');
$event=$this->db->query("query");
if($event->num_rows()>0)
{
$data['event']=$event->row();
$data['map_from']=$event->row()->address2;
}
else
{
$data['event']=NULL;
}
$data['sender_mail'] = 'xx#xx.org';
$this->load->library('email');
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$this->email->initialize($config);
$this->email->from($data['sender_mail'], 'xxxx');
$this->email->to($mail);
$this->email->subject('Map Location');
$message=$this->load->view('map_mail_format',$data,TRUE);
$this->email->message($message);
$this->email->send();
Here i am loading a view page called map_mail_format and passing values to that view page ($data) then assign that view page to a variable then send mail with that message
first you need to initialize config as
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
after you load email library and then you can store your email view to a variable and mail it
$mailbody = $this->load->view('myfile', $data, true);
You may try this
$this->load->helper(array('email'));
$this->load->library(array('email'));
$this->email->set_mailtype("html");
function export_csv(){
$this->load->helper('csv');
if(isset($_POST['term_val'])&&$_POST['term_val']<>'0'){
$term = $this->Search_model->get_term($_POST['term_val']);
$term = json_decode($term);
}else{
$term=array();
}
$orders = $this->Order_model->get_orders_export($term,'place_order');
if(count($orders)>0){
foreach($orders as $order){
$sublist['order_id']= $order->order_number;
$sublist['ship_to']= $order->ship_firstname.' '.$order->ship_lastname;
$sublist['order_date']= date('d-m-Y H:i:s',strtotime($order->ordered_on));
$sublist['email_to']= $order->ship_email;
$sublist['city']= $order->ship_city;
$sublist['pincode']= $order->ship_zip;
$sublist['ship_address']= $order->ship_address1.' , '.$order->ship_address2;
$sublist['phone']= $order->ship_phone;
$sublist['product_name']= $order->name;
$sublist['product_id']= $order->product_id;
$sublist['status']= $order->status;
$sublist1[]= $sublist;
}
$delimiter = ";";
$newline = "\r\n";
$heading[]=array('order_id'=>'Order Id','ship_to'=>'Ship To','order_date'=>'Order Date','email_to'=>'Email To',
'city'=>'City','pincode'=>'Pincode','ship_address'=>'Ship Address','phone'=>'Phone','product_name'=>'Product Name','product_id'=>'Product ID','status'=>'status');
$get_csv=array_to_csv1($sublist1,$heading,$delimiter, $newline);
ob_start();
$filename = "orders_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
// header("Content-type: text/x-csv");
//header("Content-type: text/csv");
// header("Content-type: application/csv");
// header("Content-Disposition: attachment; filename=orders".date('d-M-Y').".csv");
print_r($get_csv);
}else{
redirect($this->config->item('admin_folder').'/orders');
}
}
The above code is the controller function for export CSV and the image is the action taking place when exporting.
Problem : Actually if we export csv using codeigniter it is showing that image as shown above. If we press export csv using codeigniter it it should not show that image , it should directly export csv into excel .
if you want to generate reports in csv format it is very easy with codeigniter. Your model function
function index(){
return $query = $this->db->get('my_table');
//Here you should note i am returning
//the query object instead of
//$query->result() or $query->result_array()
}
Now in controller
function get_report(){
$this->load->model('my_model');
$this->load->dbutil();
$this->load->helper('file');
// get the object
$report = $this->my_model->index();
//pass it to db utility function
$new_report = $this->dbutil->csv_from_result($report);
//Now use it to write file. write_file helper function will do it
write_file('csv_file.csv',$new_report);
//Done
}
No externals are required everything is available in Codeigntier.
The above method will force the file to be downloaded instead of
being opening. Cheers! If you want to write xml file it is easy too.
Just use xml_from_result() method of dbutil and use write_file('xml_file.xml,$new_report)
Visit these links they will help.
Database Utility Class
And
File Helper
***> if you are trying to generate reports in csv format then it will quite
> easy with codeigniter.***
Place this code in your Controller
function get_report()
{
$this->load->model('Main_Model');
$this->load->dbutil();
$this->load->helper('file');
/* get the object */
$report = $this->Main_Model->print_report();
$delimiter = ",";
$newline = "\r\n";
$new_report = $this->dbutil->csv_from_result($report, $delimiter, $newline);
write_file( 'application/third_party/file.csv', $new_report);
$this->load->view('report_success.php');
}
Put this code into Model
public function print_report()
{
return $query = $this->db->query("SELECT * FROM Table_name");
}
report_success.php is just Successful Notification.
Your Report is being Exported. Thank you
Finally Your "file.csv" is generated.
its basically stored at physical storage.
In CodeIgniter/application/third-party/file.csv
it works.
it will help you.