Send Email with Attachment Using Cakephp Email Component

Using The Email Component (Making Calls)

In order to send emails now there are two things we need to do:

  1. Add call to component in controller variables
  2. Make call to email functions as needed

Including component in a controller;
(placed at top of controller class, along with $helpers and $name variable)

In Controller  Add Email Component

<em><strong>Class EmailController extends AppController { 
 var $name="Email"; 
 var $components = array ('Email');
 var $uses = NULL;

 function beforeFilter() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parent::beforeFilter(); 
 function EmailSend(){
 $Path = WWW_ROOT."img";
 $<em><strong>fileName</strong></em> = 'test.jpg';
 $this-&gt;Email-&gt;from&nbsp;&nbsp;&nbsp; = 'hassanblog&lt;;';
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $this-&gt;Email-&gt;to&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 'Test&lt;;';
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $this-&gt;Email-&gt;subject = 'Test Email Send With Attacment';
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$this-&gt;Email-&gt;attachments = array($Path.$fileName);
 &nbsp;&nbsp;&nbsp; &nbsp;$this-&gt;Email-&gt;template = 'simple_message';
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; $this-&gt;Email-&gt;sendAs = 'html';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;if($this-&gt;Email-&gt;send()){
 $this-&gt;session-&gt;setFlash("Email Send Successfully");

Using this action you can easily use Email component.

Note:Must be put the email component file to the component folder.

Leave a Reply