Skip to content

Instantly share code, notes, and snippets.

@kura1420
Last active January 29, 2019 03:00
Show Gist options
  • Select an option

  • Save kura1420/4fb4e39da878633844a75993cfd34f1d to your computer and use it in GitHub Desktop.

Select an option

Save kura1420/4fb4e39da878633844a75993cfd34f1d to your computer and use it in GitHub Desktop.
Controller with Codeigniter to send period email
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Send extends CI_Controller {
public function __construct()
{
parent::__construct();
// set library
$this->load->library('email');
$config['protocol']='smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_port'] = 587;
$config['smtp_timeout']='30';
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = '******';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$config['smtp_crypto'] = 'tls';
$this->email->initialize($config);
}
public function end_of_month()
{
// get date end this month
$end = date('Y-m-t');
// today
$today = date('Y-m-d');
if ($today === $end) {
// send email
$email = '[email protected]';
$this->email->from('[email protected]', 'Bot');
$this->email->to($email);
$this->email->subject('Information for Month');
$this->email->message('Email every month');
// get attachment
$this->email->attach(base_url('/uploads/sample.pdf'));
// print result
if ($this->email->send(FALSE))
{
// success
$this->email->print_debugger(array('headers'));
}
else
{
// error message
echo $this->email->print_debugger(array('headers'));
}
}
}
public function every_two_day()
{
// set date
$full_date = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
$today = date('Y-m-d');
// loop date
for ($i=1; $i<=$full_date; $i++) {
$go_send = $i % 2 ? 1 : 0; //get 2 day from this month
// if current two day, then send email
if ($go_send === 0) {
// get date to match today
$date = date('Y-m-' . $i);
if ($today === $date) {
// send email
$email = '[email protected]';
$this->email->from('[email protected]', 'Bot');
$this->email->to($email);
$this->email->subject('Information for Every 2 day');
$this->email->message('Email every 2 days');
// get attachment
$this->email->attach(base_url('/uploads/sample.pdf'));
// print result
if ($this->email->send(FALSE))
{
// success
$this->email->print_debugger(array('headers'));
}
else
{
// error message
echo $this->email->print_debugger(array('headers'));
}
}
}
}
}
public function finish()
{
// send email
$email = '[email protected]';
$this->email->from('[email protected]', 'Bot');
$this->email->to($email);
$this->email->subject('Information for Finish Transaction');
$this->email->message('Email completed transaction');
// get attachment
$this->email->attach(base_url('/uploads/sample.pdf'));
// print result
if ($this->email->send(FALSE))
{
// success
$this->email->print_debugger(array('headers'));
}
else
{
// error message
echo $this->email->print_debugger(array('headers'));
}
}
}
/* End of file Send.php */
/* Location: ./application/controllers/Send.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment