How to send Send Email with attached CSV file in codeigniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('email');
$this->load->helper('file');
$this->load->database();
}
public function index()
{
$data['rs'] = $this->db->get('customer_profile');
$this->load->view('home', $data);
}
// Download for CSV file.
// http://localhost/codeigniterR/index.php/home/csv
public function csv($query, $filename = 'CSV_Report.csv')
{
$this->load->dbutil();
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$enclosure = '"';
$query = $this->db->query("select customeremail as Radhey from customers");
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
// client side download save as per client
force_download($filename, $data);
}
// Send mail with attached CSV file
// http://localhost/codeigniterR/index.php/home/sendcsvmail
public function sendcsvmail($filename = 'CSV_Report_Radhey.csv')
{
$this->load->dbutil();
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$enclosure = '"';
$query = $this->db->query("select customeremail as Radhey from customers");
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
// Write the file to server optional file_put_contents
//file_put_contents($filename, $data);
// Load the file helper and write the file to server
write_file($filename, $data);
$this->sendmail($filename);
unlink($filename);
}
function sendmail($filename)
{
$this->email->initialize(array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.sendgrid.net',
'smtp_user' => '**********',
'smtp_pass' => '***************',
'smtp_port' => 587,
'crlf' => "\r\n",
'newline' => "\r\n"
));
$this->email->set_mailtype("html");
//$email_body ="<div>hello world</div>";
$data['msg']="Hello How r u?";
$data['userName']="Radhey Mishra";
//$message = $this->load->view('my_email_template', $data, true);
$message = "Please find the attached file.";
$this->email->from('radheykri@gmail.com', $data['userName']);
$this->email->to('radhey.krishna@myla.in');
$this->email->cc('jalaj.pathak@myla.in');
$this->email->bcc('radheykri@gmail.com');
$this->email->subject('CSV attachment');
$this->email->message($message);
//$this->email->message($email_body);
$this->email->attach($filename);
try {
$send['msg']=$this->email->send();
if($send['msg']!=true ) {
throw new Exception('no mail send please try after some time.');
}
} catch (Exception $e) {
//alert the user.
echo $e->getMessage();
}
//echo $this->email->print_debugger();
}
}
***************View****************
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Create csv using codeigniter</title>
<?php //echo base_url();?>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Latest compiled and minified Jquery library -->
<script src='https://code.jquery.com/jquery-2.1.1.min.js'></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="<?php echo base_url() ?>">Export Customer records in CSV format</a>
</div>
<ul class="nav navbar-nav pull-right">
<li class="active"><a href="<?php echo base_url()?>index.php/home/csv"><i class="glyphicon glyphicon-log-in"></i> Export Csv</a></li>
</ul>
</nav>
</div>
</div>
</div>
<!--<div class="container">
<table class="table table-striped" style="width: 100%">
<thead><th>S N</th>
<th>Customer Name</th>
<th>Customer Email</th>
</thead>
<tbody>
<?php foreach ($rs->result() as $row): ?>
<tr><td><?php echo $row->customerid ?></td>
<td><?php echo $row->customername ?></td>
<td><?php echo $row->customeremail?></td>
<td><?php //echo $row->Admin_AclDEPTID ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div> -->
</body>
</html>
class Home extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('email');
$this->load->helper('file');
$this->load->database();
}
public function index()
{
$data['rs'] = $this->db->get('customer_profile');
$this->load->view('home', $data);
}
// Download for CSV file.
// http://localhost/codeigniterR/index.php/home/csv
public function csv($query, $filename = 'CSV_Report.csv')
{
$this->load->dbutil();
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$enclosure = '"';
$query = $this->db->query("select customeremail as Radhey from customers");
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
// client side download save as per client
force_download($filename, $data);
}
// Send mail with attached CSV file
// http://localhost/codeigniterR/index.php/home/sendcsvmail
public function sendcsvmail($filename = 'CSV_Report_Radhey.csv')
{
$this->load->dbutil();
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$enclosure = '"';
$query = $this->db->query("select customeremail as Radhey from customers");
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
// Write the file to server optional file_put_contents
//file_put_contents($filename, $data);
// Load the file helper and write the file to server
write_file($filename, $data);
$this->sendmail($filename);
unlink($filename);
}
function sendmail($filename)
{
$this->email->initialize(array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.sendgrid.net',
'smtp_user' => '**********',
'smtp_pass' => '***************',
'smtp_port' => 587,
'crlf' => "\r\n",
'newline' => "\r\n"
));
$this->email->set_mailtype("html");
//$email_body ="<div>hello world</div>";
$data['msg']="Hello How r u?";
$data['userName']="Radhey Mishra";
//$message = $this->load->view('my_email_template', $data, true);
$message = "Please find the attached file.";
$this->email->from('radheykri@gmail.com', $data['userName']);
$this->email->to('radhey.krishna@myla.in');
$this->email->cc('jalaj.pathak@myla.in');
$this->email->bcc('radheykri@gmail.com');
$this->email->subject('CSV attachment');
$this->email->message($message);
//$this->email->message($email_body);
$this->email->attach($filename);
try {
$send['msg']=$this->email->send();
if($send['msg']!=true ) {
throw new Exception('no mail send please try after some time.');
}
} catch (Exception $e) {
//alert the user.
echo $e->getMessage();
}
//echo $this->email->print_debugger();
}
}
***************View****************
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Create csv using codeigniter</title>
<?php //echo base_url();?>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Latest compiled and minified Jquery library -->
<script src='https://code.jquery.com/jquery-2.1.1.min.js'></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="<?php echo base_url() ?>">Export Customer records in CSV format</a>
</div>
<ul class="nav navbar-nav pull-right">
<li class="active"><a href="<?php echo base_url()?>index.php/home/csv"><i class="glyphicon glyphicon-log-in"></i> Export Csv</a></li>
</ul>
</nav>
</div>
</div>
</div>
<!--<div class="container">
<table class="table table-striped" style="width: 100%">
<thead><th>S N</th>
<th>Customer Name</th>
<th>Customer Email</th>
</thead>
<tbody>
<?php foreach ($rs->result() as $row): ?>
<tr><td><?php echo $row->customerid ?></td>
<td><?php echo $row->customername ?></td>
<td><?php echo $row->customeremail?></td>
<td><?php //echo $row->Admin_AclDEPTID ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div> -->
</body>
</html>
Comments
Post a Comment