Search in sources :

Example 56 with MailContext

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

the class DropboxController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
 */
@Override
public void event(UserRequest ureq, Controller source, Event event) {
    if (source == fileChooserController) {
        cmc.deactivate();
        if (event.equals(Event.DONE_EVENT)) {
            boolean success = false;
            File fIn = fileChooserController.getUploadFile();
            VFSContainer fDropbox = getDropBox(ureq.getIdentity());
            String filename = fileChooserController.getUploadFileName();
            VFSLeaf fOut;
            if (fDropbox.resolve(filename) != null) {
                // FIXME ms: check if dropbox quota is exceeded -> clarify with customers
                fOut = fDropbox.createChildLeaf(getNewUniqueName(filename));
            } else {
                fOut = fDropbox.createChildLeaf(filename);
            }
            try {
                InputStream in = new FileInputStream(fIn);
                OutputStream out = new BufferedOutputStream(fOut.getOutputStream(false));
                success = FileUtils.copy(in, out);
                FileUtils.closeSafely(in);
                FileUtils.closeSafely(out);
            } catch (FileNotFoundException e) {
                logError("", e);
                return;
            }
            if (fOut instanceof MetaTagged) {
                MetaInfo info = ((MetaTagged) fOut).getMetaInfo();
                if (info != null) {
                    info.setAuthor(ureq.getIdentity());
                    info.write();
                }
            }
            if (success) {
                int numFiles = fDropbox.getItems().size();
                myContent.contextPut("numfiles", new String[] { Integer.toString(numFiles) });
                // assemble confirmation
                String confirmation = getConfirmation(ureq, fOut.getName());
                // send email if necessary
                Boolean sendEmail = (Boolean) config.get(TACourseNode.CONF_DROPBOX_ENABLEMAIL);
                if (sendEmail == null)
                    sendEmail = Boolean.FALSE;
                boolean sendMailError = false;
                if (sendEmail.booleanValue()) {
                    // send mail
                    MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
                    MailBundle bundle = new MailBundle();
                    bundle.setContext(context);
                    bundle.setToId(ureq.getIdentity());
                    bundle.setContent(translate("conf.mail.subject"), confirmation);
                    MailerResult result = CoreSpringFactory.getImpl(MailManager.class).sendMessage(bundle);
                    if (result.getFailedIdentites().size() > 0) {
                        List<Identity> disabledIdentities = new ArrayList<Identity>();
                        disabledIdentities = result.getFailedIdentites();
                        // show error that message can not be sent
                        ArrayList<String> myButtons = new ArrayList<String>();
                        myButtons.add(translate("back"));
                        String title = MailHelper.getTitleForFailedUsersError(ureq.getLocale());
                        String message = MailHelper.getMessageForFailedUsersError(ureq.getLocale(), disabledIdentities);
                        // add dropbox specific error message
                        message += "\n<br />" + translate("conf.mail.error");
                        // FIXME:FG:6.2: fix problem in info message, not here
                        message += "\n<br />\n<br />" + confirmation.replace("\n", "&#10;").replace("\r", "&#10;").replace("\u2028", "&#10;");
                        DialogBoxController noUsersErrorCtr = null;
                        noUsersErrorCtr = activateGenericDialog(ureq, title, message, myButtons, noUsersErrorCtr);
                        sendMailError = true;
                    } else if (result.getReturnCode() > 0) {
                        // show error that message can not be sent
                        ArrayList<String> myButtons = new ArrayList<String>();
                        myButtons.add(translate("back"));
                        DialogBoxController noUsersErrorCtr = null;
                        String message = translate("conf.mail.error");
                        // FIXME:FG:6.2: fix problem in info message, not here
                        message += "\n<br />\n<br />" + confirmation.replace("\n", "&#10;").replace("\r", "&#10;").replace("\u2028", "&#10;");
                        noUsersErrorCtr = activateGenericDialog(ureq, translate("error.header"), message, myButtons, noUsersErrorCtr);
                        sendMailError = true;
                    }
                }
                // inform subscription manager about new element
                if (subsContext != null) {
                    NotificationsManager.getInstance().markPublisherNews(subsContext, ureq.getIdentity(), true);
                }
                // FIXME:FG:6.2: fix problem in info message, not here
                if (!sendMailError) {
                    getWindowControl().setInfo(confirmation.replace("\n", "&#10;").replace("\r", "&#10;").replace("\u2028", "&#10;"));
                }
            } else {
                showInfo("dropbox.upload.failed");
            }
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) DialogBoxController(org.olat.core.gui.control.generic.modal.DialogBoxController) MailManager(org.olat.core.util.mail.MailManager) MailBundle(org.olat.core.util.mail.MailBundle) Identity(org.olat.core.id.Identity) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 57 with MailContext

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

the class LectureServiceImpl method sendReminder.

private void sendReminder(Identity teacher, LectureBlock lectureBlock) {
    RepositoryEntry entry = lectureBlock.getEntry();
    String language = teacher.getUser().getPreferences().getLanguage();
    Locale locale = i18nManager.getLocaleOrDefault(language);
    String startDate = Formatter.getInstance(locale).formatDate(lectureBlock.getStartDate());
    MailContext context = new MailContextImpl("[RepositoryEntry:" + entry.getKey() + "]");
    String url = Settings.getServerContextPathURI() + "/url/RepositoryEntry/" + entry.getKey() + "/LectureBlock/" + lectureBlock.getKey();
    String[] args = new String[] { // {0}
    lectureBlock.getTitle(), // {1}
    startDate, // {2}
    entry.getDisplayname(), // {3}
    url, // {4}
    userManager.getUserDisplayName(teacher) };
    Translator trans = Util.createPackageTranslator(LectureAdminController.class, locale);
    String subject = trans.translate("lecture.teacher.reminder.subject", args);
    String body = trans.translate("lecture.teacher.reminder.body", args);
    LectureReminderTemplate template = new LectureReminderTemplate(subject, body);
    MailerResult result = new MailerResult();
    MailBundle bundle = mailManager.makeMailBundle(context, teacher, template, null, null, result);
    MailerResult sendResult = mailManager.sendMessage(bundle);
    result.append(sendResult);
    String status;
    List<Identity> failedIdentities = result.getFailedIdentites();
    if (failedIdentities != null && failedIdentities.contains(teacher)) {
        status = "error";
    } else {
        status = "ok";
    }
    lectureBlockReminderDao.createReminder(lectureBlock, teacher, status);
}
Also used : Locale(java.util.Locale) MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) Translator(org.olat.core.gui.translator.Translator) MailerResult(org.olat.core.util.mail.MailerResult) RepositoryEntry(org.olat.repository.RepositoryEntry) MailBundle(org.olat.core.util.mail.MailBundle) Identity(org.olat.core.id.Identity)

Example 58 with MailContext

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

the class ContactFormController method doSend.

private void doSend(UserRequest ureq) {
    MailerResult result;
    try {
        File[] attachments = cntctForm.getAttachments();
        MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
        MailBundle bundle = new MailBundle();
        bundle.setContext(context);
        if (emailFrom == null) {
            // in case the user provides his own email in form
            bundle.setFrom(cntctForm.getEmailFrom());
        } else {
            bundle.setFromId(emailFrom);
        }
        bundle.setContactLists(cntctForm.getEmailToContactLists());
        bundle.setContent(cntctForm.getSubject(), cntctForm.getBody(), attachments);
        result = mailService.sendMessage(bundle);
        if (cntctForm.isTcpFrom()) {
            MailBundle ccBundle = new MailBundle();
            ccBundle.setContext(context);
            if (emailFrom == null) {
                // in case the user provides his own email in form
                ccBundle.setFrom(cntctForm.getEmailFrom());
                ccBundle.setTo(cntctForm.getEmailFrom());
            } else {
                ccBundle.setFromId(emailFrom);
                ccBundle.setCc(emailFrom);
            }
            ccBundle.setContent(cntctForm.getSubject(), cntctForm.getBody(), attachments);
            MailerResult ccResult = mailService.sendMessage(ccBundle);
            result.append(ccResult);
        }
        if (result != null) {
            if (result.isSuccessful()) {
                showInfo("msg.send.ok");
                // do logging
                ThreadLocalUserActivityLogger.log(MailLoggingAction.MAIL_SENT, getClass());
                fireEvent(ureq, Event.DONE_EVENT);
            } else {
                showError(ureq, result);
                fireEvent(ureq, Event.FAILED_EVENT);
            }
        }
    } catch (Exception e) {
        logError("", e);
        showWarning("error.msg.send.nok");
    }
    cntctForm.setDisplayOnly(true);
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailerResult(org.olat.core.util.mail.MailerResult) MailContext(org.olat.core.util.mail.MailContext) MailBundle(org.olat.core.util.mail.MailBundle) File(java.io.File)

Example 59 with MailContext

use of org.olat.core.util.mail.MailContext 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 60 with MailContext

use of org.olat.core.util.mail.MailContext 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)

Aggregations

MailBundle (org.olat.core.util.mail.MailBundle)70 MailContext (org.olat.core.util.mail.MailContext)70 MailContextImpl (org.olat.core.util.mail.MailContextImpl)70 MailerResult (org.olat.core.util.mail.MailerResult)66 Identity (org.olat.core.id.Identity)42 MailTemplate (org.olat.core.util.mail.MailTemplate)30 ContactList (org.olat.core.util.mail.ContactList)18 File (java.io.File)14 ArrayList (java.util.ArrayList)8 Property (org.olat.properties.Property)7 Locale (java.util.Locale)6 ContextEntry (org.olat.core.id.context.ContextEntry)6 MailManager (org.olat.core.util.mail.MailManager)6 Translator (org.olat.core.gui.translator.Translator)5 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