1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-07 16:15:22 +02:00

Tidy up and simplify emailer class, add generate_board_url function ... note changes to emailer setup! Example will be adm/index.php remind

git-svn-id: file:///svn/phpbb/trunk@3718 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Paul S. Owen 2003-03-25 14:19:23 +00:00
parent 3d43169b2a
commit b0545e7636
2 changed files with 78 additions and 100 deletions

View File

@ -19,52 +19,28 @@
* *
***************************************************************************/ ***************************************************************************/
//
// NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
//
// Bug fixes in 2.0.x should be ported to this, particularly the header changes
//
// NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
//
//
// The emailer class has support for attaching files, that isn't implemented
// in the 2.0 release but we can probable find some way of using it in a future
// release
//
class emailer class emailer
{ {
var $tpl_file; var $msg, $subject, $extra_headers, $address;
var $use_smtp; var $tpl_msg = array();
var $msg;
var $mimeOut;
var $arrPlaceHolders = array(); // an associative array that has the key = placeHolderName and val = placeHolderValue.
var $subject, $extra_headers, $address;
function emailer($use_smtp) function emailer()
{ {
$this->use_smtp = $use_smtp; $this->reset();
$this->tpl_file = NULL;
$this->address = NULL;
$this->msg = '';
$this->mimeOut = '';
} }
// Resets all the data (address, template file, etc etc to default // Resets all the data (address, template file, etc etc to default
function reset() function reset()
{ {
$this->tpl_file = '';
$this->address = ''; $this->address = '';
$this->msg = ''; $this->msg = '';
$this->memOut = '';
$this->vars = ''; $this->vars = '';
} }
// Sets an email address to send to // Sets an email address to send to
function email_address($address) function email_address($address)
{ {
$this->address = ''; $this->address = $address;
$this->address .= $address;
} }
// set up subject for mail // set up subject for mail
@ -83,62 +59,78 @@ class emailer
{ {
global $config, $phpbb_root_path; global $config, $phpbb_root_path;
if ($template_lang == '') if (trim($template_file) == '')
{
trigger_error('No template file set', E_USER_ERROR);
}
if (trim($template_lang) == '')
{ {
$template_lang = $config['default_lang']; $template_lang = $config['default_lang'];
} }
$this->tpl_file = $phpbb_root_path . 'language/' . $template_lang . '/email/' . $template_file . '.txt'; if (empty($this->tpl_msg[$template_lang . $template_file]))
if (!file_exists($this->tpl_file))
{ {
trigger_error('Could not find email template file ' . $template_file); $tpl_file = $phpbb_root_path . 'language/' . $template_lang . '/email/' . $template_file . '.txt';
if (!file_exists($tpl_file))
{
$tpl_file = $phpbb_root_path . 'language/' . $config['default_lang'] . '/email/' . $template_file . '.txt';
if (!file_exists($tpl_file))
{
trigger_error('Could not find email template file :: ' . $template_file, E_USER_ERROR);
}
}
if (!($fd = @fopen($tpl_file, 'r')))
{
trigger_error('Failed opening template file', E_USER_ERROR);
}
$this->tpl_msg[$template_lang . $template_file] = fread($fd, filesize($tpl_file));
fclose($fd);
} }
if (!$this->load_msg()) $this->msg = $this->tpl_msg[$template_lang . $template_file];
{
trigger_error('Could not load email template file ' . $template_file);
}
return true;
}
// Open the template file and read in the message
function load_msg()
{
if ($this->tpl_file == NULL)
{
trigger_error('No template file set');
}
if (!($fd = fopen($this->tpl_file, 'r')))
{
trigger_error('Failed opening template file');
}
$this->msg .= fread($fd, filesize($this->tpl_file));
fclose($fd);
return true; return true;
} }
// assign variables
function assign_vars($vars) function assign_vars($vars)
{ {
$this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars; $this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars;
} }
function parse_email() // Send the mail out to the recipients set previously in var $this->address
function send()
{ {
foreach ($this->vars as $key => $val) global $config, $phpEx, $phpbb_root_path;
if (empty($config['email_enable']))
{ {
$$key = $val; return false;
} }
// Escape all quotes, else the eval will fail. // Escape all quotes, else the eval will fail.
$this->msg = str_replace ("'", "\'", $this->msg); $this->msg = str_replace ("'", "\'", $this->msg);
$this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg); $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
// Set vars
foreach ($this->vars as $key => $val)
{
$$key = $val;
}
eval("\$this->msg = '$this->msg';"); eval("\$this->msg = '$this->msg';");
// Clear vars
foreach ($this->vars as $key => $val)
{
unset($$key);
}
// We now try and pull a subject from the email body ... if it exists, // We now try and pull a subject from the email body ... if it exists,
// do this here because the subject may contain a variable // do this here because the subject may contain a variable
$drop_header = ''; $drop_header = '';
@ -169,34 +161,18 @@ class emailer
} }
// Split up message into 76 chars as per RFC2045 // Split up message into 76 chars as per RFC2045
// $this->msg = chunk_split($this->msg); // $this->msg = chunk_split($this->msg);
return true; // Build header
} $this->extra_headers = "From: " . $config['board_email'] . "\nReturn-Path: " . $config['board_email'] . "\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 7bit\nDate: " . gmdate('D, d M Y H:i:s', time()) . " UT\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\n" . trim($this->extra_headers);
// Send the mail out to the recipients set previously in var $this->address // Send message
function send() $result = ($config['smtp_delivery']) ? smtpmail($this->address, $this->subject, $this->msg, $this->extra_headers) : @mail($this->address, $this->subject, $this->msg, $this->extra_headers);
{
global $phpEx, $phpbb_root_path;
if ($this->address == NULL)
{
trigger_error('No email address set');
}
if (!$this->parse_email())
{
return false;
}
$universal_extra = "MIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . gmdate('D, d M Y H:i:s', time()) . " UT\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\n";
$this->extra_headers = $universal_extra . trim($this->extra_headers);
$result = ($this->use_smtp) ? smtpmail($this->address, $this->subject, $this->msg, $this->extra_headers) : @mail($this->address, $this->subject, $this->msg, $this->extra_headers);
if (!$result) if (!$result)
{ {
trigger_error('Failed sending email :: ' . $result); $message = '<u>EMAIL ERROR</u> [ ' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP') . ' ]<br /><br />' . $result . '<br /><br /><u>CALLING PAGE</u><br /><br />' . ((!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']) . '<br />';
trigger_error($message, E_USER_ERROR);
} }
return true; return true;
@ -207,17 +183,17 @@ class emailer
// using SMTP Extensions // using SMTP Extensions
function server_parse($socket, $response) function server_parse($socket, $response)
{ {
while (substr($server_response,3,1) != ' ') while (substr($server_response, 3, 1) != ' ')
{ {
if (!($server_response = fgets($socket, 256))) if (!($server_response = fgets($socket, 256)))
{ {
trigger_error('Could not get mail server response codes'); trigger_error('Could not get mail server response codes', E_USER_ERROR);
} }
} }
if (!(substr($server_response, 0, 3) == $response)) if (!(substr($server_response, 0, 3) == $response))
{ {
trigger_error("Ran into problems sending Mail. Response: $server_response"); trigger_error("Ran into problems sending Mail. Response: $server_response", E_USER_ERROR);
} }
} }
@ -245,7 +221,7 @@ function smtpmail($mail_to, $subject, $message, $headers = '')
$headers = chop($headers); $headers = chop($headers);
// Make sure there are no bare linefeeds in the headers // Make sure there are no bare linefeeds in the headers
$headers = preg_replace("#(?<!\r)\n#si", "\r\n", $headers); $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
// Ok this is rather confusing all things considered, // Ok this is rather confusing all things considered,
// but we have to grab bcc and cc headers and treat them differently // but we have to grab bcc and cc headers and treat them differently
@ -253,17 +229,17 @@ function smtpmail($mail_to, $subject, $message, $headers = '')
$header_array = explode("\r\n", $headers); $header_array = explode("\r\n", $headers);
@reset($header_array); @reset($header_array);
$headers = ""; $headers = '';
while(list(, $header) = each($header_array)) while(list(, $header) = each($header_array))
{ {
if (preg_match("#^cc:#si", $header)) if (preg_match('#^cc:#si', $header))
{ {
$cc = preg_replace("#^cc:(.*)#si", "\\1", $header); $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
} }
else if (preg_match("#^bcc:/si", $header)) else if (preg_match('#^bcc:/si', $header))
{ {
$bcc = preg_replace("#^bcc:(.*)#si", "\\1", $header); $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
$header = ""; $header = '';
} }
$headers .= $header . "\r\n"; $headers .= $header . "\r\n";
} }
@ -273,19 +249,14 @@ function smtpmail($mail_to, $subject, $message, $headers = '')
$bcc = explode(',', $bcc); $bcc = explode(',', $bcc);
} }
if (trim($mail_to) == '')
{
trigger_error('No email address specified');
}
if (trim($subject) == '') if (trim($subject) == '')
{ {
trigger_error('No email Subject specified'); trigger_error('No email Subject specified', E_USER_ERROR);
} }
if (trim($message) == '') if (trim($message) == '')
{ {
trigger_error('Email message was blank'); trigger_error('Email message was blank', E_USER_ERROR);
} }
$mail_to_array = explode(',', $mail_to); $mail_to_array = explode(',', $mail_to);
@ -294,7 +265,7 @@ function smtpmail($mail_to, $subject, $message, $headers = '')
// it already. // it already.
if (!$socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20)) if (!$socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20))
{ {
trigger_error("Could not connect to smtp host : $errno : $errstr"); trigger_error("Could not connect to smtp host : $errno : $errstr", E_USER_ERROR);
} }
// Wait for reply // Wait for reply

View File

@ -766,6 +766,13 @@ function obtain_icons(&$icons)
return; return;
} }
function generate_board_url()
{
global $config;
return (($config['cookie_secure']) ? 'https://' : 'http://') . trim($config['server_name']) . (($config['server_port'] <> 80) ? ':' . trim($config['server_port']) . '/' : '/') . preg_replace('/^\/?(.*?)\/?$/', '\1', trim($config['script_path']));
}
// Redirects the user to another page then exits the script nicely // Redirects the user to another page then exits the script nicely
function redirect($url) function redirect($url)
{ {