CodeIgniter:自定义电子邮件功能不起作用

时间:2014-06-17 10:50:17

标签: php codeigniter email

我必须发送有关各种活动的电子邮件通知。就像插入记录,更新记录,删除记录事件一样。

工作代码: 我在控制器中使用它并且工作正常,没有问题

$email_hr       = $this->config->item('hr_email_templates');
$email_template = $this->config->item('employee_new_email');

$message = $this->load->view($email_hr . $email_template, $data, true);

$this->email->clear();
$this->email->from('system@myemail.com', get_config_option('site_name'));
$this->email->to(get_user_data('email', 1));
$this->email->subject(get_config_option('site_name') . ' - ' . $this->lang->line('employee_new_subject'));
$this->email->message($message);

if ($this->email->send()) {
    // do the stuffs
}

由于我必须在更多的控制器(事件)中重复这一点,我想要创造一个让我的生活更轻松的功能。所以我做了如下。但这根本不起作用,也没有发送任何电子邮件。

功能

function email_notification($subject, $message, $to = FALSE, $from = FALSE, $from_name = FALSE)
{
    $to        = isset($to) ? $to : get_user_data('email', 1);
    $from      = isset($from) ? $from : 'system@myemail.com';
    $from_name = isset($from_name) ? $from_name : get_config_option('site_name');

    $CI = get_instance();

    $CI->email->clear();
    $CI->email->from($from, $from_name);
    $CI->email->to($to);
    $CI->email->subject(get_config_option('site_name') . ' - ' . $subject);
    $CI->email->message($message);

    return TRUE; // also tried using return TRUE
}

用法

$email_hr       = $this->config->item('hr_email_templates');
$email_template = $this->config->item('employee_new_email');

$message = $this->load->view($email_hr . $email_template, $data, true);

email_notification($this->lang->line('employee_new_subject'), $message, get_user_data('email', 1));//get_user_data($field, $id)

以上功能不发送任何电子邮件,所以

if ($this->email->send()) {
    // do the stuffs
}

永远不会实现停止执行某些内容而不发送电子邮件

1 个答案:

答案 0 :(得分:0)

查看您的代码,电子邮件未在email_notification函数中发送。

function email_notification($subject, $message, $to = FALSE, $from = FALSE, $from_name = FALSE)
{
          $to        = isset($to) ? $to : get_user_data('email', 1);
          $from      = isset($from) ? $from : 'my@email.com';
          $from_name = isset($from_name) ? $from_name : get_config_option('site_name');

          $CI = get_instance();

          $CI->email->clear();
          $CI->email->from($from, $from_name);
          $CI->email->to($to);
          $CI->email->subject(get_config_option('site_name') . ' - ' . $subject);
          $CI->email->message($message);

          // SEND THE EMAIL....
          return $CI->email->send();
      }

修改

这是一个用于发送电子邮件的工作函数,但我将其用作库,而不是帮助器。

function send_email($subject, $message, $to = null, $from = null, $from_name = null)
{
      $this->load->library('email');

      $config = array( 
                'wordwrap' => false,
                'mailtype' => 'html',
                'crlf' => "\r\n",
                'newline' => "\r\n" 
      );

      $this->email->initialize($config);

      if ( ! $from ) { $from = 'from@emailaddress.com'; }
      if ( ! $from_name ) { $from = 'from name'; }
      if ( ! $to ) { $from = 'myemail@address.com'; }

      $this->email->from($from);
      $this->email->subject($subject);
      $this->email->to($to);

      $this->email->message($message);

      $this->email->send();                    
}