Search in sources :

Example 46 with MailTemplate

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

the class DENManager method getRemovedMailTemplate.

/**
 * Generates a mail template for users who were removed from a date
 * @param identity
 * @param event
 * @param trans
 * @return MailTemplate
 */
protected MailTemplate getRemovedMailTemplate(UserRequest ureq, String subjectStr, Translator trans) {
    Identity identity = ureq.getIdentity();
    String[] bodyArgs = new String[] { identity.getUser().getProperty(UserConstants.FIRSTNAME, ureq.getLocale()), identity.getUser().getProperty(UserConstants.LASTNAME, ureq.getLocale()), UserManager.getInstance().getUserDisplayEmail(identity, ureq.getLocale()), "", subjectStr };
    String subject = trans.translate("mail.participants.remove.subject", bodyArgs);
    String body = trans.translate("mail.participants.remove.body", bodyArgs);
    MailTemplate mailTempl = new MailTemplate(subject, body, null) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity ident) {
        // 
        }
    };
    return mailTempl;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) Identity(org.olat.core.id.Identity)

Example 47 with MailTemplate

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

the class DENManager method getAddedMailTemplate.

/**
 * Generates a mail template for users who were added to a date
 * @param identity
 * @param event
 * @param translator
 * @return MailTemplate
 */
protected MailTemplate getAddedMailTemplate(UserRequest ureq, String subjectStr, Translator translator) {
    Identity identity = ureq.getIdentity();
    String[] bodyArgs = new String[] { identity.getUser().getProperty(UserConstants.FIRSTNAME, ureq.getLocale()), identity.getUser().getProperty(UserConstants.LASTNAME, ureq.getLocale()), UserManager.getInstance().getUserDisplayEmail(identity, ureq.getLocale()), "", subjectStr };
    String subject = translator.translate("mail.participants.add.subject", bodyArgs);
    String body = translator.translate("mail.participants.add.body", bodyArgs);
    MailTemplate mailTempl = new MailTemplate(subject, body, null) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity ident) {
        // 
        }
    };
    return mailTempl;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) Identity(org.olat.core.id.Identity)

Example 48 with MailTemplate

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

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 49 with MailTemplate

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

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 50 with MailTemplate

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

the class MailTest method testMailToCcBccForEach.

/**
 * Test for the mail template and the context variable methods
 */
@Test
public void testMailToCcBccForEach() {
    String subject = "For Each Subject: Hello $firstname $lastname";
    String body = "For Each Body: \n\n You ($login) should go to	 \n\n'$coursename' @ $courseURL$login";
    final String coursename = "my course";
    final String courseURL = "http://www.mytrashmail.com/myTrashMail_inbox.aspx?email=";
    MailTemplate template = new MailTemplate(subject, body, null) {

        @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("lastname", user.getProperty(UserConstants.LASTNAME, null));
            context.put("login", identity.getName());
            // Put variables from greater context, eg. course id, group name etc.
            context.put("coursename", coursename);
            context.put("courseURL", courseURL);
        }
    };
    // some recipients data
    List<Identity> recipients = new ArrayList<Identity>();
    recipients.add(id1);
    recipients.add(id2);
    recipients.add(id3);
    Identity recipientCC = id4;
    // tests with / witthout CC and BCC
    MailerResult result = new MailerResult();
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
    result = sendMailAsSeparateMails(null, recipients, recipientCC, template, id6, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, 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) 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