Search in sources :

Example 66 with MailTemplate

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

the class RepositoryMailing method createMailTemplate.

private static MailTemplate createMailTemplate(RepositoryEntry re, Identity actor, String subjectKey, String bodyKey) {
    // build learning resources as list of url as string
    final String reName = re.getDisplayname();
    final String redescription = (StringHelper.containsNonWhitespace(re.getDescription()) ? FilterFactory.getHtmlTagAndDescapingFilter().filter(re.getDescription()) : "");
    final String reUrl = Settings.getServerContextPathURI() + "/url/RepositoryEntry/" + re.getKey();
    // get some data about the actor and fetch the translated subject / body via i18n module
    Locale locale = I18nManager.getInstance().getLocaleOrDefault(actor.getUser().getPreferences().getLanguage());
    String[] bodyArgs = new String[] { actor.getUser().getProperty(UserConstants.FIRSTNAME, null), actor.getUser().getProperty(UserConstants.LASTNAME, null), UserManager.getInstance().getUserDisplayEmail(actor, locale), // 2x for compatibility with old i18m properties
    UserManager.getInstance().getUserDisplayEmail(actor, locale) };
    Translator trans = Util.createPackageTranslator(RepositoryManager.class, locale);
    String subject = trans.translate(subjectKey);
    String body = trans.translate(bodyKey, bodyArgs);
    // create a mail template which all these data
    MailTemplate mailTempl = new MailTemplate(subject, body, null) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
            // Put user variables into velocity context
            User user = identity.getUser();
            context.put("firstname", user.getProperty(UserConstants.FIRSTNAME, null));
            context.put("lastname", user.getProperty(UserConstants.LASTNAME, null));
            context.put("login", UserManager.getInstance().getUserDisplayEmail(user, locale));
            // Put variables from greater context
            context.put("coursename", reName);
            context.put("coursedescription", redescription);
            context.put("courseurl", reUrl);
        }
    };
    return mailTempl;
}
Also used : Locale(java.util.Locale) User(org.olat.core.id.User) Translator(org.olat.core.gui.translator.Translator) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) Identity(org.olat.core.id.Identity)

Example 67 with MailTemplate

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

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

the class MailTest method testMailAttachments.

/**
 * Test for the mail template and the context variable methods
 */
@Test
public void testMailAttachments() {
    String subject = "Subject: Hello $firstname with attachment";
    String body = "Body: \n\n Hey $login, here's a file for you: ";
    // some attachemnts
    File[] attachments = new File[1];
    File file1;
    try {
        System.out.println("MailTest.testMailAttachments Url1=" + MailTest.class.getResource("MailTest.class"));
        file1 = new File(MailTest.class.getResource("MailTest.class").toURI());
        attachments[0] = file1;
    // TODO: cg Properties file is in olat_core.jar and not be lookup as resource (jar:file:...)
    // System.out.println("MailTest.testMailAttachments Url2=" + MailTest.class.getResource("_i18n/LocalStrings_de.properties") );
    // file2 = new File(MailTest.class.getResource("_i18n/LocalStrings_de.properties").toURI());
    // attachments[1] = file2;
    } catch (URISyntaxException e) {
        fail("ups, can't get testfiles from local path: MailTest.class and _i18n/LocalStrings_de.properties");
    }
    MailTemplate template = new MailTemplate(subject, body, attachments) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
            // Put user variables
            User user = identity.getUser();
            context.put("firstname", user.getProperty(UserConstants.FIRSTNAME, null));
            context.put("login", identity.getName());
        }
    };
    // some recipients data
    List<Identity> recipients = new ArrayList<Identity>();
    recipients.add(id1);
    MailerResult result = new MailerResult();
    result = sendMailAsSeparateMails(null, recipients, null, template, id2, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
}
Also used : User(org.olat.core.id.User) MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) Identity(org.olat.core.id.Identity) File(java.io.File) Test(org.junit.Test)

Example 69 with MailTemplate

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

the class MailTest method testMailAttachmentsInvalid.

/**
 * Test for the mail template and the context variable methods
 */
@Test
public void testMailAttachmentsInvalid() {
    String subject = "Subject: Hello $firstname with attachment";
    String body = "Body: \n\n Hey $login, here's a file for you: ";
    // some attachemnts - but no file
    File[] attachments = new File[1];
    MailTemplate template = new MailTemplate(subject, body, attachments) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
            // Put user variables
            User user = identity.getUser();
            context.put("firstname", user.getProperty(UserConstants.FIRSTNAME, null));
            context.put("login", identity.getName());
        }
    };
    // some recipients data
    List<Identity> recipients = new ArrayList<Identity>();
    recipients.add(id1);
    MailerResult result = new MailerResult();
    result = sendMailAsSeparateMails(null, recipients, null, template, id2, null);
    assertEquals(MailerResult.ATTACHMENT_INVALID, result.getReturnCode());
}
Also used : User(org.olat.core.id.User) MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity) File(java.io.File) Test(org.junit.Test)

Example 70 with MailTemplate

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

the class MailTest method testMailerResult.

/**
 * Test for the mailer result codes
 */
@Test
public void testMailerResult() {
    String subject = "MailerResult Subject: Hello everybody";
    String body = "MailerResult Body: \n\n This is just a test";
    MailTemplate template = new MailTemplate(subject, body, null) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
        // nothing to do
        }
    };
    // some recipients data
    Identity illegal1 = JunitTestHelper.createAndPersistIdentityAsUser("illegal1");
    illegal1.getUser().setProperty(UserConstants.EMAIL, "doesnotexisteserlkmlkm@sdf.com");
    Identity illegal2 = JunitTestHelper.createAndPersistIdentityAsUser("illegal2");
    illegal2.getUser().setProperty(UserConstants.EMAIL, "sd@this.domain.does.not.exist.at.all");
    Identity illegal3 = JunitTestHelper.createAndPersistIdentityAsUser("illegal3");
    illegal3.getUser().setProperty(UserConstants.EMAIL, "@ sdf");
    DBFactory.getInstance().intermediateCommit();
    List<Identity> recipients = new ArrayList<Identity>();
    recipients.add(illegal1);
    // if only one recipient: error must be indicated
    MailerResult result = new MailerResult();
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    // mail will bounce back since address does not exist, but sent to local MTA
    // this test is not very good, depends on smtp settings!
    // assertEquals(MailerResult.OK, result.getReturnCode());
    recipients = new ArrayList<Identity>();
    recipients.add(illegal2);
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    // mail will bounce back since address does not exist, but sent to local MTA
    assertEquals(MailerResult.OK, result.getReturnCode());
    recipients = new ArrayList<Identity>();
    recipients.add(illegal3);
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    assertEquals(MailerResult.RECIPIENT_ADDRESS_ERROR, result.getReturnCode());
    // now with one valid and the invalid recipient: should return ok but have
    // one recipient in the failed list
    recipients.add(id1);
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    assertEquals(MailerResult.RECIPIENT_ADDRESS_ERROR, result.getReturnCode());
    assertEquals(1, result.getFailedIdentites().size());
    // valid recipient but invalid sender
    recipients = new ArrayList<Identity>();
    recipients.add(id1);
    result = sendMailAsSeparateMails(null, recipients, null, template, illegal3, null);
    assertEquals(MailerResult.SENDER_ADDRESS_ERROR, result.getReturnCode());
    // invalid cc and bcc but valid to, mus count up the invalid accounts
    recipients = new ArrayList<Identity>();
    recipients.add(id1);
    // first
    recipients.add(illegal3);
    // second
    Identity recipientCC = illegal3;
    result = sendMailAsSeparateMails(null, recipients, recipientCC, template, id6, null);
    // mail will bounce back since address does not exist, but sent to local MTA
    assertEquals(MailerResult.RECIPIENT_ADDRESS_ERROR, result.getReturnCode());
    assertEquals(2, result.getFailedIdentites().size());
}
Also used : MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Aggregations

MailTemplate (org.olat.core.util.mail.MailTemplate)86 Identity (org.olat.core.id.Identity)62 MailerResult (org.olat.core.util.mail.MailerResult)46 MailBundle (org.olat.core.util.mail.MailBundle)32 MailContext (org.olat.core.util.mail.MailContext)30 MailContextImpl (org.olat.core.util.mail.MailContextImpl)30 VelocityContext (org.apache.velocity.VelocityContext)28 ArrayList (java.util.ArrayList)20 List (java.util.List)16 CloseableModalController (org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)14 MailNotificationEditController (org.olat.core.util.mail.MailNotificationEditController)14 User (org.olat.core.id.User)12 Test (org.junit.Test)10 UserRequest (org.olat.core.gui.UserRequest)10 WindowControl (org.olat.core.gui.control.WindowControl)10 MailPackage (org.olat.core.util.mail.MailPackage)8 Property (org.olat.properties.Property)7 File (java.io.File)6 Locale (java.util.Locale)6 TableMultiSelectEvent (org.olat.core.gui.components.table.TableMultiSelectEvent)6