Adding text email support see #6193

skala
Julio Montoya 13 years ago
parent 9310760af3
commit 19c30c22a5
  1. 31
      main/inc/lib/main_api.lib.php
  2. 21
      main/inc/lib/message.lib.php
  3. 18
      main/inc/lib/notification.lib.php
  4. 20
      main/template/default/mail/html_mail_layout.tpl
  5. 32
      main/template/default/mail/layout.tpl
  6. 13
      main/template/default/mail/sample/sample.tpl
  7. 12
      main/template/default/mail/text_mail_layout.tpl
  8. 26
      src/ChamiloLMS/Component/Mail/MailGenerator.php
  9. 7
      src/ChamiloLMS/Controller/IndexController.php

@ -6550,7 +6550,18 @@ function api_mail($recipient_name, $recipient_email, $subject, $message, $sender
* @return returns true if mail was sent
* @see class.phpmailer.php
*/
function api_mail_html($recipient_name, $recipient_email, $subject, $body, $sender_name = '', $sender_email = '', $extra_headers = null, $data_file = array(), $embedded_image = false) {
function api_mail_html(
$recipient_name,
$recipient_email,
$subject,
$body,
$sender_name = '',
$sender_email = '',
$extra_headers = null,
$data_file = array(),
$embedded_image = false,
$text_body = null
) {
global $app;
$reply_to_mail = $sender_email;
@ -6561,11 +6572,19 @@ function api_mail_html($recipient_name, $recipient_email, $subject, $body, $send
$reply_to_name = $extra_headers['reply_to']['name'];
}
//$mail->AltBody = strip_tags(str_replace('<br />',"\n", api_html_entity_decode($message)));
// Forcing the conversion.
if (strpos($body, '<html>') === false) {
$htmlBody = str_replace(array("\n\r", "\n", "\r"), '<br />', $body);
$htmlBody = '<html><head></head><body>'.$htmlBody.'</body></html>';
} else {
$htmlBody = $body;
}
//Forcing the conversion
$htmlBody = str_replace(array("\n\r", "\n", "\r"), '<br />', $body);
$htmlBody = '<html><head></head><body>'.$htmlBody.'</body></html>';
if (!empty($text_body)) {
$textBody = $text_body;
} else {
$textBody = $body;
}
try {
$message = \Swift_Message::newInstance()
@ -6574,7 +6593,7 @@ function api_mail_html($recipient_name, $recipient_email, $subject, $body, $send
->setTo(array($recipient_email => $recipient_name))
->setReplyTo(array($reply_to_mail => $reply_to_name))
->setBody($htmlBody, 'text/html')
->addPart($body, 'text/plain')
->addPart($textBody, 'text/plain')
->setEncoder(Swift_Encoding::get8BitEncoding());
if (!empty($data_file)) {
// Attach it to the message

@ -184,7 +184,8 @@ class MessageManager
$parent_id = 0,
$edit_message_id = 0,
$topic_id = 0,
$sender_id = null
$sender_id = null,
$text_content = null
) {
$table_message = Database::get_main_table(TABLE_MESSAGE);
$group_id = intval($group_id);
@ -301,7 +302,8 @@ class MessageManager
array($receiver_user_id),
$subject,
$content,
$sender_info
$sender_info,
$text_content
);
} else {
$usergroup = new UserGroup();
@ -325,7 +327,8 @@ class MessageManager
$new_user_list,
$subject,
$content,
$group_info
$group_info,
$text_content
);
}
@ -338,19 +341,20 @@ class MessageManager
/**
* A handy way to send message
*/
public static function send_message_simple($receiver_user_id, $subject, $message, $sender_id = null)
public static function send_message_simple($receiver_user_id, $subject, $htmlBody, $sender_id = null, $textBody = null)
{
return MessageManager::send_message(
$receiver_user_id,
$subject,
$message,
$htmlBody,
null,
null,
null,
null,
null,
null,
$sender_id
$sender_id,
$textBody
);
}
@ -365,7 +369,7 @@ class MessageManager
// Inject $app in the constructor of this class
global $app;
$result = $app['mail_generator']->getMessage($template, $params);
return self::send_message_simple($receiverUserId, $result['subject'], $result['body'], $senderId);
return self::send_message_simple($receiverUserId, $result['subject'], $result['html_body'], $senderId, $result['text_body']);
}
/**
@ -921,11 +925,10 @@ class MessageManager
) : '').'</div>
';
$social_link = '';
if ($_GET['f'] == 'social') {
if (isset($_GET['f']) && $_GET['f'] == 'social') {
$social_link = 'f=social';
}
if ($source == 'outbox') {
$message_content .= '<a href="outbox.php?'.$social_link.'">'.Display::return_icon(
'back.png',

@ -146,7 +146,7 @@ class Notification extends Model
* @param string content of the message
* @param array result of api_get_user_info() or UserGroup->get()
*/
public function save_notification($type, $user_list, $title, $content, $sender_info = array())
public function save_notification($type, $user_list, $title, $content, $sender_info = array(), $text_content = null)
{
$this->type = intval($type);
$content = $this->format_content($content, $sender_info);
@ -221,7 +221,10 @@ class Notification extends Model
Security::filter_terms($content),
$sender_info['complete_name'],
$sender_info['email'],
$extra_headers
$extra_headers,
array(),
null,
$text_content
);
} else {
api_mail_html(
@ -230,7 +233,10 @@ class Notification extends Model
Security::filter_terms($title),
Security::filter_terms($content),
$sender_info['complete_name'],
$sender_info['email']
$sender_info['email'],
array(),
null,
$text_content
);
}
}
@ -254,9 +260,11 @@ class Notification extends Model
* Formats the content in order to add the welcome message, the notification preference, etc
* @param string the content
* @param array result of api_get_user_info() or UserGroup->get()
* @todo create new templates based in Twig
* */
public function format_content($content, $sender_info)
{
return $content;
$new_message_text = $link_to_new_message = '';
switch ($this->type) {
@ -310,9 +318,7 @@ class Notification extends Model
$new_message_text .= '<br />'.get_lang('User').': '.$sender_name;
}
}
$group_url = api_get_path(
WEB_CODE_PATH
).'social/group_topics.php?id='.$sender_info['group_info']['id'].'&topic_id='.$sender_info['group_info']['topic_id'].'&msg_id='.$sender_info['group_info']['msg_id'].'&topics_page_nr='.$topic_page;
$group_url = api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$sender_info['group_info']['id'].'&topic_id='.$sender_info['group_info']['topic_id'].'&msg_id='.$sender_info['group_info']['msg_id'].'&topics_page_nr='.$topic_page;
$link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
break;
}

@ -1,20 +0,0 @@
<html>
<head>
<title>
{% block subject %}
{% endblock %}
</title>
</head>
<body>
<header>
<strong>Chamilo mail header!!</strong>
</header>
{% block body %}
HTML <strong>body</strong>
{% endblock %}
<hr>
<footer>
<strong>Chamilo mail footer!!</strong>
</footer>
</body>
</html>

@ -0,0 +1,32 @@
{% block subject %}
{% endblock %}
{% block html_template %}
<html>
<head>
</head>
<body>
<header>
<strong>Chamilo mail header!!</strong>
</header>
{% block html_body %}
HTML <strong>body</strong>
{% endblock %}
<hr>
<footer>
<strong>Chamilo mail footer!!</strong>
</footer>
</body>
</html>
{% endblock %}
{% block text_template %}
Chamilo mail header in plain TXT
{% block text_body %}
txt body
{% endblock %}
----------------------------------------
Chamilo mail footer in plain TXT
{% endblock %}

@ -1,10 +1,17 @@
{% extends app.template_style ~ "/mail/html_mail_layout.tpl" %}
{% extends "default/mail/layout.tpl" %}
{% block subject %}
{{ yolo.subject }}
{% endblock %}
{% block body %}
{{ yolo.content }}
{% block html_body %}
<h1>{{ yolo.title }}</h1>
<p>{{ yolo.content }}</p>
{{ yolo.user }}
{% endblock %}
{% block text_body %}
* {{ yolo.title }} *
> {{ yolo.content }}
:) {{ yolo.user }}
{% endblock %}

@ -1,12 +0,0 @@
Chamilo mail header in plain TXT
{% block body %}
txt body
{% endblock %}
----------------------------------------
Chamilo mail footer in plain TXT
{% block subject %}
txt subject
{% endblock %}

@ -2,34 +2,44 @@
namespace ChamiloLMS\Component\Mail;
/**
* Class MailGenerator
* @package ChamiloLMS\Component\Mail
*/
class MailGenerator
{
protected $twig;
protected $mailer;
/**
* @param \Twig_Environment $twig
* @param $mailer
*/
public function __construct(\Twig_Environment $twig, $mailer)
{
$this->twig = $twig;
$this->mailer = $mailer;
}
/**
* Loads a template file located in default/mail the tpl must declare 3 blocks: subject, html_body and text_body
* @param $identifier
* @param array $parameters
* @return array
*/
public function getMessage($identifier, $parameters = array())
{
/** @var \Twig_Environment $template */
$template = $this->twig->loadTemplate('default/mail/'.$identifier);
$subject = $template->renderBlock('subject', $parameters);
$bodyHtml = $template->render($parameters);
/*$bodyText = $template->renderBlock('body_text', $parameters);
$htmlBody = $template->renderBlock('html_template', $parameters);
$textBody = $template->renderBlock('text_template', $parameters);
//return $this->mailer
return \Swift_Message::newInstance()
->setSubject($subject)
->setBody($bodyText, 'text/plain')
->addPart($bodyHtml, 'text/html');*/
return array(
'subject' => $subject,
'body' => $bodyHtml
'html_body' => $htmlBody,
'text_body' => $textBody
);
}
}

@ -41,14 +41,13 @@ class IndexController extends CommonController
/** @var \Template $template */
$template = $app['template'];
/*
$params['yolo'] = array(
/*$params['yolo'] = array(
'subject' => 'subject julito',
'title' => 'Title',
'content' => 'content julito',
'user' => 'julito'
);
\MessageManager::sendMessageUsingTemplate('sample/sample.tpl', $params, 1);
*/
\MessageManager::sendMessageUsingTemplate('sample/sample.tpl', $params, 1);*/
$loginError = $app['request']->get('error');

Loading…
Cancel
Save