How to send email using your custom library in Zend framework

You can send email using the Zend_Mail library when coding in Zend framework but what if you want to send email from a number of your scripts. You cannot go and replicate the code on your every controller script as that would be a waste of effort and time. Not only that you would not be using the flexible power of OOP to create reusable code which you can use elsewhere in other projects in the future. I am going to create a custom library in Zend which will send out email. You will need to pass the sender email address, recipient email address, subject and body and the code will do the rest.

  1. Firsly create a folder by the name of Custom in your library folder.
  2. Then create a PHP script and name it as Functions.php
  3. Open the Functions.php script and enter the following code inside it.
addTo($to);
		$mail->setSubject($subject);
		$mail->setBodyHtml($body);
		$mail->setFrom($from);
		$mail->addBcc();
		$mail->send();

	}		

}

After adding the above code save the file and call the function from your controller as follows

Custom_Functions::sendMail(, , , );

Please note that I have declared the function as static in my Functions.php file so I can execute the function as is and do not need to instantiate it.

Please follow and like us:

Be the first to comment

Leave a Reply

Your email address will not be published.


*