发送电子邮件功能

时间:2013-12-14 04:44:22

标签: php codeigniter polymorphism single-responsibility-principle

我有一个用户控制器,我用于处理注册,登录,忘记密码,查看(个人资料)页面的Codeigniter应用程序的前端,我正在尝试确定最佳位置place是用于发送电子邮件功能的功能。如果某些东西最适合图书馆功能,或者它应放在其他地方。

我问这个是因为我真的想关注OOP中的Single Responsibility PrinciplePolymorphism

有人可以提供一些他们最好的建议和/或关于我如何能够找到一个好地方的建议吗?

2 个答案:

答案 0 :(得分:1)

您可以在应用程序/帮助程序中编写用于在您自己的帮助程序(称为“general_helper.php”)中发送电子邮件的公共代码。

然后在config / autoload.php中添加“general_helper”。因此,这个助手文件将在所有地方提供。帮助者可能/可能没有上课。因此,如果你没有在helper中使用class,你可以直接调用sendmail函数

  

的sendmail($到,$从,$子,$味精,$头);

答案 1 :(得分:1)

codeigniter在system / helper / email_helper中有一个本机帮助器,它提供了一个名为send_email()的函数,该函数使用php mail()函数。虽然这是非常基础的,但它可以让您了解如何设置。

我建议创建一个帮助来覆盖原生。即在应用程序/帮助程序中创建一个MY_email_helper.php并定义自己的send_email()函数

/**
 * Send an email
 *
 * @access  public
 * @return  bool
 */
if ( ! function_exists('send_email'))
{
    function send_email($recipient, $subject, $message, $from_email = NULL, $from_name = NULL, $method = NULL)
    {
        // Obtain a reference to the ci super object
        $CI =& get_instance();

        switch(strtolower($method))
        {
            /*
             * SES Free Tier allows 2000 emails per day (Up to 10,000 per day)
             * see: http://aws.amazon.com/ses/pricing/
             */
            case 'ses':
                $CI->load->library('aws_lib');
                $sender = $from_email ? ($from_name ? $from_name.' <'.$from_email.'>' : $from_email) : NULL;
                $CI->aws_lib->send_email($recipient, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $sender);
            break;

            /*
             * Mandrill Free Tier allows 12,000 per month
             * see: http://mandrill.com/pricing/
             */
            case 'mandrill':
                // todo...
            break;

            default:
                $CI->load->library('email');
                $CI->email->from($from_email, $from_name);
                $CI->email->to($recipient);
                $CI->email->subject('=?UTF-8?B?'.base64_encode($subject).'?=');
                $CI->email->message($message);
                $CI->email->send();
                log_message('debug', $CI->email->print_debugger());
        }
    }
}

这意味着如果你已经在使用send_mail()函数,只需加载MY_email_helper,一切都会正常工作。