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;
}
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);
}
}
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());
}
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());
}
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());
}
Aggregations