91 $replyTo = $this->replyTo ?
"{$this->replyName} <{$this->replyTo}>" :
"{$this->emailName} <{$this->emailFrom}>";
93 $from = $this->emailName .
" <" . $this->emailFrom .
">";
96 $headers .=
"Reply-To: ".
$replyTo .
"\r\n";
97 $headers .=
"MIME-Version: 1.0\r\n";
99 $random_hash = md5(date(
'r', time()));
101 $headers .=
"Content-Type: multipart/alternative; boundary=\"--PHP-alt-".$random_hash.
"\"\r\n";
105 if (!$this->plainMessage) $this->plainMessage = HTMLToText($this->htmlMessage);
108 ----PHP-alt-{$random_hash}
109 Content-Type: text/plain
110 Content-Disposition:
inline
111 Content-Transfer-Encoding: 8bit
113 {$this->plainMessage}
116 ----PHP-alt-{$random_hash}
117 Content-Type: text/html
118 Content-Disposition:
inline
119 Content-Transfer-Encoding: 8bit
126 $message .=
"----PHP-alt-{$random_hash}\r\n";
133 ----PHP-alt-{$random_hash}
134 Content-Type: text/calendar; charset=utf-8;method={$this->icalMethod}
135 Content-Disposition:
inline;
filename=meeting.ics
136 Content-Transfer-Encoding: 8bit
141 trace(
"Sending Mail from {$from} to {$this->to}: {$this->subject}", 3);
144 $rtn = $this->mail->send();
151 $fileAttachments =
"";
156 $fileAttachments .=
"----PHP-alt-{$random_hash}\n";
158 $fileContents = @fread($fp, filesize(
$attachment));
160 $fileContents = chunk_split(base64_encode($fileContents));
161 $fileAttachments .=
"Content-Type: application/octet-stream; name=\"". $base_filename .
"\"\n" .
162 "Content-Description: ".$base_filename.
"\n" .
163 "Content-Disposition: attachment;\n" .
" filename=\"". $base_filename .
"\"; size=".filesize(
$attachment).
";\n" .
164 "Content-Transfer-Encoding: base64\n\n" . $fileContents .
"\n\n";
167 return $fileAttachments;
195 $replyTo = $site_email_from;
196 $replyToName = $site_email_name;
200 $replyTo = $emailFrom;
205 $replyToName = $emailName;
208 $toAddr = trim($toAddr);
210 if (
$config[
'html_email_template'])
213 trace(
"Loading email template '$file'", 3);
214 $template = file_get_contents(
$file);
221 $this->transport->setTo($toAddr);
222 $this->transport->setFrom($site_email_from, $site_email_name);
223 $this->transport->setReplyTo($replyTo, $replyToName);
224 $this->transport->setReturnPAth($site_email_from);
225 $this->transport->setSubject(
$subject);
226 $this->transport->setHTMLMessage(
$message);
227 $this->transport->setPlainMessage(HTMLToText(
$message));
229 if($attachments && !is_array($attachments))
231 $attachments = array_combine(basename($attachments), $attachments);
241 $iCalMgr->setOrganizer($replyTo, $replyToName);
242 $iCalMgr->setAttendee($toAddr);
244 $this->transport->setICalAttachment($iCalMgr->format(), $iCalMgr->data->get(
"method"));
269 ----PHP-alt-<?php echo
"$random_hash\n"; ?>
270 Content-Type: text/plain
271 Content-Disposition:
inline
272 Content-Transfer-Encoding: 8bit
274 <?php echo trim(HTMLToText(
$msg)); ?>
277 ----PHP-alt-<?php echo
"$random_hash\n"; ?>
278 Content-Type: text/html
279 Content-Disposition:
inline
280 Content-Transfer-Encoding: 8bit
284 <?php echo $fileAttachments ?>
288 ----PHP-alt-<?php echo $random_hash; ?>--
304 if(!count($attachments))
return "";
306 foreach($attachments as $display_name =>
$attachment)
312 $fileAttachments .=
"----PHP-alt-{$random_hash}\n";
314 $fileContents = @fread($fp, filesize(
$attachment));
316 $fileContents = chunk_split(base64_encode($fileContents));
317 $fileAttachments .=
"Content-Type: application/octet-stream; name=\"". $base_filename .
"\"\n" .
318 "Content-Description: ".$base_filename.
"\n" .
319 "Content-Disposition: attachment;\n" .
" filename=\"".$display_name .
"\"; size=".filesize(
$attachment).
";\n" .
320 "Content-Transfer-Encoding: base64\n\n" .$fileContents .
"\n\n";
324 return $fileAttachments;
336 $method = $iCalMgr->data->get(
"method");
338 $cal_message =
"----PHP-alt-{$random_hash}\n";
339 $cal_message .=
"Content-Type: text/calendar; charset=utf-8;method={$method}\n";
340 $cal_message .=
"Content-Disposition: inline; filename=meeting.ics\n";
341 $cal_message .=
"Content-Transfer-Encoding: 8bit\n";
343 $cal_message .= $iCalMgr->format();
350 $rtn = $this->transport->send();
352 trace(
"EmailHandler send rtn $rtn", 3);
355 if ($this->onSendComplete AND is_callable($this->onSendComplete))
357 call_user_func($this->onSendComplete,
$rtn);
457 $form->dropdown(
"transport_mode",
"Selected Transport Mode",
$options);
473 $this->item = clone(
$item);
474 $this->item->filter =
null;
486 private function cleanupTemplateOutput(
$output)
488 trace(
"** Cleaning Up Email Template Output HTML", 3);
490 $output = preg_replace(
"/<p>\s*<\!DOCTYPE/s",
"<!DOCTYPE",
$output);
492 $output = preg_replace(
"/<\\/html>\\s*<\\/p>/s",
"</html>",
$output);
494 trace(
"Base URL: $baseURL", 3);
498 $output = str_replace(
"<head>",
"<head><base href='{$baseURL}'>",
$output);
510 $emailText = $this->emailTemplate->recipients . $this->emailTemplate->subject . $this->emailTemplate->message;
513 $fields = array(
"recipients",
"subject",
"message");
514 foreach($fields as
$field)
515 $this->$field =
$mgr->searchAndReplace($this->emailTemplate->$field);
522 $validRecipients = array();
524 trace(
"EmailManager:getRecipients {$this->recipients}", 3);
527 $recipients = explode(
",", $this->
recipients);
532 if(preg_match(
'/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $recipient))
533 $validRecipients[] = $recipient;
535 trace(
"EmailManager:getRecipients invalid recipient $recipient", 3);
540 return $validRecipients;
564 trace(
"EmailManager:: Warning - no valid recipients", 3);
571 trace(
"EmailManager:: sending email to recipient $recipient", 3);
577 if ($this->onSendComplete AND is_callable($this->onSendComplete))
579 call_user_func($this->onSendComplete, $this, $recipient,
$rtn);
601 $sendingItem =
new JoinResult();
605 $sendingItem->$cl =
$item;
613 trace(
"Sending email '$name'", 3);
615 $rtn = $emailManager->sendEmail();
616 trace(
"EmailManager:: rtn code $rtn", 3);
621 trace(
"EmailManager::no template found", 3);
628 trace(
"EmailManager::setDefaults", 3);
632 Settings::setDefaultValue(
"email",
"transport_mode",
"SMTP",
"String",
"Specifies the mail transport for system generated emails",
"Mail Transport",
null, 0);
634 Settings::setDefaultValue(
"email",
"use_debugging_mode",
false,
"Boolean",
"Enable local logging of generated emails (instead of sending via SMTP)",
"Debugging Mode",
null, 0);
636 $defaultPath =
$config[
'uploadbase'] . DIRECTORY_SEPARATOR .
"email_debug_logs";
638 if (!file_exists($defaultPath)) mkdir($defaultPath);
640 Settings::setDefaultValue(
"email",
"debugging_mode_output_path", $defaultPath,
"String",
"Folder that will store the logged emails in debugging mode",
"Debugging Mode",
null, 1);
641 Settings::setDefaultValue(
"email",
"email_from",
"andy@sonjara.com",
"String",
"Default From address for generated emails",
"Contact Details",
null, 1);
642 Settings::setDefaultValue(
"email",
"email_contact",
"andy@sonjara.com",
"String",
"Default To address for generated emails",
"Contact Details",
null, 2);
643 Settings::setDefaultValue(
"email",
"email_name",
"Andy Green",
"String",
"Default Name of email contact for generated emails",
"Contact Details",
null, 3);
644 Settings::setDefaultValue(
"email",
"default_contact_us_recipient",
"",
"String",
"Default email address(es) that will receive messages from the Contact Us form. These are used if no Contact Topics are defined",
"Contact Details",
null, 4);
645 Settings::setDefaultValue(
"email",
"use_captcha",
"Never", String,
"Specify when to use a Captcha to verify the user",
"Contact Details",
"Never\nAnonymous Only\nAlways", 5);
648 Settings::setDefaultValue(
"email",
"override_PHP_settings",
false, Boolean,
"Use these server settings instead of the global settings in your php.ini file",
"Server Settings", 0);
649 Settings::setDefaultValue(
"email",
"mail_server",
"localhost",
"String",
"Host name or IP address of your upstream SMTP server",
"Server Settings",
"", 1);
650 Settings::setDefaultValue(
"email",
"mail_server_port", 25, Number,
"Port to use on the upstream SMTP server",
"Server Settings",
"", 2);
651 Settings::setDefaultValue(
"email",
"username",
"", String,
"SMTP Account username (if authentication is required)",
"Server Settings",
"", 3);
652 Settings::setDefaultValue(
"email",
"password",
"", String,
"SMTP Account password (if authentication is required)",
"Server Settings",
"", 4);
654 Settings::setDefaultValue(
"email",
"log_messages",
false, Boolean,
"Log messages in the database after sending",
"Message Logging",
"", 1);
655 Settings::setDefaultValue(
"email",
"log_message_bodies",
false, Boolean,
"Specifies whether to store the message text when logging an email message. ".
656 "It is recommended to leave this unchecked to conserve space in the database.",
"Message Logging",
"", 2);
662 $mgr->upgrade($version);
675 trace(
"EmailManager::Scanning $path", 3);
677 $handle = opendir(
$path);
683 while(
false !== (
$file = readdir($handle)))
686 if (!preg_match(
"/(^\.{1,2}$)/i",
$file))
691 $log->date = date(
"F d, Y g:ia", (filemtime(
$path . DIRECTORY_SEPARATOR .
$file)));
726 trace(
"Sending email '$name'", 3);
728 $rtn = $emailManager->sendEmail();
729 trace(
"EmailManager:: rtn code $rtn", 3);
if(! $attachment_id) $attachment
setFrom($email, $name="")
setReplyTo($email, $name)
setPlainMessage($message)
setICalAttachment($ical, $method)
addAttachment($filename, $attachment)
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
formatiCalendar($iCalMgr, $random_hash)
The iCal event should come in formatted by the event handler formatICal function.
formatAttachments($attachments, $random_hash)
EmailHandler($toAddr, $subject, $message="", $emailFrom="", $emailName="", $attachments=array(), $iCalMgr=null)
getMessageScript($msg, $random_hash, $attachments, $iCalMgr)
static logEmail($mgr, $toAddr, $rtn)
Record email sent in a log.
Takes an email template and an obj of any DataItem class and sends email to a list of recipients afte...
mergeEmail()
Use MergeCodeManager, which shares functionality with TextLookup.
static getTransportModes()
static upgradeComponent($version)
static registerSettingsFormExtension()
$emailName
The name of the email sender (optional)
static settingsExtension($form)
$emailTemplateName
The name of the email template.
static send($source, $name, $onSendComplete="")
Sends an email template, based on the supplied DataItem (or array of DataItems)
static registerSerializationHandler()
static registerSMTPTransport()
EmailManager($item, $emailTemplate, $onSendComplete="")
static createTransport($mode=null)
$attachments
optional - array of files to attach to the email
static registerTransport($mode, $class)
Registers an email transport handler.
static getEmailTemplate($name)
Retrieves the named email templated.
static getStyles()
Returns the HTML link tags for CSS files specified by the registered components in their manifest fil...
static using()
Import the datamodels, views and manifest for the specified component(s).
writeAttachment($filename, $attachment)
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.
static getValue($component, $name)
Retrieve the value of the specified Setting.
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
static registerExtension($component, $handler)
Provides a simple implementation of a SerializationHandler that can serialize a single DataItem class...
$method
Pull out a simple reference to the request method.
sendEmailUsingEmailManager($sendingItem, $name, $onSendComplete="")
if(!Settings::getValue("debug", "enable_trace_file_downloads")) $filename