如何创建codeigniter类以将所有电子邮件存储到表中

时间:2014-01-28 05:20:55

标签: codeigniter class email helper

如何在codeigniter中创建一个帮助程序类来存储在我的网站中发送和接收的所有电子邮件。我需要使用所有电子邮件功能调用该类

$this->load->library('myclass');

如果我调用此类,则此函数应将电子邮件的$ to,time,body和subject存储到表(table1)。怎么可能?

2 个答案:

答案 0 :(得分:0)

创建一个名为“myemail”的库,并将其放在应用程序/库中。

application/libraries/myemail.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CI_Myemail
{
    public function __construct()
    {      
        $this->CI =& get_instance();
    }
        public function saveEmail($to,$body,$subject)
        {
             $this->CI->load->model("table_model");
             $this->table_model->save(array("to"=>$to,"body"=>$body,"subject"=>$subject,"mail_sent_time"=>date("Y-m-d H:i:s")));
        }
}

然后你必须创建table_model和write函数来将数据保存到数据中。

在控制器中,您必须将此库加载为

$this->load->library('myemail');

在控制器中,您必须调用

$this->myemail->saveEmail($this->to,$body,$subject);// Here, $this->do is controller variable as you mentioned in comment

答案 1 :(得分:0)

您可以通过帮助函数发送电子邮件轻松完成此操作。您需要在哪里发送电子邮件调用该功能。在调用发送电子邮件的方法之前,函数内部将数据保存到表中。此外,您可以保存电子邮件而不发送状态待定,并通过cronjob发送以改善用户体验。我在我的网站上做同样的事情。

帮助函数正如我在下面使用它。您可以根据需要进行调整。从控制器调用时,数据数组应具有所有细节。

function sendEmail($data, $immediate=FALSE) {
$subject = $data['subject'];
$to = $data['to'];
$viewName = $data['template'];
$CI = & get_instance();

$CI->config->load("thephpcode.com");
$from = $CI->config->item('Sender');
$fromName = $CI->config->item('SenderName');
$priority = $CI->config->item('Priority');

if (isset($data['from'])) {
    $from = $data['from'];
    $fromName = $data['fromName'];
}

if (isset($data['priority']))
    $priority = $data['priority'];

$body = $CI->load->view($viewName, $data, TRUE);

if ($from == "") {
    log_message('error', 'From value is not set in Email helper for sending email');
    return;
}
$bcc = '';
if (isset($data['bcc'])) {
    $bcc = $data['bcc'];
}
/*
$replyto ='';
if (isset($data['reply_to'])) {
    $replyto = $data['reply_to'];
    $replytoname = $data['reply_to_name'];
}
*/
$status = 'Pending';
if ($immediate)
{
    $status = 'Sent';
}
$dbdata = array();
$dbdata['from'] = $from;
$dbdata['fromName'] = $fromName;
if (isset($data['reply_to'])) {
    $dbdata['replyto'] = $data['reply_to'];
    $dbdata['replytoname'] = $data['reply_to_name'];
}
$dbdata['to'] = $to;
$dbdata['subject'] = $subject;
$dbdata['body'] = $body;
$dbdata['bcc'] = $bcc;
$dbdata['status'] = $status;
$dbdata['priority'] = $priority;
$CI->load->model('email_model', 'email_model');
$CI->email_model->insert($dbdata, 'email_queue');

if (!$immediate)
{
    return TRUE;
}
//Send the email    
$CI->load->library('email');
$CI->email->initialize($CI->config->item('email_config'));

$CI->email->from($from, $fromName);
$CI->email->to($to);
if (isset($data['bcc']))
{
    $CI->email->bcc($data['bcc']);
}
if (isset($data['reply_to']))
{
    $CI->email->reply_to($data['reply_to'],$data['reply_to_name']);
}
$CI->email->subject($subject);
$CI->email->message($body);
$CI->email->send();

return;
}