Location: PHPKode > projects > GnomePHP > peec-GnomePHP-b5a360b/gnomephp/mvc/Mailer.php
<?php
namespace gnomephp\mvc;

use gnomephp\Configuration;
/**
 *
 * Notifier, Mailer
 * @author peec
 *
 */
abstract class Mailer extends Controller{
	
	private $content = '';

	/**
	 * 
	 * Message object.
	 * @var \Swift_Message
	 */
	private $message = null;

	public function __construct(){
		parent::__construct();

		// Load the swiftmailer library.
		require_once  GNOME_FW_PATH . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR . 'swiftmailer' . DIRECTORY_SEPARATOR . 'swift_required.php';
		

		$this->message = $this->newMessage();

	}



	/**
	 * Creates a new message and deletes the current message.
	 * @return \Swift_Message
	 */
	protected function newMessage(){
		$this->message = \Swift_Message::newInstance();
		return $this->message; 
	}

	/**
	 * Gets the current message.
	 * @return \Swift_Message
	 */
	protected function message(){
		return $this->message;
	}

	/**
	 * Sends the email, returns the output of mail() function.
	 * @param string $viewFile The view file name where the mail is written in a view file.
	 * @return int The amount of messages sent.
	 * @throws \Swift_TransportException
	 */
	protected function send($viewFile=null){

		$mailSettings = Configuration::get('application', 'mail');

		// There are 3 transport routes available.
		switch($mailSettings['transport']){
			case 'smtp':

				$transport = \Swift_SmtpTransport::newInstance($mailSettings['smtp']['host'], $mailSettings['smtp']['port'], ($mailSettings['smtp']['encryption'] ? $mailSettings['smtp']['encryption'] : null));

				if($mailSettings['smtp']['username'])$transport->setUsername($mailSettings['smtp']['username']);
				if($mailSettings['smtp']['password'])$transport->setPassword($mailSettings['smtp']['password']);

				break;
			case 'mail':
				$transport = \Swift_MailTransport::newInstance();
				break;
			case 'sendmail':
				$transport = \Swift_SendmailTransport::newInstance($mailSettings['sendmail']['command']);
				break;
			default:
				throw new MailerException(sprintf("%s is not a transport route available for sending emails. You may select either: smtp, mail or sendmail.", $mailSettings['transport']));
		}

		$mailer = \Swift_Mailer::newInstance($transport);



		if ($viewFile !== null)$this->content = $this->view->render($viewFile, true);


		$mailer = \Swift_Mailer::newInstance($transport);

		//Create a message from view if not already set.
		if ($viewFile !== null)$this->message->setBody($this->content);

		//Send the message
		$result = $mailer->send($this->message);
		
		return $result;
	}



}
Return current item: GnomePHP