CodeIgniter Forums
DRY sendEmail() ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: DRY sendEmail() ? (/showthread.php?tid=63669)

Pages: 1 2


DRY sendEmail() ? - solidcodes - 11-25-2015

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.


RE: DRY sendEmail() ? - Bhavesh - 11-25-2015

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'); 



RE: DRY sendEmail() ? - solidcodes - 11-25-2015

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.


RE: DRY sendEmail() ? - InsiteFX - 11-26-2015

Both of you should be checking the send method to make sure it did not error.


RE: DRY sendEmail() ? - solidcodes - 11-26-2015

@insidefx

can you show us your sample codes please.
thanks in advance.


RE: DRY sendEmail() ? - solidcodes - 11-26-2015

@Bhavesh

why did you used/call this code below?
Code:
$CI =& get_instance();

please explain thanks.


RE: DRY sendEmail() ? - orionstar - 11-26-2015

(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.


RE: DRY sendEmail() ? - solidcodes - 11-26-2015

@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


RE: DRY sendEmail() ? - solidcodes - 11-26-2015

here is the updated codes under helper folder now.
Code:
function sendEmail($data = array())
    {
        $CI =& get_instance();
        $CI->load->library('email');        
        
        $CI->email
            ->from($data['from_email'], $data['from_name'])
            ->to($data['to'])
            ->cc($data['cc'])
            ->subject($data['subject'])
            ->message($data['message'])
            ->set_mailtype($data['mailtype']);        

        $CI->email->send();
    }



RE: DRY sendEmail() ? - kenjis - 11-26-2015

See http://www.codeigniter.com/user_guide/libraries/email.html#CI_Email::send

The send() method returns FALSE on failure.
You should take care of the case.