<?php
/**
 * @package linea21.core
 * @subpackage mail
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

// necessary for loops
if(!in_array(SITE_PATH.'/class/system/class.phpmailer.php', get_included_files()))
	include_once("../class/system/class.phpmailer.php");

$mail = new phpmailer();

if(defined('MAIL_PORT')) $mail->Port = MAIL_PORT;

if (defined('MAIL_MAILER')) {
	$mail->Mailer = MAIL_MAILER;
	if(MAIL_MAILER=='sendmail') {
		if(defined('MAIL_SENDMAIL')) $mail->Sendmail = MAIL_SENDMAIL;
	}
	if(MAIL_MAILER=='smtp') {
		if(defined('MAIL_HOST')) $mail->Host = MAIL_HOST;

	}
}

if (defined('MAIL_SMTP_AUTH') && MAIL_SMTP_AUTH == true) {
	$mail->SMTPAuth = MAIL_SMTP_AUTH;
	if (defined('MAIL_SMTP_USER')) $mail->Username = MAIL_SMTP_USER;
	if (defined('MAIL_SMTP_PASS')) $mail->Password = MAIL_SMTP_PASS;
}

/**
 echo "destinataire : ".$email_recipient."<br />";
 echo "sujet : ".$email_subject."<br />";
 echo "body : ".$email_html_body."<br />";
 echo "methode : ".$email_method."<br />";
 echo "host : ".$mail->Host."<br />";
 echo "port : ".$mail->Port."<br />";
 */
$mail->SetLanguage('en');
$mail->CharSet = CHARSET;
isset($email_from) ? $mail->From = $email_from : $mail->From = MAIL_FROM;
isset($email_fromname) ? $mail->FromName = $email_fromname : $mail->FromName = MAIL_FROMNAME;
$mail->Subject = $email_subject;
$mail->WordWrap = 75;
$mail->Body = $email_html_body;
$mail->AltBody = str_replace('&amp;', '&', $email_text_body);
$mail->AddReplyTo(MAIL_REPLY, MAIL_REPLYNAME);

if(is_array($email_recipient)) {
	
	for($i=0; $i<count($email_recipient); $i++) {

		$mail->AddAddress($email_recipient[$i]);
		$r = $mail->Send();
		logfile(LOG_MAILER, array($mail->Subject, $email_recipient, $mail->ErrorInfo));
		if(!$r && MOD_DEBUG == 1) {
			_debug('<b>Mail has not been sent</b>. mail->ErrorInfo : ' . $mail->ErrorInfo);
		}

		$mail->ClearAddresses();
		$mail->ClearAttachments();

	}
} else {

	$mail->AddAddress($email_recipient);
	$r = $mail->Send();
	logfile(LOG_MAILER, array($mail->Subject, $email_recipient, $mail->ErrorInfo));
	if(!$r && MOD_DEBUG == 1) {
		_debug('<b>Mail has not been sent</b>. mail->ErrorInfo : ' . $mail->ErrorInfo);
	}
	$mail->ClearAddresses();
	$mail->ClearAttachments();

}

?>