I am using the following code to send email in codeigniter and would like to know where the best place to put the code is.
It currently exists in my controller but is to bulky and I am sure there is a better place for it.
The way my email code works is that I set the variables that I want in the email and then input them into the template and send.
I will be having more than one email being sent on my site so where should I put the code and how. (library helper??)
Code:
$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();
Well, there’s not much to say: if you cannot reduce the code, wherever you put it it’s always that “bulky” 🙂
Me, I would have stored it in a model. Since to me it looks like…a model, that you’re going to use multiple times, so it fits nicely in the concept. But a library would be fine too, I’m not saying that.
Something that strikes me, is that all your view variables – apart from the Post one – are hardcoded…Are you telling me you’re hardcoding different values depending on the method that calls the email library, or those are all fixed? In the latter case, why not just create a “template” view with the only variable being the, actually, really variable text? (“post” ones, I mean).
If those are variables, but to some extent, you might also considering using a custom config file holding in each key the different possibilities.
Summing up, it really depends on the degree of customization each email has:
I await for further details to improve my answer