我使用以下代码在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 © 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'] = ' ';
$emailbody = $this->load->view('email/templates/simple-basic',$emaildata,true);
$this->email->message($emailbody);
$this->email->send();
答案 0 :(得分:1)
嗯,没什么好说的:如果你不能减少代码,无论你把它放在哪里,它总是那么“笨重”:)
我,我会把它存放在一个模型中。从我看来,它看起来像是一个模型,你将要多次使用,所以它非常适合这个概念。但是图书馆也没关系,我不是这么说的。
令我印象深刻的是,所有的视图变量 - 除了Post one之外 - 都是硬编码的...你是否告诉我你根据调用电子邮件库的方法硬编码不同的值,或者那些都是固定?在后一种情况下,为什么不创建一个“模板”视图,唯一的变量实际上是真正的变量文本? (“发布”,我的意思是)。
如果这些是变量,但在某种程度上,您可能还会考虑使用自定义配置文件,在每个键中保存不同的可能性。
总结一下,这实际上取决于每封电子邮件的自定义程度:
我等待进一步的细节以改善我的答案
答案 1 :(得分:0)
将此代码放入函数中并将该函数放入库中,并将该库包含在控制器中。 无论何时你需要发送电子邮件,只需从你的控制器调用该功能,这是更多的管理方式..