Example 2 – sending e-mails with attachments

In this example, we will see how the controller can load a mail service library and use it to send mail attachments.

The CI mail library is not an auto-loaded library, and hence, will be loaded by the controller we are building for sending e-mail.

The CI mail library makes it easy to send subject messages of non-English languages supporting UTF-8 both for the subject and the mail body. Adding attachments to an e-mail becomes a piece of cake using the CI mail library. We only need to have the files on a known directory path in our server and refer to them to attach them to the mail.

We can attach one or more files to create the mail body. HTML/TEXT is defined via a simple configuration setting to the CI mail library.

This example will be constructed from the mail controller only; you may add a rendered view later on to add to the example report on the mailing list, sending a report of a list of e-mail destinations instead of just one or two destinations.

Let us specify the default controller filename as controller/mail.

Let us assume the URI to the project root is http://mydomain.com/myproject. Hence, the URI to execute the controller for sending the mail will be http://mydomain.com/myproject/mail.

We shall remember that in CodeIgniter, if you refer only to the controller URI path, the CI will operate the function controller class index()function, if any. In case the controller class does have index()function. And actually in any case, the controller constructor will be called to create the class instance.

The controller file

The controller file, controller/email.php, will initially load the CI mail library, then it will configure the mail service properties, such as from/to e-mail address, subject, HTML body, and the attachment files. Finally, the controller will issue the e-mail send service of the library, getting back the operation completion status to report to the web user. In case of a failure, the controller will render a report for the reason of the failure with debugging information provided by the CI mail library.

Note

The helpers used are provided with the sample source code provided with this book.

Since this controller example has several functions, we shall review their usage initially, before we review the code as follows:

  • __construct(): This contractor loads the CI e-mail library to be used by other functions
  • index(): This builds the e-mail message and sends it to its destination
  • doc_root_path(): This provides the directory path to find the e-mail attachments to send

Regarding the need to load resources such as libraries, helpers, and models, the best practice is the amount of usage in our controller. Let's say, for example, that we have 40 controllers and 39 of them need the same library. We shall add that library into the auto-load list, /config/autoload.php. If we did add a resource, such as a library, model, or helper into the project auto-load, we can eliminate loading it in the class that needs the resource services as follows:

class Email extends CI_Controller
// The controller/email.php file will contain this class
{
  function __construct()
  {
    // call the parent constructor to inherit all its services
    parent::__construct();
    // Loads the CI e-mail library, so that it will be instantiated // and its methods will be accessed, as $this->mail->METHOD_NAME.
    $this->load->library('email'),
    }
  // Define the controller methodindex (the default method), so that referring to the URI mydomain.com/myproject/email will execute the index method call
  function index() {
    // Configure the library to work with UTF-8 strings // multi-language support, as well as enable HTML content body.
    $config['charset'] = 'utf-8';
    $config['mailtype'] = 'html';
    
    // Loads the configuration settings by initialize method
    $this->email->initialize($config);
    // Since the mail body is HTML, define CR/LF to be // replaced with HTML <BR/>
    $this->email->set_newline("<BR/>");
    // Define the 'From' Email address
    $this->email->from('[email protected]', 'Eli Orr'),
    
    // Define the 'To' Email/s
    $this->email->to (array('"Name 1" <[email protected]>', '"Name 2" <[email protected]>'));
    // Set the e-mail subject
    $this->email->subject ("This is the Subject – can be ANY UTF-8 chars");
    // Define the e-mail body in HTML format, as we set the message to be HTML typed
    $this->email->message
    ('<H1>Hello there!<H1/>
    <p>
    This Email is sent from CI via its cool e-mail library)<BR/>
    <font color=green size=3><b>See Attached Files</b></font><BR/>
    Attachedfiles: <BR/>
    <ul>
    <li> 1 - File One.</li>
    <li> 2 - Second File </li>
    </p>
    );
    
    // Load the attachments
    $path = $this->doc_root_path ();
    // Doc root For example, /home/yourdomain.com/public_html
    // Let say attachment under public_html as /attachments
    $attachment_path1 = $path."/attachments/file1.jpg";
    $attachment_path2 = $path."/attachments/file2.php";
    
    // Set the two attachment file paths
    $this->email->attach($attachment_path1);
    $this->email->attach($attachment_path2);
    
    // We have the e-mail object ready! Let us send it!
    // execute send and check the result status
    if ($this->email->send())
    {
      // The e-mail was sent successfully.
      echo 'Your email was sent!';
      }
    else {
      // We had some problems, let's show what was wrong
      echo $this->email->print_debugger();
      }
    }
  functiondoc_root_path () {
    // An auxiliary method for calculating attachment // file path in our server 
    return $doc_root = $_SERVER["DOCUMENT_ROOT"];
    }
  }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.84