Search in sources :

Example 1 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class NotificationsManagerImpl method sendEmail.

private boolean sendEmail(Identity to, Translator translator, List<SubscriptionItem> subItems) {
    String title = translator.translate("rss.title", new String[] { NotificationHelper.getFormatedName(to) });
    StringBuilder htmlText = new StringBuilder();
    htmlText.append("<style>");
    htmlText.append(".o_m_sub h4 {margin: 0 0 10px 0;}");
    htmlText.append(".o_m_sub ul {padding: 0 0 5px 20px; margin: 0;}");
    htmlText.append(".o_m_sub ul li {padding: 0; margin: 1px 0;}");
    htmlText.append(".o_m_go {padding: 5px 0 0 0}");
    htmlText.append(".o_date {font-size: 90%; color: #888}");
    htmlText.append(".o_m_footer {background: #FAFAFA; border: 1px solid #eee; border-radius: 5px; padding: 0 0.5em 0.5em 0.5em; margin: 1em 0 1em 0;' class='o_m_h'}");
    htmlText.append("</style>");
    for (Iterator<SubscriptionItem> it_subs = subItems.iterator(); it_subs.hasNext(); ) {
        SubscriptionItem subitem = it_subs.next();
        // o_m_wrap class for overriding styles in master mail template
        htmlText.append("<div class='o_m_wrap'>");
        // add background here for gmail as they ignore classes.
        htmlText.append("<div class='o_m_sub' style='background: #FAFAFA; padding: 5px 5px; margin: 10px 0;'>");
        // 1: title
        htmlText.append(subitem.getTitle());
        htmlText.append("\n");
        // 2: content
        String desc = subitem.getDescription();
        if (StringHelper.containsNonWhitespace(desc)) {
            htmlText.append(desc);
        }
        // 3: goto-link
        String link = subitem.getLink();
        if (StringHelper.containsNonWhitespace(link)) {
            htmlText.append("<div class='o_m_go'><a href=\"").append(link).append("\">");
            SubscriptionInfo subscriptionInfo = subitem.getSubsInfo();
            if (subscriptionInfo != null) {
                String innerType = subscriptionInfo.getType();
                String typeName = NewControllerFactory.translateResourceableTypeName(innerType, translator.getLocale());
                String open = translator.translate("resource.open", new String[] { typeName });
                htmlText.append(open);
                htmlText.append(" &raquo;</a></div>");
            }
        }
        htmlText.append("\n");
        htmlText.append("</div></div>");
    }
    String basePath = Settings.getServerContextPathURI() + "/auth/HomeSite/" + to.getKey() + "/";
    htmlText.append("<div class='o_m_footer'>");
    htmlText.append(translator.translate("footer.notifications", new String[] { basePath + "mysettings/0", basePath += "notifications/0", basePath + "/tab/1" }));
    htmlText.append("</div>");
    MailerResult result = null;
    try {
        MailBundle bundle = new MailBundle();
        bundle.setToId(to);
        bundle.setContent(title, htmlText.toString());
        result = CoreSpringFactory.getImpl(MailManager.class).sendExternMessage(bundle, null, true);
    } catch (Exception e) {
    // FXOLAT-294 :: sending the mail will throw nullpointer exception if To-Identity has no
    // valid email-address!, catch it...
    }
    if (result == null || result.getReturnCode() > 0) {
        if (result != null)
            log.warn("Could not send email to identity " + to.getName() + ". (returncode=" + result.getReturnCode() + ", to=" + to + ")");
        else
            log.warn("Could not send email to identity " + to.getName() + ". (returncode = null) , to=" + to + ")");
        return false;
    } else {
        return true;
    }
}
Also used : SubscriptionItem(org.olat.core.commons.services.notifications.SubscriptionItem) MailerResult(org.olat.core.util.mail.MailerResult) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) NoSubscriptionInfo(org.olat.core.commons.services.notifications.model.NoSubscriptionInfo) MailBundle(org.olat.core.util.mail.MailBundle) AssertException(org.olat.core.logging.AssertException)

Example 2 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class MailManagerImpl method makeMailBundle.

@Override
public MailBundle makeMailBundle(MailContext ctxt, Identity recipientTO, MailTemplate template, Identity sender, String metaId, MailerResult result) {
    MailBundle bundle;
    if (MailHelper.isDisabledMailAddress(recipientTO, result)) {
        // email disabled, nothing to do
        bundle = null;
    } else {
        MailContent msg = createWithContext(recipientTO, template, result);
        if (msg != null && result.getReturnCode() == MailerResult.OK) {
            // send mail
            bundle = new MailBundle();
            bundle.setContext(ctxt);
            bundle.setFromId(sender);
            bundle.setToId(recipientTO);
            bundle.setMetaId(metaId);
            bundle.setContent(msg);
        } else {
            bundle = null;
        }
    }
    return bundle;
}
Also used : MailBundle(org.olat.core.util.mail.MailBundle) SimpleMailContent(org.olat.core.util.mail.model.SimpleMailContent) MailContent(org.olat.core.util.mail.MailContent)

Example 3 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class BulkAssessmentTask method sendFeedback.

private void sendFeedback(List<BulkAssessmentFeedback> feedbacks) {
    if (task == null) {
        log.error("Haven't a task to know creator and modifiers of the task", null);
        return;
    }
    Identity creator = task.getCreator();
    String language = creator.getUser().getPreferences().getLanguage();
    Locale locale = I18nManager.getInstance().getLocaleOrDefault(language);
    Translator translator = Util.createPackageTranslator(BulkAssessmentOverviewController.class, locale, Util.createPackageTranslator(AssessmentManager.class, locale));
    MailManager mailManager = CoreSpringFactory.getImpl(MailManager.class);
    TaskExecutorManager taskManager = CoreSpringFactory.getImpl(TaskExecutorManager.class);
    String feedbackStr = renderFeedback(feedbacks, translator);
    MailBundle mail = new MailBundle();
    mail.setToId(creator);
    mail.setFrom(WebappHelper.getMailConfig("mailReplyTo"));
    List<Identity> modifiers = taskManager.getModifiers(task);
    if (modifiers.size() > 0) {
        ContactList cc = new ContactList("CC");
        cc.addAllIdentites(modifiers);
        mail.setContactList(cc);
    }
    String businessPath = "";
    ICourse course = CourseFactory.loadCourse(courseRes);
    CourseNode node = course.getRunStructure().getNode(courseNodeIdent);
    String courseTitle = course.getCourseTitle();
    String nodeTitle = node.getShortTitle();
    String numOfAssessedIds = Integer.toString(datas == null ? 0 : datas.getRowsSize());
    String date = Formatter.getInstance(locale).formatDateAndTime(new Date());
    mail.setContext(new MailContextImpl(courseRes, courseNodeIdent, businessPath));
    String subject = translator.translate("confirmation.mail.subject", new String[] { courseTitle, nodeTitle });
    String body = translator.translate("confirmation.mail.body", new String[] { courseTitle, nodeTitle, feedbackStr, numOfAssessedIds, date });
    mail.setContent(subject, body);
    mailManager.sendMessage(mail);
}
Also used : Locale(java.util.Locale) TaskExecutorManager(org.olat.core.commons.services.taskexecutor.TaskExecutorManager) MailContextImpl(org.olat.core.util.mail.MailContextImpl) AssessmentManager(org.olat.course.assessment.AssessmentManager) ICourse(org.olat.course.ICourse) ContactList(org.olat.core.util.mail.ContactList) Date(java.util.Date) Translator(org.olat.core.gui.translator.Translator) MailManager(org.olat.core.util.mail.MailManager) CourseNode(org.olat.course.nodes.CourseNode) TACourseNode(org.olat.course.nodes.TACourseNode) GTACourseNode(org.olat.course.nodes.GTACourseNode) MSCourseNode(org.olat.course.nodes.MSCourseNode) AssessableCourseNode(org.olat.course.nodes.AssessableCourseNode) ProjectBrokerCourseNode(org.olat.course.nodes.ProjectBrokerCourseNode) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 4 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class LoginTableDataModel method sendMail.

/**
 * Send the mail with informations: who deletes, when, list of deleted users
 * list of not deleted users, reason for deletion
 */
public void sendMail() {
    String recipient = WebappHelper.getMailConfig("mailDeleteUser");
    if (recipient.equals("disabled")) {
        return;
    }
    StringBuilder loginsFound = new StringBuilder();
    for (String login : lstLoginsFound) {
        loginsFound.append(login + "\n");
    }
    StringBuilder loginsNotfound = new StringBuilder();
    for (String login : lstLoginsNotfound) {
        loginsNotfound.append(login + "\n");
    }
    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, getLocale());
    String[] bodyArgs = new String[] { getIdentity().getName(), loginsFound.toString(), loginsNotfound.toString(), reason, df.format(new Date()) };
    ContactList cl = new ContactList(recipient);
    cl.add(recipient);
    cl.add(getIdentity());
    try {
        MailBundle bundle = new MailBundle();
        bundle.setFrom(WebappHelper.getMailConfig("mailReplyTo"));
        bundle.setContent(translate("mail.subject"), translate("mail.body", bodyArgs));
        bundle.setContactList(cl);
        mailService.sendMessage(bundle);
    } catch (Exception e) {
        logError("Notificatoin mail for bulk deletion could not be sent", null);
    }
}
Also used : DateFormat(java.text.DateFormat) ContactList(org.olat.core.util.mail.ContactList) MailBundle(org.olat.core.util.mail.MailBundle) Date(java.util.Date)

Example 5 with MailBundle

use of org.olat.core.util.mail.MailBundle in project OpenOLAT by OpenOLAT.

the class GroupController method doRemoveIdentitiesFromGroup.

private void doRemoveIdentitiesFromGroup(UserRequest ureq, List<Identity> toBeRemoved, MailTemplate mailTemplate) {
    fireEvent(ureq, new IdentitiesRemoveEvent(toBeRemoved));
    identitiesTableModel.remove(toBeRemoved);
    if (tableCtr != null) {
        // can be null in the follwoing case.
        // the user which does the removal is also in the list of toBeRemoved
        // hence the fireEvent does trigger a disposal of a GroupController, which
        // in turn nullifies the tableCtr... see also OLAT-3331
        tableCtr.modelChanged();
    }
    // send the notification mail
    if (mailTemplate != null) {
        // means no sender in footer
        Identity sender = null;
        if (showSenderInRemovMailFooter) {
            sender = ureq.getIdentity();
        }
        String metaId = UUID.randomUUID().toString();
        MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
        MailerResult result = new MailerResult();
        MailBundle[] bundles = mailManager.makeMailBundles(context, toBeRemoved, mailTemplate, sender, metaId, result);
        result.append(mailManager.sendMessage(bundles));
        if (mailTemplate.getCpfrom()) {
            MailBundle ccBundle = mailManager.makeMailBundle(context, ureq.getIdentity(), mailTemplate, sender, metaId, result);
            result.append(mailManager.sendMessage(ccBundle));
        }
        MailHelper.printErrorsAndWarnings(result, getWindowControl(), ureq.getUserSession().getRoles().isOLATAdmin(), ureq.getLocale());
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Aggregations

MailBundle (org.olat.core.util.mail.MailBundle)126 MailerResult (org.olat.core.util.mail.MailerResult)100 Identity (org.olat.core.id.Identity)72 MailContextImpl (org.olat.core.util.mail.MailContextImpl)72 MailContext (org.olat.core.util.mail.MailContext)70 MailTemplate (org.olat.core.util.mail.MailTemplate)32 Translator (org.olat.core.gui.translator.Translator)28 ContactList (org.olat.core.util.mail.ContactList)28 Locale (java.util.Locale)20 File (java.io.File)18 Date (java.util.Date)16 Test (org.junit.Test)14 MailManager (org.olat.core.util.mail.MailManager)14 DBMailLight (org.olat.core.util.mail.model.DBMailLight)12 ArrayList (java.util.ArrayList)10 Property (org.olat.properties.Property)7 ContextEntry (org.olat.core.id.context.ContextEntry)6 AssertException (org.olat.core.logging.AssertException)6 TemporaryKey (org.olat.registration.TemporaryKey)6 UserManager (org.olat.user.UserManager)6