Search in sources :

Example 41 with MailerResult

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

the class UserDeletionManager method sendUserDeleteEmailTo.

/**
 * Send 'delete'- emails to a list of identities. The delete email is an announcement for the user-deletion.
 *
 * @param selectedIdentities
 * @return String with warning message (e.g. email-address not valid, could not send email).
 *         If there is no warning, the return String is empty ("").
 */
public String sendUserDeleteEmailTo(List<Identity> selectedIdentities, MailTemplate template, boolean isTemplateChanged, String keyEmailSubject, String keyEmailBody, Identity sender, Translator pT) {
    StringBuilder buf = new StringBuilder();
    if (template != null) {
        template.addToContext("responseTo", deletionModule.getEmailResponseTo());
        for (Iterator<Identity> iter = selectedIdentities.iterator(); iter.hasNext(); ) {
            Identity identity = iter.next();
            if (!isTemplateChanged) {
                // Email template has NOT changed => take translated version of subject and body text
                Translator identityTranslator = Util.createPackageTranslator(SelectionController.class, I18nManager.getInstance().getLocaleOrDefault(identity.getUser().getPreferences().getLanguage()));
                template.setSubjectTemplate(identityTranslator.translate(keyEmailSubject));
                template.setBodyTemplate(identityTranslator.translate(keyEmailBody));
            }
            template.putVariablesInMailContext(template.getContext(), identity);
            logDebug(" Try to send Delete-email to identity=" + identity.getName() + " with email=" + identity.getUser().getProperty(UserConstants.EMAIL, null));
            MailerResult result = new MailerResult();
            MailBundle bundle = mailManager.makeMailBundle(null, identity, template, null, null, result);
            if (bundle != null) {
                mailManager.sendMessage(bundle);
            }
            if (template.getCpfrom()) {
                MailBundle ccBundle = mailManager.makeMailBundle(null, sender, template, sender, null, result);
                if (ccBundle != null) {
                    mailManager.sendMessage(ccBundle);
                }
            }
            if (result.getReturnCode() != MailerResult.OK) {
                buf.append(pT.translate("email.error.send.failed", new String[] { identity.getUser().getProperty(UserConstants.EMAIL, null), identity.getName() })).append("\n");
            }
            logAudit("User-Deletion: Delete-email send to identity=" + identity.getName() + " with email=" + identity.getUser().getProperty(UserConstants.EMAIL, null));
            markSendEmailEvent(identity);
        }
    } else {
        // no template => User decides to sending no delete-email, mark only in lifecycle table 'sendEmail'
        for (Iterator<Identity> iter = selectedIdentities.iterator(); iter.hasNext(); ) {
            Identity identity = iter.next();
            logAudit("User-Deletion: Move in 'Email sent' section without sending email, identity=" + identity.getName());
            markSendEmailEvent(identity);
        }
    }
    return buf.toString();
}
Also used : Translator(org.olat.core.gui.translator.Translator) MailerResult(org.olat.core.util.mail.MailerResult) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 42 with MailerResult

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

the class InfoMessageFrontendManagerImpl method sendInfoMessage.

@Override
public boolean sendInfoMessage(InfoMessage infoMessage, MailFormatter mailFormatter, Locale locale, Identity from, List<Identity> tos) {
    infoMessageManager.saveInfoMessage(infoMessage);
    boolean send = false;
    if (tos != null && !tos.isEmpty()) {
        Set<Long> identityKeySet = new HashSet<Long>();
        ContactList contactList = new ContactList("Infos");
        for (Identity to : tos) {
            if (identityKeySet.contains(to.getKey()))
                continue;
            contactList.add(to);
            identityKeySet.add(to.getKey());
        }
        try {
            String subject = null;
            String body = null;
            if (mailFormatter != null) {
                subject = mailFormatter.getSubject(infoMessage);
                body = mailFormatter.getBody(infoMessage);
            }
            if (!StringHelper.containsNonWhitespace(subject)) {
                subject = infoMessage.getTitle();
            }
            if (!StringHelper.containsNonWhitespace(body)) {
                body = infoMessage.getMessage();
            }
            File attachment = null;
            if (StringHelper.containsNonWhitespace(infoMessage.getAttachmentPath())) {
                File root = getStoragePath().getBasefile();
                attachment = new File(root, infoMessage.getAttachmentPath());
            }
            MailContext context = new MailContextImpl(mailFormatter.getBusinessPath());
            MailBundle bundle = new MailBundle();
            bundle.setContext(context);
            bundle.setFromId(from);
            bundle.setContactList(contactList);
            if (attachment != null) {
                bundle.setContent(subject, body, attachment);
            } else {
                bundle.setContent(subject, body);
            }
            MailerResult result = mailManager.sendMessage(bundle);
            send = result.isSuccessful();
        } catch (Exception e) {
            log.error("Cannot send info messages", e);
        }
    }
    infoSubscriptionManager.markPublisherNews(infoMessage.getOLATResourceable(), infoMessage.getResSubPath());
    MultiUserEvent mue = new MultiUserEvent("new_info_message");
    coordinatorManager.getCoordinator().getEventBus().fireEventToListenersOf(mue, oresFrontend);
    return send;
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) ContactList(org.olat.core.util.mail.ContactList) IOException(java.io.IOException) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle) File(java.io.File) MultiUserEvent(org.olat.core.util.event.MultiUserEvent) HashSet(java.util.HashSet)

Example 43 with MailerResult

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

the class MembersMailController method doSendEmailToMember.

private void doSendEmailToMember(UserRequest ureq, ContactList contactList) {
    boolean success = false;
    try {
        File[] attachmentArr = getAttachments();
        MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
        MailBundle bundle = new MailBundle();
        bundle.setContext(context);
        bundle.setFromId(getIdentity());
        bundle.setContactLists(Collections.singletonList(contactList));
        bundle.setContent(subjectEl.getValue(), bodyEl.getValue(), attachmentArr);
        MailerResult result = mailService.sendMessage(bundle);
        if (copyFromEl.isAtLeastSelected(1)) {
            MailBundle ccBundle = new MailBundle();
            ccBundle.setContext(context);
            ccBundle.setFromId(getIdentity());
            ccBundle.setCc(getIdentity());
            ccBundle.setContent(subjectEl.getValue(), bodyEl.getValue(), attachmentArr);
            MailerResult ccResult = mailService.sendMessage(ccBundle);
            result.append(ccResult);
        }
        success = result.isSuccessful();
    } catch (Exception e) {
        // error in recipient email address(es)
        handleAddressException(success);
    }
    if (success) {
        showInfo("msg.send.ok");
        // do logging
        ThreadLocalUserActivityLogger.log(MailLoggingAction.MAIL_SENT, getClass());
        fireEvent(ureq, Event.DONE_EVENT);
    } else {
        showInfo("error.msg.send.nok");
        fireEvent(ureq, Event.FAILED_EVENT);
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) MailBundle(org.olat.core.util.mail.MailBundle) File(java.io.File)

Example 44 with MailerResult

use of org.olat.core.util.mail.MailerResult in project openolat by klemens.

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 : IdentitiesRemoveEvent(org.olat.admin.securitygroup.gui.IdentitiesRemoveEvent) 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)

Example 45 with MailerResult

use of org.olat.core.util.mail.MailerResult in project openolat by klemens.

the class GroupController method doAddIdentitiesToGroup.

/**
 * Add users from the identites array to the group if they are not guest users
 * and not already in the group
 *
 * @param ureq
 * @param choosenIdentities
 */
private void doAddIdentitiesToGroup(UserRequest ureq, List<Identity> choosenIdentities, MailTemplate mailTemplate) {
    // additional security check
    if (!mayModifyMembers)
        throw new AssertException("not allowed to add member!");
    IdentitiesAddEvent identitiesAddedEvent = new IdentitiesAddEvent(choosenIdentities);
    // process workflow to BusinessGroupManager via BusinessGroupEditController
    fireEvent(ureq, identitiesAddedEvent);
    if (!identitiesAddedEvent.getAddedIdentities().isEmpty()) {
        // update table model
        reloadData();
    }
    // build info message for identities which could be added.
    StringBuilder infoMessage = new StringBuilder();
    for (Identity identity : identitiesAddedEvent.getIdentitiesWithoutPermission()) {
        infoMessage.append(translate("msg.isingroupanonymous", userManager.getUserDisplayName(identity))).append("<br />");
    }
    for (Identity identity : identitiesAddedEvent.getIdentitiesAlreadyInGroup()) {
        infoMessage.append(translate("msg.subjectalreadyingroup", userManager.getUserDisplayName(identity))).append("<br />");
    }
    // send the notification mail fro added users
    StringBuilder errorMessage = new StringBuilder();
    if (mailTemplate != null) {
        // means no sender in footer
        Identity sender = null;
        if (showSenderInAddMailFooter) {
            sender = ureq.getIdentity();
        }
        String metaId = UUID.randomUUID().toString();
        MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
        MailerResult result = new MailerResult();
        MailBundle[] bundles = mailManager.makeMailBundles(context, identitiesAddedEvent.getAddedIdentities(), 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.appendErrorsAndWarnings(result, errorMessage, infoMessage, ureq.getUserSession().getRoles().isOLATAdmin(), ureq.getLocale());
    }
    // report any errors on screen
    if (infoMessage.length() > 0)
        getWindowControl().setWarning(infoMessage.toString());
    if (errorMessage.length() > 0)
        getWindowControl().setError(errorMessage.toString());
}
Also used : AssertException(org.olat.core.logging.AssertException) MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) IdentitiesAddEvent(org.olat.admin.securitygroup.gui.IdentitiesAddEvent) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Aggregations

MailerResult (org.olat.core.util.mail.MailerResult)140 MailBundle (org.olat.core.util.mail.MailBundle)100 Identity (org.olat.core.id.Identity)82 MailContext (org.olat.core.util.mail.MailContext)66 MailContextImpl (org.olat.core.util.mail.MailContextImpl)66 MailTemplate (org.olat.core.util.mail.MailTemplate)46 Test (org.junit.Test)26 File (java.io.File)24 ArrayList (java.util.ArrayList)20 ContactList (org.olat.core.util.mail.ContactList)20 Locale (java.util.Locale)16 Translator (org.olat.core.gui.translator.Translator)16 VelocityContext (org.apache.velocity.VelocityContext)14 DBMailLight (org.olat.core.util.mail.model.DBMailLight)14 Date (java.util.Date)12 MailPackage (org.olat.core.util.mail.MailPackage)12 List (java.util.List)8 User (org.olat.core.id.User)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 Property (org.olat.properties.Property)7