Save send emails

I add some functionality to your project for my needs but i think it will be great to have it in future release. I know the way i add this is brutal and not preaty but it works for me. If you hav eny advice for me plz let me know.

I add table “ip_emails_sent” with fields id, invoice_id, to, cc, bcc, subject, timestamp

Create controller in “invoices/models//mdl_invoices_save_emails.php”

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Mdl_invoices_save_emails extends CI_Model
{
    public function save_send($invoice_id, $subject, $to, $cc, $bcc)
    {
        $query = $this->db->query("
          INSERT INTO `23504194_0000001`.`ip_emails_sent` (`invoice_id`, `to`, `cc`, `bcc`, `subject`) 
          VALUES ('$invoice_id', '$to', '$cc', '$bcc', '$subject');
        ");
    }
    public function email_sends($invoice_id)
    {
        $query = $this->db->query("
          SELECT * 
          FROM  `ip_emails_sent` 
          WHERE `invoice_id` = $invoice_id
        ");
        return $query->result();
    }
}

Change “invoices/controllers/cron.php”

add lines 29-31

$this->load->model('invoices/mdl_invoices');
$this->load->model('invoices/mdl_invoices_save_emails');
$this->load->helper('mailer');

add lines 110-115

if (email_invoice($target_id, $pdf_template, $from, $to, $subject, $body, $cc, $bcc, $attachment_files)) {
 $this->mdl_invoices->mark_sent($target_id);
 $this->mdl_invoices_save_emails->save_send($target_id, $subject, $to, $cc, $bcc);
 $this->mdl_invoice_amounts->calculate($target_id);
} else {

Change "invoices/controllers/invoices.php"
in view function i add my model load at line arround 122

$this->load->model('invoices/mdl_invoices_save_emails');

and load to view at lines arround 143

$this->layout->set(
array(
    'invoice' => $invoice,
    'emails_sent' => $this->mdl_invoices_save_emails->emails_sent($invoice_id),
    'items' => $this->mdl_items->where('invoice_id', $invoice_id)->get()->result(),

Change “mailer/controllers/mailer.php”

public function send_invoice($invoice_id)
{
    if ($this->input->post('btn_cancel')) {
        redirect('invoices');
   }
   if (!$this->mailer_configured) return;
   $this->load->model('upload/mdl_uploads');
    $from = array(
       $this->input->post('from_email'),
       $this->input->post('from_name')
    );
    $pdf_template = $this->input->post('pdf_template');
    $to = $this->input->post('to_email');
    $subject = $this->input->post('subject');
    if (strlen($this->input->post('body')) != strlen(strip_tags($this->input->post('body')))) {
        $body = htmlspecialchars_decode($this->input->post('body'));
    } else {
        $body = htmlspecialchars_decode(nl2br($this->input->post('body')));
    }
    $cc = $this->input->post('cc');
    $bcc = $this->input->post('bcc');
    $attachment_files = $this->mdl_uploads->get_invoice_uploads($invoice_id);
    if (email_invoice($invoice_id, $pdf_template, $from, $to, $subject, $body, $cc, $bcc, $attachment_files)) {
        $this->mdl_invoices->mark_sent($invoice_id);
        **$this->load->model('invoices/mdl_invoices_save_emails');**
        **$this->mdl_invoices_save_emails->save_send($invoice_id, $subject, $to, $cc, $bcc);**
        $this->session->set_flashdata('alert_success', trans('email_successfully_sent'));
        redirect('invoices/view/' . $invoice_id);
    } else {
        redirect('mailer/invoice/' . $invoice_id);
    }
}

And finaly the moust dirty work i added view of sent emails in invoice view.php at lines arround 507 its bottom of the page right after URL for Guest

<div class="row">
>     <label>
>         Wysłane emaile:
>     </label>
>     <table class="table table-hover">
>     <th>
>         Temat
>     </th>
>     <th>
>         Do
>     </th>
>     <th>
>         Do wiadomości
>     </th>
>     <th>
>         Ukryte do wiadomości
>     </th>
>     <th>
>         Data wysłania
>     </th>
>             <?foreach ($emails_sent as $row){?>
>                 <tr> 
>                     <td> <?=$row->subject?> </td>
>                     <td> <?=$row->to?> </td>
>                     <td> <?=$row->cc?> </td>
>                     <td> <?=$row->bcc?> </td>
>                     <td> <?=$row->timestamp?> </td>
>                 </tr>
>             <?}?>
>     </table>
> </div>

Last Update: 11.04.2017

Is this basically a log of all sent emails? If so, I would like to have this feature implemented.

I would also like this feature implemented.