用codeigniter发送电子邮件|在哪里存储代码


Sending email with codeigniter | Where to store code?

我使用以下代码在codeigniter中发送电子邮件,并想知道放置代码的最佳位置。

它目前存在于我的控制器中,但是太笨重了,我相信有一个更好的地方放它。

我的电子邮件代码的工作方式是,我设置了我想在电子邮件中的变量,然后将它们输入到模板中并发送。

我将有一个以上的电子邮件被发送到我的网站上,所以我应该把代码和如何。(图书馆助手? ?)

代码:

                $this->load->library('email');
                $this->email->initialize();
                $this->email->from('noreply@domain.com', 'domainy');
                $email = $this->input->post('email_address');
                $this->email->to($email); 
                $this->email->subject('Great question. We will answer it as soon as we can');
                $emaildata['title'] = 'Company - Ask us a question';
                $emaildata['preheader_teaser'] = 'Your question has been received and we will be answering it shortly. Please keep an eye on your inbox for our response.';
                $emaildata['preheader_links'] = '   <em><b>Do not reply to this email</b></em>';
                $emaildata['header_image'] = 'http://www.domain.com/assets/img/logo.png';
                $emaildata['body_content'] = '
                <h1>Hi '.$this->input->post('first_name').',</h1>
                <h2 style="color:#ccc !important;">Great question!</h2>
                <p>Your question has been submitted to our consultants and received with thanks. We will answer you question as soon as we can.</p>
                <p>Response generally takes within 24 to 48 hours and will be in the form of an email or (if provided) a telephone call.</p>
                <p>We will handle your question in the most efficient way possible and get back to you with an answer as soon as we can.</p>
                <p>Have a super day!</p>
                <h4  style="color:#1a3665 !important;">Your question to us:</h4>
                '.$this->input->post('question').'          
                ';
                $emaildata['footer_social_links'] = 'Follow us on ';
                $emaildata['footer_copyright'] = 'Copyright &copy; domain MMXI, All rights reserved.';
                $emaildata['footer_description'] = '"Impartial, help and assistance, as and when you need it"';
                $emaildata['footer_mailing_address'] = 'enquiries@domain.com';
                $emaildata['footer_utility_links'] = '<a href="http://www.domain.com/">www.domain.com</a>';
                $emaildata['footer_rewards'] = '&nbsp;';
                $emailbody = $this->load->view('email/templates/simple-basic',$emaildata,true);
                $this->email->message($emailbody);  
                $this->email->send();

好吧,没有什么可说的:如果你不能减少代码,无论你把它放在哪里,它总是那么"庞大":)

如果是我,我会把它存储在一个模型中。因为在我看来…一个你会多次使用的模型,所以它很适合这个概念。但是图书馆也可以,我不是那个意思。

让我震惊的是,除了Post之外,所有的视图变量都是硬编码的…你是在告诉我,你是根据调用电子邮件库的方法硬编码不同的值,还是这些都是固定的?在后一种情况下,为什么不创建一个"模板"视图,其中唯一的变量实际上是真正的变量文本?(我的意思是"post")。

如果这些是变量,但在某种程度上,您也可以考虑使用自定义配置文件保存每个键的不同可能性。


总之,这真的取决于每个电子邮件的定制程度:

  1. 如果唯一改变的是post值,只传递那些到模板。
  2. 如果其他字段正在变化,但不是很多,并且都在固定的可能性范围内,则使用自定义配置文件。
  3. 如果你打算每次声明一个不同的变量,那么,无论你把它放在哪里,你都必须重新写一遍,所以控制器,模型或库不会改变任何东西。
  4. 如何使用数据库,其中存储不同的模板?您可以使用内置的模板库来更改变量位,然后将处理后的模板传递给电子邮件库。

我等待进一步的细节来改进我的答案

将此代码放入函数中,并将该函数放入库中,并将该库包含到控制器中。当你需要发送邮件时,只需从控制器调用该函数,这是更管理的方式。