Search in sources :

Example 31 with MailBundle

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

the class BusinessGroupMailing method sendEmail.

protected static void sendEmail(Identity ureqIdentity, Identity identity, BusinessGroupShort group, MailType type, MailPackage mailing) {
    if (mailing != null && !mailing.isSendEmail()) {
        return;
    }
    if (mailing == null) {
        BaseSecurity securityManager = CoreSpringFactory.getImpl(BaseSecurity.class);
        BusinessGroupModule groupModule = CoreSpringFactory.getImpl(BusinessGroupModule.class);
        Roles ureqRoles = securityManager.getRoles(ureqIdentity);
        if (!groupModule.isMandatoryEnrolmentEmail(ureqRoles)) {
            return;
        }
    }
    MailTemplate template = mailing == null ? null : mailing.getTemplate();
    if (mailing == null || mailing.getTemplate() == null) {
        // booking by myself
        if (type != null && type == MailType.addParticipant && ureqIdentity != null && ureqIdentity.equals(identity)) {
            template = BGMailHelper.createAddMyselfMailTemplate(group, ureqIdentity);
        } else {
            template = getDefaultTemplate(type, group, ureqIdentity);
        }
    } else if (group != null && template.getContext() != null && needTemplateEnhancement(template)) {
        BusinessGroupService businessGroupService = CoreSpringFactory.getImpl(BusinessGroupService.class);
        List<RepositoryEntryShort> repoEntries = businessGroupService.findShortRepositoryEntries(Collections.singletonList(group), 0, -1);
        template = new MailTemplateDelegate(template, group, repoEntries);
    }
    MailContext context = mailing == null ? null : mailing.getContext();
    if (context == null) {
        context = new MailContextImpl(null, null, "[BusinessGroup:" + group.getKey() + "]");
    }
    MailerResult result = new MailerResult();
    String metaId = mailing != null ? mailing.getUuid() : null;
    MailManager mailService = CoreSpringFactory.getImpl(MailManager.class);
    MailBundle bundle = mailService.makeMailBundle(context, identity, template, ureqIdentity, metaId, result);
    if (bundle != null) {
        mailService.sendMessage(bundle);
    }
    if (mailing != null) {
        mailing.appendResult(result);
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) BusinessGroupModule(org.olat.group.BusinessGroupModule) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) Roles(org.olat.core.id.Roles) BaseSecurity(org.olat.basesecurity.BaseSecurity) BusinessGroupService(org.olat.group.BusinessGroupService) MailTemplate(org.olat.core.util.mail.MailTemplate) MailManager(org.olat.core.util.mail.MailManager) List(java.util.List) MailBundle(org.olat.core.util.mail.MailBundle)

Example 32 with MailBundle

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

the class BusinessGroupServiceImpl method deleteBusinessGroupWithMail.

@Override
public MailerResult deleteBusinessGroupWithMail(BusinessGroup businessGroupTodelete, String businessPath, Identity deletedBy, Locale locale) {
    List<Identity> users = businessGroupRelationDAO.getMembers(businessGroupTodelete, GroupRoles.coach.name(), GroupRoles.participant.name(), GroupRoles.waiting.name());
    // now delete the group first
    deleteBusinessGroup(businessGroupTodelete);
    dbInstance.commit();
    // finally send email
    MailTemplate mailTemplate = BGMailHelper.createDeleteGroupMailTemplate(businessGroupTodelete, deletedBy);
    if (mailTemplate != null) {
        String metaId = UUID.randomUUID().toString();
        MailContext context = new MailContextImpl(businessPath);
        MailerResult result = new MailerResult();
        MailBundle[] bundles = mailManager.makeMailBundles(context, users, mailTemplate, null, metaId, result);
        result.append(mailManager.sendMessage(bundles));
        return result;
    }
    return null;
}
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) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 33 with MailBundle

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

the class PwChangeController method sendEmail.

private TemporaryKey sendEmail(UserRequest ureq, Identity identity) {
    if (!userModule.isPwdChangeAllowed(identity)) {
        getWindowControl().setWarning(translate("password.cantchange"));
        return null;
    }
    Preferences prefs = identity.getUser().getPreferences();
    Locale locale = i18nManager.getLocaleOrDefault(prefs.getLanguage());
    ureq.getUserSession().setLocale(locale);
    myContent.contextPut("locale", locale);
    Translator userTrans = Util.createPackageTranslator(PwChangeController.class, locale);
    String emailAdress = identity.getUser().getProperty(UserConstants.EMAIL, locale);
    if (!StringHelper.containsNonWhitespace(emailAdress)) {
        // for security reason, don't show an error, go simply to the next step
        stepSendEmailConfiration();
        return null;
    }
    // get remote address
    String ip = ureq.getHttpReq().getRemoteAddr();
    String today = DateFormat.getDateInstance(DateFormat.LONG, ureq.getLocale()).format(new Date());
    // mailer configuration
    String serverpath = Settings.getServerContextPathURI();
    TemporaryKey tk = rm.createAndDeleteOldTemporaryKey(identity.getKey(), emailAdress, ip, RegistrationManager.PW_CHANGE);
    myContent.contextPut("pwKey", tk.getRegistrationKey());
    StringBuilder body = new StringBuilder();
    body.append("<style>").append(".o_footer {background: #FAFAFA; border: 1px solid #eee; border-radius: 5px; padding: 1em; margin: 1em;}").append(".o_body {background: #FAFAFA; padding: 1em; margin: 1em;}").append("</style>").append("<div class='o_body'>").append(userTrans.translate("pwchange.headline")).append(userTrans.translate("pwchange.intro", new String[] { identity.getName() })).append(userTrans.translate("pwchange.body", new String[] { serverpath, tk.getRegistrationKey(), i18nModule.getLocaleKey(ureq.getLocale()) })).append(userTrans.translate("pwchange.body.alt", new String[] { serverpath, tk.getRegistrationKey(), i18nModule.getLocaleKey(ureq.getLocale()) })).append("</div>").append("<div class='o_footer'>").append(userTrans.translate("reg.wherefrom", new String[] { serverpath, today, ip })).append("</div>");
    MailBundle bundle = new MailBundle();
    bundle.setToId(identity);
    bundle.setContent(userTrans.translate("pwchange.subject"), body.toString());
    MailerResult result = mailManager.sendExternMessage(bundle, null, false);
    if (result.getReturnCode() == MailerResult.OK) {
        getWindowControl().setInfo(translate("email.sent"));
    }
    stepSendEmailConfiration();
    return tk;
}
Also used : Locale(java.util.Locale) Translator(org.olat.core.gui.translator.Translator) MailerResult(org.olat.core.util.mail.MailerResult) Preferences(org.olat.core.id.Preferences) MailBundle(org.olat.core.util.mail.MailBundle) Date(java.util.Date)

Example 34 with MailBundle

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

the class RegistrationWebService method register.

/**
 * Register with the specified email
 * @response.representation.200.doc Registration successful
 * @response.representation.304.doc Already registered, HTTP-Header location set to redirect
 * @response.representation.400.doc Email address not allowed
 * @param email The email address
 * @param request The HTTP Request
 * @return
 */
@PUT
public Response register(@QueryParam("email") String email, @Context HttpServletRequest request) {
    if (!CoreSpringFactory.getImpl(RegistrationModule.class).isSelfRegistrationEnabled()) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    ResponseBuilder response;
    Locale locale = getLocale(request);
    Translator translator = getTranslator(locale);
    MailManager mailM = CoreSpringFactory.getImpl(MailManager.class);
    UserManager userManager = UserManager.getInstance();
    RegistrationManager rm = CoreSpringFactory.getImpl(RegistrationManager.class);
    boolean foundUser = userManager.findUniqueIdentityByEmail(email) != null;
    boolean noNewUserWithEmail = !userManager.isEmailAllowed(email);
    String serverpath = Settings.getServerContextPathURI();
    if (foundUser && noNewUserWithEmail) {
        // redirect
        URI redirectUri = UriBuilder.fromUri(Settings.getServerContextPathURI()).build();
        response = Response.ok().status(Status.NOT_MODIFIED).location(redirectUri);
    } else if (userManager.isEmailAllowed(email)) {
        String ip = request.getRemoteAddr();
        TemporaryKey tk = null;
        UserModule userModule = CoreSpringFactory.getImpl(UserModule.class);
        if (userModule.isEmailUnique()) {
            tk = rm.loadTemporaryKeyByEmail(email);
        }
        if (tk == null) {
            tk = rm.loadOrCreateTemporaryKeyByEmail(email, ip, RegistrationManager.REGISTRATION);
        }
        String today = DateFormat.getDateInstance(DateFormat.LONG, locale).format(new Date());
        String[] bodyAttrs = new String[] { serverpath, tk.getRegistrationKey(), CoreSpringFactory.getImpl(I18nModule.class).getLocaleKey(locale) };
        String[] whereFromAttrs = new String[] { serverpath, today, ip };
        String body = translator.translate("reg.body", bodyAttrs) + SEPARATOR + translator.translate("reg.wherefrom", whereFromAttrs);
        try {
            MailBundle bundle = new MailBundle();
            bundle.setTo(email);
            bundle.setContent(translator.translate("reg.subject"), body);
            MailerResult result = mailM.sendExternMessage(bundle, null, true);
            if (result.isSuccessful()) {
                response = Response.ok();
            } else {
                response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
            log.error("", e);
        }
    } else {
        response = Response.serverError().status(Status.BAD_REQUEST);
    }
    return response.build();
}
Also used : Locale(java.util.Locale) RestSecurityHelper.getLocale(org.olat.restapi.security.RestSecurityHelper.getLocale) RegistrationManager(org.olat.registration.RegistrationManager) MailerResult(org.olat.core.util.mail.MailerResult) TemporaryKey(org.olat.registration.TemporaryKey) URI(java.net.URI) Date(java.util.Date) Translator(org.olat.core.gui.translator.Translator) UserManager(org.olat.user.UserManager) MailManager(org.olat.core.util.mail.MailManager) UserModule(org.olat.user.UserModule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) MailBundle(org.olat.core.util.mail.MailBundle) PUT(javax.ws.rs.PUT)

Example 35 with MailBundle

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

the class MailManagerTest method testSend_BCC.

@Test
public void testSend_BCC() {
    // send a mail to three ids
    Identity fromId = JunitTestHelper.createAndPersistIdentityAsUser("mail-7-" + UUID.randomUUID().toString());
    Identity toId_1 = JunitTestHelper.createAndPersistIdentityAsUser("mail-8-" + UUID.randomUUID().toString());
    Identity toId_2 = JunitTestHelper.createAndPersistIdentityAsUser("mail-9-" + UUID.randomUUID().toString());
    Identity toId_3 = JunitTestHelper.createAndPersistIdentityAsUser("mail-10-" + UUID.randomUUID().toString());
    ContactList ccs = new ContactList("unit-test-cc");
    ccs.add(toId_1);
    ccs.add(toId_2);
    ccs.add(toId_3);
    MailBundle bundle = new MailBundle();
    bundle.setFromId(fromId);
    bundle.setContactList(ccs);
    bundle.setContent("Hello ccList", "Content of ccList");
    MailerResult result = mailManager.sendMessage(bundle);
    Assert.assertNotNull(result);
    Assert.assertEquals(MailerResult.OK, result.getReturnCode());
    dbInstance.commitAndCloseSession();
    // retrieve the inbox of 1
    List<DBMailLight> incomingsMails = mailManager.getInbox(toId_1, Boolean.TRUE, Boolean.TRUE, null, 0, -1);
    Assert.assertNotNull(incomingsMails);
    Assert.assertEquals(1, incomingsMails.size());
    DBMailLight incomingMail = incomingsMails.get(0);
    Assert.assertNotNull(incomingMail);
    Assert.assertEquals("Hello ccList", incomingMail.getSubject());
    // retrieve the inbox of 2
    List<DBMailLight> incomingsMails_2 = mailManager.getInbox(toId_2, Boolean.TRUE, Boolean.TRUE, null, 0, -1);
    Assert.assertNotNull(incomingsMails_2);
    Assert.assertEquals(1, incomingsMails_2.size());
    Assert.assertEquals(incomingMail, incomingsMails_2.get(0));
    // retrieve the inbox of 3
    List<DBMailLight> incomingsMails_3 = mailManager.getInbox(toId_2, Boolean.TRUE, Boolean.TRUE, null, 0, -1);
    Assert.assertNotNull(incomingsMails_3);
    Assert.assertEquals(1, incomingsMails_3.size());
    Assert.assertEquals(incomingMail, incomingsMails_3.get(0));
}
Also used : 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) DBMailLight(org.olat.core.util.mail.model.DBMailLight) Test(org.junit.Test)

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