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/email_template/email_template.inc.php

330 lines
9.8 KiB
PHP
Raw Normal View History

<?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
2009-08-03 04:10:16 +00:00
*
* 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
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Module:Mail
*/
/**
2009-08-03 04:10:16 +00:00
* The main AgileBill Mail Template Class
*
* include_once(PATH_MODULES.'email_template/email_template.inc.php');
* $my = new email_template;
2009-08-03 04:10:16 +00:00
* $my->send('newsletter_subscribe','4d1800b401f5d340f022688de0ac2687','f1714072da3c05a220ac3b60a3a57d88','2','3');
*
* @package AgileBill
* @subpackage Module:Mail
*/
2009-08-03 04:10:16 +00:00
class email_template extends OSB_module {
/**
* Send or Queue template based email
*
* Prefixing the template with "admin->" will result in the mail being
* sent to the admin only.
*
* @param string $template_name Name of template to use
* @param int $acct Account ID or email address
*/
public function send($template_name,$acct,$sql1,$sql2,$sql3,$queue=true) {
global $VAR,$C_debug,$C_list;
$db = &DB();
2009-08-03 04:10:16 +00:00
# Send to admin only?
$admin_only = false;
2009-08-03 04:10:16 +00:00
if (preg_match('/^admin->/',$template_name)) {
$admin_only = true;
2009-08-03 04:10:16 +00:00
$template_name = preg_replace('/^admin->/','',$template_name);
}
2009-08-03 04:10:16 +00:00
$template = $db->Execute(sqlselect($db,'email_template','*',array('name'=>$template_name)));
2009-08-03 04:10:16 +00:00
# If the template is not active, return.
if (! $template || $template->fields['status'] != '1')
return;
2009-08-03 04:10:16 +00:00
# Setup our Email
$E = array();
2009-08-03 04:10:16 +00:00
# Email Priority
$E['priority'] = $template->fields['priority'];
2009-08-03 04:10:16 +00:00
# Get the setup_email settings
if (empty($template->fields['setup_email_id']))
$setup_email_id = DEFAULT_SETUP_EMAIL;
else
2009-08-03 04:10:16 +00:00
$setup_email_id = $template->fields['setup_email_id'];
2009-08-03 04:10:16 +00:00
$setup_email = $db->Execute(sqlSelect($db,'setup_email','*',array('id'=>$setup_email_id)));
2009-08-03 04:10:16 +00:00
# Queue the email
if ($queue && $C_list->is_installed('email_queue') && $setup_email->fields['queue']) {
# Set sql vars
2009-08-03 04:10:16 +00:00
if (is_array($sql1))
$sql1 = serialize($sql1);
if (is_array($sql2))
$sql2 = serialize($sql2);
if (is_array($sql3))
$sql3 = serialize($sql3);
if (is_array($VAR))
$var = serialize($VAR);
# If this was an admin only email, we need to rewrite the template name again.
if ($admin_only)
$sql_template = sprintf('admin->%s',$template->fields['name']);
else
$sql_template = $template->fields['name'];
2009-08-03 04:10:16 +00:00
# Check that this email is not already in the queue
$duplicates = $db->Execute(sqlSelect($db,'email_queue','id',
array('status'=>0,'account_id'=>$acct,'email_template'=>$sql_template,'sql1'=>$sql1,'sql2'=>$sql2,'sql3'=>$sql3)
));
if ($duplicates != false && $duplicates->RecordCount() > 0)
return;
# Queue this Email
$db->Execute(sqlInsert($db,'email_queue',array(
'date_orig'=>time(),
'date_last'=>time(),
'status'=>0,
'account_id'=>$acct,
'email_template'=>$sql_template,
'sql1'=>$sql1,
'sql2'=>$sql2,
'sql3'=>$sql3,
'var'=>$var)
));
return;
}
2009-08-03 04:10:16 +00:00
# Is this an SMTP connection
if ($setup_email->fields['type'] == 1) {
$E['server'] = $setup_email->fields['server'];
$E['account'] = $setup_email->fields['username'];
$E['password'] = $setup_email->fields['password'];
}
2009-08-03 04:10:16 +00:00
$E['from_name'] = $setup_email->fields['from_name'];
$E['from_email'] = $setup_email->fields['from_email'];
2009-08-03 04:10:16 +00:00
if ($setup_email->fields['cc_list'])
$E['cc_list'] = explode(',',$setup_email->fields['cc_list']);
2009-08-03 04:10:16 +00:00
if ($setup_email->fields['bcc_list'])
$E['bcc_list'] = explode(',',$setup_email->fields['bcc_list']);
2009-08-03 04:10:16 +00:00
# Get the account settings
$account = $db->Execute($q=sqlSelect($db,'account','*',sprintf('(email=::%s:: OR id=::%s::)',$acct,$acct)));
if (! $account) {
$C_debug->error(__FILE__,__METHOD__,sprintf('%s %s',$db->ErrorMsg(),$q));
return false;
}
2009-08-03 04:10:16 +00:00
if ($admin_only) {
$E['to_email'] = $setup_email->fields['from_email'];
$E['to_name'] = $setup_email->fields['from_name'];
$ab_account = true;
} else {
if ($account->RecordCount() > 0) {
$E['to_email'] = $account->fields['email'];
$E['to_name'] = sprintf('%s %s',$account->fields['first_name'],$account->fields['last_name']);
$ab_account = true;
} else {
2009-08-03 04:10:16 +00:00
$E['to_email'] = $acct;
$E['to_name'] = $acct;
$ab_account = false;
}
}
2009-08-03 04:10:16 +00:00
# Get the template translation for the specified account for text/htm
if ($ab_account && $account->fields['language_id'])
$language_id = $account->fields['language_id'];
else
$language_id = DEFAULT_LANGUAGE;
2009-08-03 04:10:16 +00:00
$email_template_translate = $db->Execute(sqlSelect($db,'email_template_translate','*',array('language_id'=>$language_id,'email_template_id'=>$template->fields['id'])));
2009-08-03 04:10:16 +00:00
# If there is no translation, get the default translation for this email
if (! $email_template_translate || ! $email_template_translate->RecordCount())
$email_template_translate = $db->Execute(sqlSelect($db,'email_template_translate','*',array('language_id'=>DEFAULT_LANGUAGE,'email_template_id'=>$template->fields['id'])));
2009-08-03 04:10:16 +00:00
# Unable to locate translation?
if (! $email_template_translate || ! $email_template_translate->RecordCount()) {
global $C_debug;
2009-08-03 04:10:16 +00:00
$C_debug->error(__FILE__,__METHOD__,
sprintf('Unable to locate translation for Email Template %s and Language %s OR %s',$template->fields['name'],$language_id,DEFAULT_LANGUAGE));
return;
}
2009-08-03 04:10:16 +00:00
# Set the subject:
$E['subject'] = $email_template_translate->fields['subject'];
2009-08-03 04:10:16 +00:00
# Determine whether to send HTML or not...
if ($ab_account && $account->fields['email_type'] == 1) {
if (! empty($email_template_translate->fields['message_html'])) {
$E['body_html'] = $email_template_translate->fields['message_html'];
$E['html'] = '1';
} else {
2009-08-03 04:10:16 +00:00
$E['body_html'] = false;
$E['html'] = '0';
}
2009-08-03 04:10:16 +00:00
} else {
2009-08-03 04:10:16 +00:00
$E['html'] = '0';
}
2009-08-03 04:10:16 +00:00
$E['body_text'] = $email_template_translate->fields['message_text'];
2009-08-03 04:10:16 +00:00
# Get the date-time
include_once(PATH_CORE.'list.inc.php');
2009-08-03 04:10:16 +00:00
$cl = new CORE_list;
$date = $cl->date_time(time());
2009-08-03 04:10:16 +00:00
# Url formatting.
if ($admin_only) {
$site_url = URL.'admin.php';
$site_ssl_url = SSL_URL.'admin.php';
2009-08-03 04:10:16 +00:00
} else {
$site_url = URL;
2009-08-03 04:10:16 +00:00
$site_ssl_url = SSL_URL;
}
2009-08-03 04:10:16 +00:00
# Get the replace vars from the email template:
$replace = array(
'%site_name%' => $E['from_name'],
'%site_email%' => $E['from_email'],
'%url%' => $site_url,
'%date%' => $date,
'%ssl_url%' => $site_ssl_url);
# Include the replace vars from the $VAR variable:
foreach ($VAR as $key => $value) {
$re_this = sprintf('%%var_%s%%',$key);
$replace[$re_this] = $value;
}
2009-08-03 04:10:16 +00:00
# Get the replace vars from the account
$replace['%acct_id%'] = $acct;
2009-08-03 04:10:16 +00:00
if ($ab_account)
foreach ($account->fields as $key => $value) {
$re_this = sprintf('%%acct_%s%%',$key);
$replace[$re_this] = $value;
}
2009-08-03 04:10:16 +00:00
# Get the SQL1/2/3 Query/Arrays
foreach (array(1,2,3) as $i)
if ($template->fields['sql_'.$i] && $sql1 && ! is_array($sql1)) {
# Set our DB prefix
$sql = str_replace('%DB_PREFIX%',AGILE_DB_PREFIX,$template->fields['sql_'.$i]);
# Set the SQL
$sql = str_replace('%SQL1%',$db->qstr($sql1),$sql);
if ($sql2 && ! is_array($sql2))
$sql = str_replace('%SQL2%',$db->qstr($sql2),$sql);
if ($sql3 && ! is_array($sql3))
$sql = str_replace('%SQL3%',$db->qstr($sql3),$sql);
$sql .= sprintf(' AND site_id=%s',DEFAULT_SITE);
$SQL = $db->Execute($sql);
if (! $SQL) {
# Return the error message
global $C_debug;
$C_debug->error(__FILE__,__METHOD__,sprintf('%s %s',$db->ErrorMsg(),$sql));
} elseif ($SQL->RecordCount() > 0) {
# Get the replace vars from the sql results:
foreach ($SQL->fields as $key => $value) {
$re_this = sprintf('%%sql%s_%s%%',$i,$key);
$replace[$re_this] = $value;
}
}
}
2009-08-03 04:10:16 +00:00
if (is_array($sql1)) {
echo '<PRE>1a.HOW DID I GET HERE??? (WHAT SHOULD THIS DO??)';debug_print_backtrace();die();
while(list($key,$value) = each($sql1))
$replace[$key] = $value;
2009-08-03 04:10:16 +00:00
} elseif ($sql1 && ! $template->fields['sql_1']) {
$replace['%sql1%'] = $sql1;
}
2009-08-03 04:10:16 +00:00
if (is_array($sql2)) {
echo '<PRE>2a.HOW DID I GET HERE??? (WHAT SHOULD THIS DO??)';debug_print_backtrace();die();
while(list($key,$value) = each($sql2))
$replace[$key] = $value;
2009-08-03 04:10:16 +00:00
} elseif ($sql2 && ! $template->fields['sql_2']) {
$replace['%sql2%'] = $sql2;
}
2009-08-03 04:10:16 +00:00
if (is_array($sql3)) {
echo '<PRE>3a.HOW DID I GET HERE??? (WHAT SHOULD THIS DO??)';debug_print_backtrace();die();
while(list($key,$value) = each($sql3))
$replace[$key] = $value;
} elseif ($sql3 && ! $template->fields['sql_3']) {
$replace['%sql3%'] = $sql3;
}
2009-08-03 04:10:16 +00:00
foreach (array('subject','body_text','body_html') as $i)
if (! empty($E[$i])) {
# Replace the $replace vars in the body and subject
foreach ($replace as $key => $value)
$E[$i] = str_replace($key,$value,$E[$i]);
2009-08-03 04:10:16 +00:00
# Remove any unparsed vars from the body text and html:
if (preg_match('/%/',$E[$i]))
$E[$i] = preg_replace('/%[a-zA-Z0-9_]{1,}%/','',$E[$i]);
}
# Set any attachments (not currently supported)
$E['attatchments'] = '';
2009-08-03 04:10:16 +00:00
# Email log?
if ($C_list->is_installed('email_log')) {
include_once(PATH_MODULES.'email_log/email_log.inc.php');
2009-08-03 04:10:16 +00:00
$log = new email_log;
$log->add($acct,$E['subject'],$E['body_text'],$E['to_email'],false,$E['priority']);
}
2009-08-03 04:10:16 +00:00
# Call the mail class
require_once(PATH_CORE.'email.inc.php');
$email = new CORE_email;
2009-08-03 04:10:16 +00:00
if ($setup_email->fields['type'] == 1)
return $email->SMTP_Mail($E);
else
2009-08-03 04:10:16 +00:00
return $email->PHP_Mail($E);
}
}
2009-08-03 04:10:16 +00:00
?>