2011-12-04 51 views
0

我正在使用以下代码在codeigniter中发送电子邮件,并且想知道放置代码的最佳位置在哪里。用codeigniter发送电子邮件|在哪里存储代码?

它目前存在于我的控制器中,但体积庞大,我相信它有一个更好的地方。

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

我将在我的网站上发送多个电子邮件,因此我应该在哪里放置代码和方式。 (图书馆助手?)

代码:

   $this->load->library('email'); 
       $this->email->initialize(); 

       $this->email->from('[email protected]', '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'] = '[email protected]'; 
       $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(); 

回答

1

那么,有没有什么好说的:如果你不能减少代码,无论你把它放在它始终是“笨重” :)

我,我会将它存储在模型中。对我来说,它看起来像一个模型,你会多次使用,所以它很适合这个概念。但一个图书馆也可以,我不是这样说的。

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

如果这些都是变量,但在某种程度上,你也可以考虑使用一个自定义配置文件,在每个关键字中持有不同的可能性。


总结,这实际上取决于程度的定制每个电子邮件有:

  1. 如果唯一的变化就是后的值,通过只有那些为模板。
  2. 如果其他字段正在改变,但不是很多,并且都具有固定范围的可能性,请使用custom config文件。
  3. 如果您要每次声明一个不同的变量,那么无论您放置哪个变量,都必须重新写入它们,因此控制器,模型或库不会更改任何内容。
  4. 怎么样使用数据库,它存储不同的模板?您可以使用内置的变量只更改可变位,然后将处理后的模板传递到电子邮件库。

我在等待着进一步的细节,以提高我的回答

0

将这个代码的函数和地点,在一个库函数,并包括图书馆到控制器中.. 时,你只是需要发送电子邮件从你的控制器调用该功能,这是更多的管理方式..