/** * Send an email to any email address * * @param string $from Email address or string: "name <email>" * @param string $to Email address or string: "name <email>" * @param string $subject The subject of the message * @param string $body The message body * @param array $params Optional parameters (none used in this function) * * @return bool * @throws NotificationException * @since 1.7.2 */ function elgg_send_email($from, $to, $subject, $body, array $params = null) { if (!$from) { $msg = "Missing a required parameter, '" . 'from' . "'"; throw new \NotificationException($msg); } if (!$to) { $msg = "Missing a required parameter, '" . 'to' . "'"; throw new \NotificationException($msg); } $headers = array("Content-Type" => "text/plain; charset=UTF-8; format=flowed", "MIME-Version" => "1.0", "Content-Transfer-Encoding" => "8bit"); // return true/false to stop elgg_send_email() from sending $mail_params = array('to' => $to, 'from' => $from, 'subject' => $subject, 'body' => $body, 'headers' => $headers, 'params' => $params); // $mail_params is passed as both params and return value. The former is for backwards // compatibility. The latter is so handlers can now alter the contents/headers of // the email by returning the array $result = _elgg_services()->hooks->trigger('email', 'system', $mail_params, $mail_params); if (!is_array($result)) { // don't need null check: Handlers can't set a hook value to null! return (bool) $result; } // strip name from to and from $to_address = Address::fromString($result['to']); $from_address = Address::fromString($result['from']); $subject = elgg_strip_tags($result['subject']); $subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8'); // Sanitise subject by stripping line endings $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject); $subject = trim($subject); $body = elgg_strip_tags($result['body']); $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8'); $body = wordwrap($body); $message = new Message(); $message->setEncoding('UTF-8'); $message->addFrom($from_address); $message->addTo($to_address); $message->setSubject($subject); $message->setBody($body); foreach ($result['headers'] as $headerName => $headerValue) { $message->getHeaders()->addHeaderLine($headerName, $headerValue); } try { _elgg_services()->mailer->send($message); } catch (\Zend\Mail\Exception\RuntimeException $e) { _elgg_services()->logger->error($e->getMessage()); return false; } return true; }
/** * Send an email to any email address * * @param mixed $from Email address or string: "name <email>" * @param mixed $to Email address or string: "name <email>" * @param string $subject The subject of the message * @param string $body The message body * @param array $params Optional parameters * @return bool * @throws NotificationException */ function notifications_html_handler_send_email($from, $to, $subject, $body, array $params = null) { $options = array('to' => $to, 'from' => $from, 'subject' => $subject, 'body' => $body, 'params' => $params, 'headers' => array("Content-Type" => "text/html; charset=UTF-8; format=flowed", "MIME-Version" => "1.0", "Content-Transfer-Encoding" => "8bit")); // $mail_params is passed as both params and return value. The former is for backwards // compatibility. The latter is so handlers can now alter the contents/headers of // the email by returning the array $options = elgg_trigger_plugin_hook('email', 'system', $options, $options); if (!is_array($options)) { // don't need null check: Handlers can't set a hook value to null! return (bool) $options; } try { if (empty($options['from'])) { $msg = "Missing a required parameter, '" . 'from' . "'"; throw new \NotificationException($msg); } if (empty($options['to'])) { $msg = "Missing a required parameter, '" . 'to' . "'"; throw new \NotificationException($msg); } $options['to'] = \Elgg\Mail\Address::fromString($options['to']); $options['from'] = \Elgg\Mail\Address::fromString($options['from']); $options['subject'] = elgg_strip_tags($options['subject']); $options['subject'] = html_entity_decode($options['subject'], ENT_QUOTES, 'UTF-8'); // Sanitise subject by stripping line endings $options['subject'] = preg_replace("/(\r\n|\r|\n)/", " ", $options['subject']); $options['subject'] = elgg_get_excerpt(trim($options['subject'], 80)); $message = new \Zend\Mail\Message(); foreach ($options['headers'] as $headerName => $headerValue) { $message->getHeaders()->addHeaderLine($headerName, $headerValue); } $message->setEncoding('UTF-8'); $message->addFrom($options['from']); $message->addTo($options['to']); $message->setSubject($options['subject']); $body = new Zend\Mime\Message(); $html = new \Zend\Mime\Part($options['body']); $html->type = "text/html"; $body->addPart($html); $files = elgg_extract('attachments', $options['params']); if (!empty($files) && is_array($files)) { foreach ($files as $file) { if (!$file instanceof \ElggFile) { continue; } $attachment = new \Zend\Mime\Part(fopen($file->getFilenameOnFilestore(), 'r')); $attachment->type = $file->getMimeType() ?: $file->detectMimeType(); $attachment->filename = $file->originalfilename ?: basename($file->getFilename()); $attachment->disposition = Zend\Mime\Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Zend\Mime\Mime::ENCODING_BASE64; $body->addPart($attachment); } } $message->setBody($body); $transport = notifications_html_handler_get_transport(); if (!$transport instanceof Zend\Mail\Transport\TransportInterface) { throw new \NotificationException("Invalid Email transport"); } $transport->send($message); } catch (\Exception $e) { elgg_log($e->getMessage(), 'ERROR'); return false; } return true; }