-
solidcodes Senior Member
   
-
Posts: 267
Threads: 27
Joined: Jun 2015
Reputation:
2
11-25-2015, 08:00 PM
(This post was last modified: 11-25-2015, 08:05 PM by solidcodes.)
I just created a simple DRY sendEmail() method.
Hoping this will save me time from not re-inventing the wheel over and over again.
here is the codes
Code: /***
* author: warren nazareno
* email: [email protected]
* purpose: Will send email message (DRY)
* Date created: November 26, 2015
***/
public function sendEmail($data = array())
{
$this->load->library('email');
$this->email
->from($data['from_email'], $data['from_name'])
->to($data['to'])
->cc($data['cc'])
->subject($data['subject'])
->message($data['message'])
->set_mailtype($data['mailtype']);
$this->email->send();
}
Below is an example of how to call the method above from controller
Code: public function callSendEmail()
{
$email_data = array(
'from_email' => '[email protected]',
'from_name' => 'George',
'to' => '[email protected]',
'cc' => '[email protected]',
'bcc' => '',
'subject' => 'Lawyers',
'message' => 'I hate lawyers',
'mailtype' => 'HTML'
);
$this->sendEmail($email_data);
}
It's just a simple first step.
currently I put it under MY_Controller.
Should I put this under helper file? or stick with MY_Controller.
thanks in advance.
-
Bhavesh Junior Member
 
-
Posts: 28
Threads: 4
Joined: Nov 2015
Reputation:
0
Hi
Create one helper file in helper folder (mail_helper.php)
and write
PHP Code: if ( ! function_exists('sendMail')) { function sendMail($toEmail,$toName,$fromEmail,$fromName,$subject,$message,$ccEmail='',$ccName='') { $CI =& get_instance(); $CI->load->library('email'); // load library $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'XXX', 'smtp_port' => XX, 'smtp_user' => '[email protected]', 'smtp_pass' => 'XXXXXXXi', 'mailtype' => 'html', 'crlf' => '\r\n', 'newline' => '\r\n', 'wordwrap' => TRUE, 'starttls' => FALSE, 'charset' => 'iso-8859-1' ); $CI->email->initialize($config); $CI->email->set_newline("\r\n"); $CI->email->from($fromEmail, $fromName); $CI->email->subject($subject); $CI->email->message($message); $CI->email->to($toEmail); $CI->email->send(); return; } }
and call or use in controller like below
PHP Code: $this->load->helper('mail_helper'); sendMail($to_email,$to_name,$from_email,$from_name,'Mail subject','Mail message');
-
solidcodes Senior Member
   
-
Posts: 267
Threads: 27
Joined: Jun 2015
Reputation:
2
actually this code below,
Code: sendMail($to_email,$to_name,$from_email,$from_name,'Mail subject','Mail message');
For starter that code is fine, but it's called longlinitis disease.
Beautiful codes are short and concise.
-
solidcodes Senior Member
   
-
Posts: 267
Threads: 27
Joined: Jun 2015
Reputation:
2
@ Bhavesh
why did you used/call this code below?
Code: $CI =& get_instance();
please explain thanks.
-
orionstar Member
  
-
Posts: 128
Threads: 4
Joined: Oct 2014
Reputation:
5
(11-26-2015, 05:00 PM)solidcodes Wrote: @Bhavesh
why did you used/call this code below?
Code: $CI =& get_instance();
please explain thanks.
In a helper you cannot access the features of CodeIgniter with the $this variable, so if you want to utilize CI's features you have to get the CI superobject which is possible with the get_instance() method.
-
solidcodes Senior Member
   
-
Posts: 267
Threads: 27
Joined: Jun 2015
Reputation:
2
@ orionstar
I'm familiar with it but I forgot because I've been to laravel.
thanks for reminding me.
This is the reason to be a good programmer you need to use it and practice it everyday.
or else you will forget.
@ Bhavesh
thanks for sharing the codes dude.
+1 for sharing
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
|