Friday, 27 May 2016

Loading

Php Codeigniter Use Sending Email

Php Code igniter Use Sending Email: 

Code Igniter is much easier to sending email. You also configure the preferences regarding email in Code Igniter.Code Igniter's robust Email class.




               1.   Multiple Protocols: Mail, Send mail, and SMTP
        2.          Multiple recipients
        3.   CC and BCCs
        4.   HTML or Plaintext email
        5.   Attachments
        6.   Word wrapping
        7.   Priorities
      8.   BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
       9.   Email Debugging tools

Sending Email :
The from() function is used to set − from where the email is being sent and to() function is used − to whom the email is being sent. The subject() and message() function is used to set the subject and message of the email.
Make the changes in the routes.php file in application/config/routes.phpand add the following line at the end of the file.
$route['email'] = 'Email_Controller';
$this->load->library(‘email’);

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');

$this->email->to('someone@example.com'); 

$this->email->cc('another@another-example.com'); 

$this->email->bcc('them@their-example.com'); 

$this->email->subject('Email Test');

$this->email->message('Testing the email class.'); 

$this->email->send();

echo $this->email->print_debugger();
 








Example :

Controller : 
<?php 
   class Emailsend_controller extends CI_Controller {  
      function __construct() { 
         parent::__construct(); 
         $this->load->library('session'); 
         $this->load->helper('form'); 
      }          
      public function index() {    
         $this->load->helper('form'); 
         $this->load->view('email_form'); 
      }   
      public function send_mail() { 
         $from_email = "name@example.com"; 
         $to_email = $this->input->post('email');    
         //Load email library 
         $this->load->library('email');    
         $this->email->from($from_email, 'Your Name'); 
         $this->email->to($to_email);
         $this->email->subject('Email Test'); 
         $this->email->message('Testing the email class.');    
         //Send mail 
         if($this->email->send()) 
         $this->session->set_flashdata("email_sent","Email sent successfully."); 
         else 
         $this->session->set_flashdata("email_sent","Error in sending Email."); 
         $this->load->view('email_form'); 
      } 
   } ?>
View :
<!DOCTYPE html> 
<html lang = "en"> 
   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter Email Example</title> 
   </head>       
   <body> 
      <?php 
         echo $this->session->flashdata('email_sent'); 
         echo form_open('/Email_controller/send_mail'); 
      ?>                  
      <input type = "email" name = "email" required /> 
      <input type = "submit" value = "SEND MAIL">            
      <?php 
         echo form_close(); 
      ?> 
   </body>       
</html>
 
 Email Function Reference:

$this->email->from() :

Sets the email address and name of the person sending the email:
$this->email->from('you@example.com', 'Your Name');

$this->email->reply_to()

Sets the reply-to address. If the information is not provided the information in the "from" function is used. Example:
$this->email->reply_to('you@example.com', 'Your Name');

$this->email->to()

Sets the email address(s) of the recipient(s). Can be a single email, a comma-delimited list or an array:
$this->email->to('someone@example.com');
$this->email->to('one@example.com, two@example.com, three@example.com');
$list = array('one@example.com', 'two@example.com', 'three@example.com');



$this->email->to($list);

$this->email->cc()

Sets the CC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

$this->email->bcc()

Sets the BCC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

$this->email->subject()

Sets the email subject:
$this->email->subject('This is my subject');

$this->email->message()

Sets the email message body:
$this->email->message('This is my message');

$this->email->set_alt_message()

Sets the alternative email message body:
$this->email->set_alt_message('This is the alternative message');
This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative message with no HTML formatting which is added to the header string for people who do not accept HTML email. If you do not set your own message CodeIgniter will extract the message from your HTML email and strip the tags.

$this->email->clear()

Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.




foreach ($list as $name => $address)

{

    $this->email->clear();



    $this->email->to($address);

    $this->email->from('your@example.com');

    $this->email->subject('Here is your info '.$name);

    $this->email->message('Hi '.$name.' Here is the info you requested.');

    $this->email->send();

}
If you set the parameter to TRUE any attachments will be cleared as well:
$this->email->clear(TRUE);

$this->email->send()

The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used conditionally:
if ( ! $this->email->send())

{

    // Generate error

}

$this->email->attach()

Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:
$this->email->attach('/path/to/photo1.jpg');

$this->email->attach('/path/to/photo2.jpg');

$this->email->attach('/path/to/photo3.jpg');

$this->email->send();

$this->email->print_debugger()

Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging. 

Overriding Word Wrapping :

If you have word wrapping enabled (recommended to comply with RFC 822) and you have a very long link in your email it can get wrapped too, causing it to become un-clickable by the person receiving it. CodeIgniter lets you manually override word wrapping within part of your message like this:
{unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}






No comments:

Post a Comment