<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------

// 邮件模板模型
class MailTemplateModel extends CommonModel {
	//用于外部模块调用的邮件发送函数
	public function sendPasswordMail($user_id,$new_pwd)
	{
			if(sunnyiC("MAIL_ON")==1)
			{
				$mail_template = D("MailTemplate")->where("name='get_password'")->find();
				$user_info = D("User")->getById($user_id);
				if($user_info)
				{
					$username = $user_info['user_name'];
					if($user_info['nickname']!='')
					{
						$username .= "(".$user_info['nickname'].")";
					}
				}
				else 
				{
					return L("USERNAME_NOT_EXIST");
				}
				if($mail_template)
				{
							$mail = new Mail();	
							$mail->IsHTML($mail_template['is_html']); // 设置邮件格式为 HTML
							$mail_title = $mail_template['mail_title'];
							$mail_content = $mail_template['mail_content'];
							$mail_title = str_replace("{\$username}",$username,$mail_title);
							$mail_title = str_replace("{\$password}",$new_pwd,$mail_title);
							$mail_content = str_replace("{\$username}",$username,$mail_content);
							$mail_content = str_replace("{\$password}",$new_pwd,$mail_content);
							
							$mail->Subject = $mail_title; // 标题					
							$mail->Body =  $mail_content; // 内容
							$mail->AddAddress($user_info['email'],$username);	
							if(!$mail->Send())
							{
								return $mail->ErrorInfo;
							}
							else 
							{
								return L("PASSWORD_SEND_SUCCESS");
							}
				}
			}
			else 
			{
				return false;
			}
	}
	
	//用于外部模块调用的邮件发送函数
	public function sendOrderMail($template_name,$order_id)
	{
			if(sunnyiC("MAIL_ON")==1)
			{
				$mail_template = D("MailTemplate")->where("name='".$template_name."'")->find();
				$order_info = D("Order")->getById($order_id);
				$user_info = D("User")->getById($order_info['user_id']);
				if($user_info)
				{
					$username = $user_info['user_name'];
					if($user_info['nickname']!='')
					{
						$username .= "(".$user_info['nickname'].")";
					}
				}
				else 
				{
					$username = L("USERNAME_NOT_EXIST");
				}
				if($mail_template)
				{
							$mail = new Mail();	
							$mail->IsHTML($mail_template['is_html']); // 设置邮件格式为 HTML
							$mail_title = $mail_template['mail_title'];
							$mail_content = $mail_template['mail_content'];
							$mail_title = str_replace("{\$username}",$username,$mail_title);
							$mail_title = str_replace("{\$order_sn}",$order_info['sn'],$mail_title);
							$mail_content = str_replace("{\$username}",$username,$mail_content);
							$mail_content = str_replace("{\$order_sn}",$order_info['sn'],$mail_content);
							
							$mail->Subject = $mail_title; // 标题					
							$mail->Body =  $mail_content; // 内容
							$mail->AddAddress($order_info['email'],$username);	
							if(!$mail->Send())
							{
								return false;
							}
							else 
							{
								return true;
							}
				}
			}
			else 
			{
				return false;
			}
	}
}
?>