Search in sources :

Example 61 with MailContextImpl

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

the class SendMailController method formOK.

@Override
protected void formOK(UserRequest ureq) {
    ContactList contactList = new ContactList("");
    Collection<String> roleList = contactEl.getSelectedKeys();
    String[] roles = roleList.toArray(new String[roleList.size()]);
    List<Identity> identities = repositoryService.getMembers(repoEntries, RepositoryEntryRelationType.both, roles);
    if (identities.isEmpty()) {
        showWarning("error.contact.to.empty");
    } else {
        Set<Identity> deduplicates = new HashSet<>(identities);
        contactList.addAllIdentites(deduplicates);
        boolean success = false;
        try {
            File[] attachmentArr = getAttachments();
            MailContext context = null;
            if (repoEntries.size() == 1) {
                context = new MailContextImpl("[RepositoryEntry:" + repoEntries.get(0).getKey() + "]");
            }
            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) ContactList(org.olat.core.util.mail.ContactList) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle) File(java.io.File) HashSet(java.util.HashSet)

Example 62 with MailContextImpl

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

the class RepositoryMailing method sendEmail.

public static void sendEmail(Identity ureqIdentity, Identity identity, RepositoryEntry re, Type type, MailPackage mailing) {
    if (mailing != null && !mailing.isSendEmail()) {
        return;
    }
    String email = identity.getUser().getProperty(UserConstants.EMAIL, null);
    String emailAlt = identity.getUser().getProperty(UserConstants.INSTITUTIONALEMAIL, null);
    if (!StringHelper.containsNonWhitespace(email) && !StringHelper.containsNonWhitespace(emailAlt)) {
        return;
    }
    if (mailing == null) {
        BaseSecurity securityManager = CoreSpringFactory.getImpl(BaseSecurity.class);
        RepositoryModule repositoryModule = CoreSpringFactory.getImpl(RepositoryModule.class);
        Roles ureqRoles = securityManager.getRoles(ureqIdentity);
        if (!repositoryModule.isMandatoryEnrolmentEmail(ureqRoles)) {
            return;
        }
    }
    MailTemplate template = mailing == null ? null : mailing.getTemplate();
    if (mailing == null || mailing.getTemplate() == null) {
        template = getDefaultTemplate(type, re, ureqIdentity);
    }
    MailContext context = mailing == null ? null : mailing.getContext();
    if (context == null) {
        context = new MailContextImpl(null, null, "[RepositoryEntry:" + re.getKey() + "]");
    }
    String metaId = mailing == null ? null : mailing.getUuid();
    MailerResult result = new MailerResult();
    MailManager mailManager = CoreSpringFactory.getImpl(MailManager.class);
    MailBundle bundle = mailManager.makeMailBundle(context, identity, template, ureqIdentity, metaId, result);
    if (bundle != null) {
        mailManager.sendMessage(bundle);
    }
    if (mailing != null) {
        mailing.appendResult(result);
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) MailTemplate(org.olat.core.util.mail.MailTemplate) MailManager(org.olat.core.util.mail.MailManager) Roles(org.olat.core.id.Roles) MailBundle(org.olat.core.util.mail.MailBundle) BaseSecurity(org.olat.basesecurity.BaseSecurity)

Example 63 with MailContextImpl

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

the class EPShareListController method sendInvitation.

/**
 * sends a link to the map to permitted users by email
 *
 * @param ureq
 * @param wrapper
 */
private void sendInvitation(UserRequest ureq, EPSharePolicyWrapper wrapper) {
    EPMapPolicy.Type shareType = wrapper.getType();
    List<Identity> identitiesToMail = new ArrayList<Identity>();
    Invitation invitation = null;
    if (shareType.equals(EPMapPolicy.Type.allusers)) {
        return;
    } else if (shareType.equals(EPMapPolicy.Type.invitation)) {
        invitation = wrapper.getInvitation();
    } else if (shareType.equals(EPMapPolicy.Type.group)) {
        List<BusinessGroup> groups = wrapper.getGroups();
        List<Identity> members = businessGroupService.getMembers(groups, GroupRoles.coach.name(), GroupRoles.participant.name());
        identitiesToMail.addAll(members);
    } else if (shareType.equals(EPMapPolicy.Type.user)) {
        identitiesToMail = wrapper.getIdentities();
    }
    wrapper.setInvitationSend(true);
    ContactList contactList = null;
    if (identitiesToMail.size() == 1) {
        contactList = new ContactList(identitiesToMail.get(0).getUser().getProperty(UserConstants.EMAIL, ureq.getLocale()));
    } else {
        contactList = new ContactList(translate("map.share.invitation.mail.list"));
    }
    contactList.addAllIdentites(identitiesToMail);
    String busLink = "";
    if (invitation != null) {
        contactList.add(invitation.getMail());
        busLink = getInvitationLink(invitation, map);
    } else {
        BusinessControlFactory bCF = BusinessControlFactory.getInstance();
        ContextEntry mapCE = bCF.createContextEntry(map.getOlatResource());
        ArrayList<ContextEntry> cEList = new ArrayList<ContextEntry>();
        cEList.add(mapCE);
        busLink = bCF.getAsURIString(cEList, true);
    }
    boolean success = false;
    try {
        String first = getIdentity().getUser().getProperty(UserConstants.FIRSTNAME, null);
        String last = getIdentity().getUser().getProperty(UserConstants.LASTNAME, null);
        String sender = first + " " + last;
        String[] bodyArgs = new String[] { busLink, sender };
        MailContext context = new MailContextImpl(map.getOlatResource(), null, getWindowControl().getBusinessControl().getAsString());
        MailBundle bundle = new MailBundle();
        bundle.setContext(context);
        bundle.setFrom(WebappHelper.getMailConfig("mailReplyTo"));
        bundle.setContactList(contactList);
        bundle.setContent(translate("map.share.invitation.mail.subject"), translate("map.share.invitation.mail.body", bodyArgs));
        MailerResult result = mailManager.sendMessage(bundle);
        success = result.isSuccessful();
    } catch (Exception e) {
        logError("Error on sending invitation mail to contactlist, invalid address.", e);
    }
    if (success) {
        showInfo("map.share.invitation.mail.success");
    } else {
        showError("map.share.invitation.mail.failure");
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) BusinessGroup(org.olat.group.BusinessGroup) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) EPMapPolicy(org.olat.portfolio.manager.EPMapPolicy) ArrayList(java.util.ArrayList) Invitation(org.olat.basesecurity.Invitation) ContactList(org.olat.core.util.mail.ContactList) ContextEntry(org.olat.core.id.context.ContextEntry) Type(org.olat.portfolio.manager.EPMapPolicy.Type) BusinessControlFactory(org.olat.core.id.context.BusinessControlFactory) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 64 with MailContextImpl

use of org.olat.core.util.mail.MailContextImpl 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) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 65 with MailContextImpl

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

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)

Aggregations

MailBundle (org.olat.core.util.mail.MailBundle)72 MailContextImpl (org.olat.core.util.mail.MailContextImpl)72 MailContext (org.olat.core.util.mail.MailContext)70 MailerResult (org.olat.core.util.mail.MailerResult)66 Identity (org.olat.core.id.Identity)44 MailTemplate (org.olat.core.util.mail.MailTemplate)30 ContactList (org.olat.core.util.mail.ContactList)20 File (java.io.File)14 ArrayList (java.util.ArrayList)8 Locale (java.util.Locale)8 MailManager (org.olat.core.util.mail.MailManager)8 Translator (org.olat.core.gui.translator.Translator)7 Property (org.olat.properties.Property)7 ContextEntry (org.olat.core.id.context.ContextEntry)6 HashSet (java.util.HashSet)4 List (java.util.List)4 IdentitiesAddEvent (org.olat.admin.securitygroup.gui.IdentitiesAddEvent)4 IdentitiesRemoveEvent (org.olat.admin.securitygroup.gui.IdentitiesRemoveEvent)4 BaseSecurity (org.olat.basesecurity.BaseSecurity)4 AssertException (org.olat.core.logging.AssertException)4