This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/core/email.inc.php
2011-05-03 09:49:01 +10:00

218 lines
6.1 KiB
PHP

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @subpackage Core
*/
/**
* The main AgileBill CORE Mail Class
*
* This class handles the interface to SMTP and Mail() functions.
*
* <code>
* $arr = array(
* 'from_html' => 'true/false' (so we know whether to stripslashes or not)
* 'html' => '0/1',
* 'from_name' => '',
* 'from_email' => '',
* 'priority' => '0/1',
* 'to_email' => 'email@email.com',
* 'to_name' => '',
* 'bcc_list' => array('email@email.com'),
* 'cc_list' => array('email@email.com'),
* 'subject' => '',
* 'body_text' => '',
* 'body_html' => '',
* 'attachments' => array(array('file' => 'file.exe',
* 'data' => 'file data here...'))
* 'server' => 'mail.domain.com',
* 'account' => '',
* 'password' => '');
* </code>
*
* @package AgileBill
* @subpackage Core
*/
class CORE_email {
var $debug=false;
public function PHP_Mail($arr) {
# SET THE SMTP SETTINGS
#ini_set('sendmail_from',@$arr['from_email']);
#ini_set('SMTP',@$arr['server']);
# CC LIST
$cc = '';
if (isset($arr['cc_list']) && is_array($arr['cc_list']))
$cc = implode(',',$arr['cc_list']);
# BCC LIST
$bcc = '';
if (isset($arr['bcc_list']) && is_array($arr['bcc_list']))
$bcc = implode(',',$arr['bcc_list']);
$headers = '';
# FROM:
$headers .= sprintf('From: "%s" <%s>',$arr['from_name'],$arr['from_email'])."\r\n";
$headers .= sprintf('Reply-To: "%s" <%s>',$arr['from_name'],$arr['from_email'])."\r\n";
# HTML/non-HTML version of body & headers
$headers .= "MIME-Version: 1.0\r\n";
if (isset($arr['html']) && $arr['html'] == '1' && isset($arr['body_html'])) {
# Specify MIME version 1.0
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$body = $arr['body_html'];
} else {
# Specify MIME version 1.0
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$body = $arr['body_text'];
}
# CC:
if (trim($cc))
$headers .= sprintf('Cc: %s',$cc)."\r\n";
# BCC:
if (trim($bcc))
$headers .= sprintf('Bcc: %s',$bcc)."\r\n";
### PRIORITY
if(isset($arr['priority']) && $arr['priority'] == '1')
$headers .= "X-Priority: 1\r\n";
else
$headers .= "X-Priority: 3\r\n";
# Strip Slashes
if (! isset($arr['from_html']) || @$arr['html_form'] == false) {
# From database, we must strip slashes
$arr['subject'] = stripslashes($arr['subject']);
$body = stripslashes($body);
} elseif (@$arr['from_html'] == true && get_magic_quotes_gpc()) {
# Straight from html, we must strip slashes
$arr['subject'] = stripslashes($arr['subject']);
$body = stripslashes($body);
}
if ($this->debug) {
if (mail($arr['to_email'],$arr['subject'],$body,$headers)) {
global $C_debug;
$C_debug->alert(__FILE__,__METHOD__,sprintf('PHP mail() failed to send message "%s" to "%s"',$arr['subject'],$arr['to_email']));
return false;
}
} else {
if (@mail($arr['to_email'],$arr['subject'],$body,$headers)) {
global $C_debug;
$C_debug->alert(__FILE__,__METHOD__,sprintf('PHP mail() failed to send message "%s" to "%s"',$arr['subject'],$arr['to_email']));
return false;
}
}
return true;
}
public function SMTP_Mail($arr) {
# Include the phpmailer class
require_once(PATH_INCLUDES.'phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = @$arr['server'];
$mail->Username = @$arr['account'];
$mail->Password = @$arr['password'];
$mail->From = $arr['from_email'];
$mail->FromName = $arr['from_name'];
$mail->AddAddress($arr['to_email'],@$arr['to_name']);
#$mail->AddReplyTo($arr['from_name'], $arr['from_email']);
# CC LIST
if (isset($arr['cc_list']) && is_array($arr['cc_list']))
foreach ($arr['cc_list'] as $email)
$mail->AddCC($email,'');
# BCC LIST
if (isset($arr['bcc_list']) && is_array($arr['bcc_list']))
foreach ($arr['bcc_list'] as $email)
$mail->AddBCC($email,'');
# Strip Slashes
if (! isset($arr['from_html']) || @$arr['html_form'] == false) {
# From database, we must strip slashes
$arr['subject'] = stripslashes($arr['subject']);
@$arr['body_html'] = stripslashes($arr['body_html']);
@$arr['body_text'] = stripslashes($arr['body_text']);
} elseif (@$arr['from_html'] == true && get_magic_quotes_gpc()) {
# Straight from html, we must strip slashes
$arr['subject'] = stripslashes($arr['subject']);
@$arr['body_html'] = stripslashes($arr['body_html']);
@$arr['body_text'] = stripslashes($arr['body_text']);
}
# HTML/non-HTML version of body & headers
if (isset($arr['html']) && $arr['html'] == '1' && isset($arr['body_html'])) {
$mail->IsHTML(true);
$mail->Body = @$arr['body_html'];
$mail->AltBody = @$arr['body_text'];
} else {
$mail->IsHTML(false);
$mail->Body = @$arr['body_text'];
$mail->WordWrap = 50;
}
# Subject
$mail->Subject = $arr['subject'];
# PRIORITY
if(isset($arr['priority']) && $arr['priority'] == '1')
$mail->Priority = 1;
else
$mail->Priority = 3;
/* Attachments
$mail->AddAttachment("/var/tmp/file.tar.gz");
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
*/
if (! $mail->Send()) {
global $C_debug;
$C_debug->error(__FILE__,__METHOD__,sprintf('SMTP mail() failed to send message "%s" to "%s" on server "%s" (%s)',
$arr['subject'],$arr['to_email'],$arr['server'],$mail->ErrorInfo));
if ($this->debug) {
echo 'Message was not sent <p>';
printf('Mailer Error: %s',$mail->ErrorInfo);
}
return false;
}
return true;
}
}
?>