Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/tools/AppMailAgent.php
<?php

/**
 * Product: Katyshop
 * @version 0.3.2.1
 * @author Catalin Hulea - hide@address.com
 * @copyright Copyright (C) 2007 Catalin Hulea
 * @license GNU General Public License version 3
 * 			You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
 * @link https://sourceforge.net/projects/katyshop
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

class AppMailAgent extends PHPMailer
{
	function AppMailAgent()
	{
		$cfg = Application::getConfigValue("mail_agent");

		// get default configuration from $cfg["mail_agent"]
		$arr = get_class_vars(get_class($this));
		foreach ($arr as $key => $value)
		{
			if(array_key_exists($key, $cfg))
				$this->$key = $cfg[$key];
		}
	}

	//###################################
	//# OVERRIDE BASE CLASS METHODS		#
	//###################################

	function clear()
	{
		parent::ClearAddresses();
		parent::ClearAllRecipients();
		parent::ClearAttachments();
		parent::ClearBCCs();
		parent::ClearCCs();
		parent::ClearCustomHeaders();
		parent::ClearReplyTos();
	}

	function Send()
	{
		$ret = parent::Send();
		$this->clear();
		return $ret;
	}

	//###################################
	//# CUSTOM APPLICATION METHDOS		#
	//###################################

	/**
	 * @param User $user
	 */
	function registrationMail($user, $pass)
	{
		$escPass = Escape::html($pass);
		$this->AddAddress($user->email);
		$this->AddReplyTo($this->From, $this->FromName);
		$this->Subject = APP_NAME . " - account created";

		$this->Body = "
			<div style=\"padding: 10px; background-color: #fffaf4; color: #582f06; font-family: verdana; font-size: 10pt;\">
			<h1 style=\"font-size: 10pt; border: 1px solid #a17f60;	padding: 5px; \">Buna ziua {$user->username},</h1>

			<p>Bine ati venit la " . APP_NAME . ", va multumim pentru ca v-ati inregistrat.</p>
			<pre>
			----------------------------
			Username: {$user->username}
			Parola: $escPass
			Cod de activare: {$user->activation_code}
			----------------------------
			</pre>

			<p>Va rugam sa dati click pe linkul urmator pentru a va activa contul:</p>

			<a href=\"" . BASE_HREF . "activate.php?username={$user->username}&activation_code={$user->activation_code}\" style=\"color: #582f06; font-family: verdana; font-size: 10pt;\">
			" . BASE_HREF . "activate.php?username={$user->username}&activation_code={$user->activation_code}
			</a>
			</div>
			";

		$this->AltBody = "
			Buna ziua {$user->username},

			Bine ati venit la " . APP_NAME . ", va multumim pentru ca v-ati inregistrat.

			----------------------------
			Username: {$user->username}
			Parola: $escPass
			Cod de activare: {$user->activation_code}
			----------------------------

			Va rugam sa dati click pe linkul urmator pentru a va activa contul:

			" . BASE_HREF . "activate.php?username={$user->username}&activation_code={$user->activation_code}
			";
		Logger::vardump($this->AltBody, "mail body from mailagent->registrationMail()", __FILE__, __LINE__);
		return $this->Send();
	}

	/**
	 * @param User $user
	 */
	function forgotPasswordMail($user, $pass)
	{
		$escPass = Escape::html($pass);
		$this->AddAddress($user->email);
		$this->AddReplyTo($this->From, $this->FromName);
		$this->Subject = APP_NAME . " - parola schimbata";

		$shortActCode = "Cod de activare: {$user->activation_code}";
		$htmlActCode = "";
		$textActCode = "";
		if(!$user->wasActivated())
		{
			$shortActCode = "";

			$htmlActCode = "
			<p>Va rugam sa dati click pe linkulu urmator pentru a va activa contul:</p>

			<a href=\"" . BASE_HREF . "activate.php?username={$user->username}&activation_code={$user->activation_code}\" style=\"color: #582f06; font-family: verdana; font-size: 10pt;\">
			" . BASE_HREF . "activate.php?username={$user->username}&activation_code={$user->activation_code}
			</a>";

			$textActCode = "
			Va rugam sa dati click pe linkul urmator pentru a va activa contul:

			" . BASE_HREF . "activate.php?username={$user->username}&activation_code={$user->activation_code}
			";
		}

		$this->Body = "
			<div style=\"padding: 10px; background-color: #fffaf4; color: #582f06; font-family: verdana; font-size: 10pt;\">
			<h1 style=\"font-size: 10pt; border: 1px solid #a17f60;	padding: 5px; \">Buna ziua {$user->username},</h1>

			<p>Parola dumneavoastra a fost schimbata.</p>
			<pre>
			----------------------------
			Username: {$user->username}
			Parola: $escPass
			$shortActCode
			----------------------------
			</pre>

			<p>Acest email este trimis automat deoarece a fost folosit formularul
			'parola uitata' de pe siteul nostru pentru a schimba parola. Puteti gasi acest formular aici:</p>
			<a href=\"" . BASE_HREF . "forgot_password.php\">" . BASE_HREF . "forgot_password.php</a>

			<p>Puteti sa dati click acum pe acest link pentru a va loga folosind noua parola generata:</p>
			<a href=\"" . BASE_HREF . "login.php\">" . BASE_HREF . "login.php</a>

			<p>Dupa ce va logati va puteti schimba preferintele din profil aici:</p>
			<a href=\"" . BASE_HREF . "profile_view.php\">" . BASE_HREF . "profile_view.php</a>

			$htmlActCode
			</div>
			";

		$this->AltBody = "
			Buna ziua {$user->username},

			Parola dumneavoastra a fost schimbata

			----------------------------
			Username: {$user->username}
			Parola: $escPass
			$shortActCode
			----------------------------

			Acest mail este trimis automat deoarece a fost folosit formularul 'parola uitata'
			de pe siteul nostru. Puteti vedea acest formular aici:
			" . BASE_HREF . "forgot_password.php

			Puteti da click acum pe acest link pentru a va loga folosind noua parola generata:
			" . BASE_HREF . "login.php

			Duap ce va logati puteti schimba preferintele din profil aici:
			" . BASE_HREF . "profile_view.php

			$textActCode
			";

		Logger::vardump($this->AltBody, "mail body from mailagent->forgotPasswordMail()", __FILE__, __LINE__);
		return $this->Send();
	}

	/**
	 * @param User $user
	 * @param Order $order
	 */
	function newOrder($user, $order)
	{
		$currency = Application::getConfigValue("default_currency");
		$this->AddAddress($user->email);
		$this->AddCC($this->From, $this->FromName);
		$this->AddReplyTo($this->From, $this->FromName);
		$this->Subject = APP_NAME . " - comanda noua";

		$s = "
			<h1>" . Escape::html(APP_NAME) . " Comanda noua!</h1>

			<p>Utilizatorul " . Escape::html($user->username) . " (" . Escape::html($user->first_name . " " . $user->last_name) . ")
			a trimis o noua comanda cu valoarea totala {$order->value_total} $currency. Produsele comandate sunt:
			";
		for($i = 0; $i < $order->getProductsCount(); $i++)
		{
			if($i > 0)
				$s .= ", ";
			$op = $order->getOrderProduct($i + 1);
			$s .= $op->quantity . " " . Escape::html($op->measuring_unit) . " " . Escape::html($op->product_name);
		}
		$s .= ".</p>
			<p>Factura proforma este atasata.</p>
			";
		$this->Body = $s;

		$s = "
			" . APP_NAME . " Comanda noua!

			Utilizatorul " . $user->username . " (" . $user->first_name . " " . $user->last_name . ")
			a trimis o noua comanda cu valoarea totala " . displayPrice($order->value_total) . " $currency. Produsele comandate sunt:
			";
		for($i = 0; $i < $order->getProductsCount(); $i++)
		{
			if($i > 0)
				$s .= ", ";
			$op = $order->getOrderProduct($i + 1);
			$s .= $op->quantity . " " . $op->measuring_unit . " " . $op->product_name;
		}
		$s .= ".
			Atasata este factura proforma.";
		$this->AltBody = $s;

		$filename = DATA_DIR . "/orders/{$order->code}.html";
		if(!is_file($filename))
		{
			Logger::err("could not find proforma file to attach to mail", __FILE__, __LINE__);
		}
		else
		{
			$this->AddAttachment($filename, $order->code . ".html", "base64", "text/html");
		}

		Logger::vardump($this->AltBody, "mail body from mailagent->newOrder()", __FILE__, __LINE__);
		return $this->Send();
	}

	function orderStatusChanged($id_order, $newStatus)
	{
		$db = Application::getDb();
		$order = $db->tbOrder->getRecordById($id_order);
		$u = $db->tbUser->getUserById($order->id_user);
		$u = Factory::instantiateUser($u);


		$currency = Application::getConfigValue("default_currency");
		$this->AddAddress($u->email);
		$this->AddCC($this->From, $this->FromName);
		$this->AddReplyTo($this->From, $this->FromName);
		$this->Subject = APP_NAME . " - statusul comenzii a fost schimbat";

		$this->Body = "
			<h1>" . Escape::html(APP_NAME) . " Statusul comenzii a fost schimbat!</h1>

			<p>Statusul comenzii {$order->code} trimisa de utilizatorul " . Escape::html($u->username) . " (" . Escape::html($u->first_name . " " . $u->last_name) . ") a fost schimbat.
			Statusul vechi era '" . Escape::html($order->status) . "', iar acum noul status este '" . Escape::html($newStatus) . "'.</p>

			<p>Factura proforma este atasata la acest mail.</p>
			";

		$this->AltBody = "
			" . APP_NAME . " statusul comenzii a fost schimbat!

			Statusul comenzii {$order->code} trimisa de utilizatorul " . $u->username . " (" . $u->first_name . " " . $u->last_name . ") a fost schimbat.
			Vechiul status era '" . $order->status . "', iar acum noul status este '" . $newStatus . "'.

			Factura proforma este atasata.
			";

		$filename = DATA_DIR . "/orders/{$order->code}.html";
		if(!is_file($filename))
		{
			Logger::err("could not find proforma file to attach to mail", __FILE__, __LINE__);
		}
		else
		{
			$this->AddAttachment($filename, $order->code . ".html", "base64", "text/html");
		}

		Logger::vardump($this->AltBody, "mail body from mailagent->orderStatusChanged()", __FILE__, __LINE__);
		return $this->Send();
	}

	/**
	 * @param ContactMessage $m
	 */
	function contactMessage($m)
	{
		$this->AddAddress($this->From, $this->FromName);
		$this->AddReplyTo($this->From, $this->FromName);
		$this->Subject = APP_NAME . " - mesaj de contact";

		$this->Body = '
			<h1>' . Escape::html(APP_NAME) . ' - mesaj de contact</h1>
			<p>Vizitator: ' . Escape::html($m->sender_name) . ' (<a href="mailto:' . Escape::htmlValue($m->sender_email) . '">' . Escape::html($m->sender_email) . '</a></p>
			<p>Subiect: <b>' . Escape::html($m->subject) . '</b></p>
			<p>Mesaj: <br><br>' . Escape::html($m->message) . '</p>
			<br>
			<br>
			<h2>Detalii utilizator</h2>
			<p>' . Escape::html($m->user_details) . '</p>
			';

		$this->AltBody = '
			' . APP_NAME . ' - contact message
			Vizitator: ' . $m->sender_name . ' (' . $m->sender_email . ')
			Subiect: ' . $m->subject . '
			Mesaj:
			' . $m->message . '

			Detalii utilizator
			' . ($m->user_details) . '
			';

		Logger::vardump($this->AltBody, "mail body from mailagent->contactMessage()", __FILE__, __LINE__);
		return $this->Send();
	}


}


?>
Return current item: Katyshop