/** * Hook to handle emails send by elgg_send_email * * @param string $hook * @param string $type * @param bool $return * @param array $params * to => who to send the email to * from => who is the sender * subject => subject of the message * body => message * params => optional params */ function html_email_handler_email_hook($hook, $type, $return, $params) { // generate HTML mail body $html_message = html_email_handler_make_html_body($params["subject"], $params["body"]); // set options for sending $options = array("to" => $params["to"], "from" => $params["from"], "subject" => $params["subject"], "html_message" => $html_message, "plaintext_message" => $params["body"]); return html_email_handler_send_email($options); }
function html_email_handler_notification_handler(ElggEntity $from, ElggUser $to, $subject, $message, array $params = NULL) { if (!$from) { $msg = elgg_echo("NotificationException:MissingParameter", array("from")); throw new NotificationException($msg); } if (!$to) { $msg = elgg_echo("NotificationException:MissingParameter", array("to")); throw new NotificationException($msg); } if ($to->email == "") { $msg = elgg_echo("NotificationException:NoEmailAddress", array($to->guid)); throw new NotificationException($msg); } // To $to = html_email_handler_make_rfc822_address($to); // From $site = elgg_get_site_entity(); // If there's an email address, use it - but only if its not from a user. if (!$from instanceof ElggUser && !empty($from->email)) { $from = html_email_handler_make_rfc822_address($from); } elseif (!empty($site->email)) { // Use email address of current site if we cannot use sender's email $from = html_email_handler_make_rfc822_address($site); } else { // If all else fails, use the domain of the site. if (!empty($site->name)) { $name = $site->name; if (strstr($name, ',')) { $name = '"' . $name . '"'; // Protect the name with quotations if it contains a comma } $name = '=?UTF-8?B?' . base64_encode($name) . '?='; // Encode the name. If may content nos ASCII chars. $from = $name . " <noreply@" . get_site_domain($site->getGUID()) . ">"; } else { $from = "noreply@" . get_site_domain($site->getGUID()); } } // generate HTML mail body $html_message = html_email_handler_make_html_body($subject, $message); // set options for sending $options = array("to" => $to, "from" => $from, "subject" => '=?UTF-8?B?' . base64_encode($subject) . '?=', "html_message" => $html_message, "plaintext_message" => $message); if (!empty($params) && is_array($params)) { $options = array_merge($options, $params); } return html_email_handler_send_email($options); }
/** * Sends out a full HTML mail * * @param string $hook 'email' * @param string $type 'system' * @param array $return_value In the format: * to => STR|ARR of recipients in RFC-2822 format (http://www.faqs.org/rfcs/rfc2822.html) * from => STR of senden in RFC-2822 format (http://www.faqs.org/rfcs/rfc2822.html) * subject => STR with the subject of the message * body => STR with the message body * plaintext_message STR with the plaintext version of the message * html_message => STR with the HTML version of the message * cc => NULL|STR|ARR of CC recipients in RFC-2822 format (http://www.faqs.org/rfcs/rfc2822.html) * bcc => NULL|STR|ARR of BCC recipients in RFC-2822 format (http://www.faqs.org/rfcs/rfc2822.html) * date => NULL|UNIX timestamp with the date the message was created * attachments => NULL|ARR of array(array('mimetype', 'filename', 'content')) * @param array $params The unmodified core parameters * * @return void|bool */ public static function emailHandler($hook, $type, $return_value, $params) { static $plugin_setting; if (!isset($plugin_setting)) { $plugin_setting = false; // do we need to handle sending of mails? if (elgg_get_plugin_setting('notifications', 'html_email_handler') === 'yes') { $plugin_setting = true; } } if (!$plugin_setting) { return; } // if someone else handled sending they should return true|false if (empty($return_value) || !is_array($return_value)) { return; } $additional_params = elgg_extract('params', $return_value); if (is_array($additional_params)) { $return_value = array_merge($return_value, $additional_params); } return html_email_handler_send_email($return_value); }
/** * Sends the preview newsletter * * @param Newsletter $entity newsletter to be send * @param string $email emailaddress of the recipient * * @return bool if sending is succes or not */ function newsletter_send_preview(Newsletter $entity, $email) { $result = false; if (!empty($entity) && elgg_instanceof($entity, "object", Newsletter::SUBTYPE) && !empty($email)) { $container = $entity->getContainerEntity(); // build correct subject if ($entity->subject) { $message_subject = $entity->subject; } else { $message_subject = elgg_echo("newsletter:subject", array($container->name, $entity->title)); } // plaintext message $message_plaintext_content = elgg_echo("newsletter:plain_message", array($entity->getURL())); // html content $message_html_content = elgg_view_layout("newsletter", array("entity" => $entity)); // convert to inline CSS for email clients $message_html_content = html_email_handler_css_inliner($message_html_content); // add unsubscribe link $unsubscribe_link = newsletter_generate_unsubscribe_link($container, $email); $message_html_content = str_ireplace(urlencode("{unsublink}"), $unsubscribe_link, $message_html_content); // replace online link $online_link = $entity->getURL(); $new_online_link = $online_link . "?e=" . $email; $message_html_content = str_ireplace($online_link, $new_online_link, $message_html_content); // start creating sending options $send_options = array("from" => html_email_handler_make_rfc822_address($container), "subject" => $message_subject, "plaintext_message" => $message_plaintext_content, "to" => $email, "html_message" => $message_html_content); // send preview $result = html_email_handler_send_email($send_options); } return $result; }
/** * Send out the generated digest * * @param ElggUser $user * @param string $subject * @param string $html_body * @param string $plain_link * @param bool $bypass * @return boolean */ function digest_send_mail(ElggUser $user, $subject, $html_body, $plain_link = "") { global $digest_mail_send; $result = false; if (!empty($user) && elgg_instanceof($user, "user", null, "ElggUser") && !empty($subject) && !empty($html_body)) { // convert css if ($transform = html_email_handler_css_inliner($html_body)) { $html_body = $transform; } // email settings $to = html_email_handler_make_rfc822_address($user); if (!empty($plain_link)) { // make a plaintext message for non HTML users $plaintext_message .= elgg_echo("digest:mail:plaintext:description", array($plain_link)); } // send out the mail $options = array("to" => $to, "subject" => $subject, "html_message" => $html_body, "plaintext_message" => $plaintext_message); if (html_email_handler_send_email($options, $user->email)) { if (empty($digest_mail_send)) { $digest_mail_send = 1; } else { $digest_mail_send++; } $result = true; } } return $result; }
<?php /** * A test page for theme developer to view the layout of an email notification */ elgg_admin_gatekeeper(); $user = elgg_get_logged_in_user_entity(); $site = elgg_get_site_entity(); $subject = elgg_echo("useradd:subject"); $plain_message = elgg_echo("useradd:body", array($user->name, $site->name, $site->url, $user->username, 'test123')); $html_message = elgg_view("html_email_handler/notification/body", array("subject" => $subject, "body" => $plain_message, "recipient" => $user)); $html_message = html_email_handler_css_inliner($html_message); $html_message_ext = html_email_handler_normalize_urls($html_message); $html_message_ext = html_email_handler_base64_encode_images($html_message_ext); echo $html_message_ext; if (get_input("mail")) { // Test sending a basic HTML mail $options = array('to' => $user->email, 'subject' => $subject, 'body' => $plain_message, 'recipient' => $user, 'attachments' => array(array('filepath' => dirname(__DIR__) . '/manifest.xml'))); html_email_handler_send_email($options); // Test sending attachments through notify_user() $to = $user->guid; $from = $site->guid; $subject = 'Notification test'; $message = 'This notification has been sent using notify_user() and it should have an attachment.'; $params = array('recipient' => $user, 'attachments' => array(array('filepath' => dirname(__DIR__) . '/manifest.xml'))); notify_user($to, $from, $subject, $message, $params, array('email')); }
/** * Sends the preview newsletter * * @param Newsletter $entity newsletter to be send * @param string $email emailaddress of the recipient * * @return bool if sending is succes or not */ function newsletter_send_preview(Newsletter $entity, $email) { if (!elgg_instanceof($entity, "object", Newsletter::SUBTYPE) || empty($email)) { return false; } $container = $entity->getContainerEntity(); // build correct subject if ($entity->subject) { $message_subject = $entity->subject; } else { $message_subject = elgg_echo('newsletter:subject', [$container->name, $entity->title]); } // plaintext message $message_plaintext_content = elgg_echo('newsletter:plain_message', [$entity->getURL()]); // html content $message_html_content = elgg_view_layout('newsletter', ['entity' => $entity]); // convert to inline CSS for email clients $message_html_content = html_email_handler_css_inliner($message_html_content); // add unsubscribe link $unsubscribe_link = newsletter_generate_unsubscribe_link($container, $email); $message_html_content = str_ireplace(urlencode("{unsublink}"), $unsubscribe_link, $message_html_content); // replace online link $online_link = $entity->getURL(); $new_online_link = $online_link . '?e=' . $email; $message_html_content = str_ireplace($online_link, $new_online_link, $message_html_content); // send preview return html_email_handler_send_email(['from' => html_email_handler_make_rfc822_address($container), 'subject' => $message_subject, 'plaintext_message' => $message_plaintext_content, 'to' => $email, 'html_message' => $message_html_content]); }
/** * Send out the generated digest * * @param ElggUser $user the user to send the digest to * @param string $subject message subject * @param string $html_body html message * @param string $plain_link plaintext message * * @return bool */ function digest_send_mail(ElggUser $user, $subject, $html_body, $plain_link = "") { global $digest_mail_send; $result = false; // validate input if (empty($user) || !elgg_instanceof($user, "user", null, "ElggUser") || empty($subject) || empty($html_body)) { return $result; } // convert css $transform = html_email_handler_css_inliner($html_body); if (!empty($transform)) { $html_body = $transform; } // email settings - prevent sending to any other address than the recipient personn $to = html_email_handler_make_rfc822_address($user, false); $plaintext_message = ""; if (!empty($plain_link)) { // make a plaintext message for non HTML users $plaintext_message = elgg_echo("digest:mail:plaintext:description", array($plain_link)); } // send out the mail $options = array("to" => $to, "subject" => $subject, "html_message" => $html_body, "plaintext_message" => $plaintext_message); if (html_email_handler_send_email($options)) { if (empty($digest_mail_send)) { $digest_mail_send = 1; } else { $digest_mail_send++; } $result = true; } return $result; }