Skip to content
Snippets Groups Projects
Commit abbf0890 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

Added setBody() to Mailer class

This method makes it easy to send a HTML mail based on the default text
mails. This is probably enough for simpler mails where no sophisticated
HTML is needed except a bit of formatting and linked URLs.
parent 8a215f09
No related branches found
No related tags found
No related merge requests found
......@@ -121,10 +121,80 @@ class Mailer {
$this->sendparam = $param;
}
/**
* Set the text and HTML body and apply replacements
*
* This function applies a whole bunch of default replacements in addition
* to the ones specidifed as parameters
*
* If you pass the HTML part or HTML replacements yourself you have to make
* sure you encode all HTML special chars correctly
*
* @fixme the HTML head and body still needs to be set
* @param string $text plain text body
* @param array $textrep replacements to apply on the text part
* @param array $htmlrep replacements to apply on the HTML part, leave null to use $textrep
* @param array $html the HTML body, leave null to create it from $text
*/
public function setBody($text, $textrep=null, $htmlrep=null, $html=null){
global $INFO;
global $conf;
// create HTML from text if not given
if(is_null($html)){
$html = hsc($text);
$html = nl2br($text);
}
if(!is_null($textrep) && is_null($htmlrep)){
$htmlrep = array_map('hsc',$textrep);
}
// prepare default replacements
$ip = clientIP();
$trep = array(
'DATE' => dformat(),
'BROWSER' => $_SERVER['HTTP_USER_AGENT'],
'IPADDRESS' => $ip,
'HOSTNAME' => gethostsbyaddrs($ip),
'TITLE' => $conf['title'],
'DOKUWIKIURL' => DOKU_URL,
'USER' => $_SERVER['REMOTE_USER'],
'NAME' => $INFO['userinfo']['name'],
'MAIL' => $INFO['userinfo']['mail'],
);
$trep = array_merge($trep,(array) $textrep);
$hrep = array(
'DATE' => '<i>'.hsc(dformat()).'</i>',
'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']),
'IPADDRESS' => '<code>'.hsc($ip).'</code>',
'HOSTNAME' => '<code>'.hsc(gethostsbyaddrs($ip)).'</code>',
'TITLE' => hsc($conf['title']),
'DOKUWIKIURL' => '<a href="'.DOKU_URL.'">'.DOKU_URL.'</a>',
'USER' => hsc($_SERVER['REMOTE_USER']),
'NAME' => hsc($INFO['userinfo']['name']),
'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'.
hsc($INFO['userinfo']['mail']).'</a>',
);
$hrep = array_merge($hrep,(array) $htmlrep);
// Apply replacements
foreach ($trep as $key => $substitution) {
$text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
}
foreach ($hrep as $key => $substitution) {
$html = str_replace('@'.strtoupper($key).'@',$substitution, $html);
}
$this->setHTML($html);
$this->setText($text);
}
/**
* Set the HTML part of the mail
*
* Placeholders can be used to reference embedded attachments
*
* You probably want to use setBody() instead
*/
public function setHTML($html){
$this->html = $html;
......@@ -132,6 +202,8 @@ class Mailer {
/**
* Set the plain text part of the mail
*
* You probably want to use setBody() instead
*/
public function setText($text){
$this->text = $text;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment