Welcome Guest, Not a member yet? Register   Sign In
DRY sendEmail() ?
#1

(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'            => 'i[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.
Reply
#2

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'); 
Reply
#3

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.
Reply
#4

Both of you should be checking the send method to make sure it did not error.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

@insidefx

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

@Bhavesh

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

please explain thanks.
Reply
#7

(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.
Reply
#8

@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
Reply
#9

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();
    }
Reply
#10

See http://www.codeigniter.com/user_guide/li...mail::send

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




Theme © iAndrew 2016 - Forum software by © MyBB