use of org.olat.core.util.mail.MailerResult 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());
}
use of org.olat.core.util.mail.MailerResult 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());
}
use of org.olat.core.util.mail.MailerResult in project OpenOLAT by OpenOLAT.
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());
}
use of org.olat.core.util.mail.MailerResult in project OpenOLAT by OpenOLAT.
the class EPShareListController method sendInvitation.
/**
* sends a link to the map to permitted users by email
*
* @param ureq
* @param wrapper
*/
private void sendInvitation(UserRequest ureq, EPSharePolicyWrapper wrapper) {
EPMapPolicy.Type shareType = wrapper.getType();
List<Identity> identitiesToMail = new ArrayList<Identity>();
Invitation invitation = null;
if (shareType.equals(EPMapPolicy.Type.allusers)) {
return;
} else if (shareType.equals(EPMapPolicy.Type.invitation)) {
invitation = wrapper.getInvitation();
} else if (shareType.equals(EPMapPolicy.Type.group)) {
List<BusinessGroup> groups = wrapper.getGroups();
List<Identity> members = businessGroupService.getMembers(groups, GroupRoles.coach.name(), GroupRoles.participant.name());
identitiesToMail.addAll(members);
} else if (shareType.equals(EPMapPolicy.Type.user)) {
identitiesToMail = wrapper.getIdentities();
}
wrapper.setInvitationSend(true);
ContactList contactList = null;
if (identitiesToMail.size() == 1) {
contactList = new ContactList(identitiesToMail.get(0).getUser().getProperty(UserConstants.EMAIL, ureq.getLocale()));
} else {
contactList = new ContactList(translate("map.share.invitation.mail.list"));
}
contactList.addAllIdentites(identitiesToMail);
String busLink = "";
if (invitation != null) {
contactList.add(invitation.getMail());
busLink = getInvitationLink(invitation, map);
} else {
BusinessControlFactory bCF = BusinessControlFactory.getInstance();
ContextEntry mapCE = bCF.createContextEntry(map.getOlatResource());
ArrayList<ContextEntry> cEList = new ArrayList<ContextEntry>();
cEList.add(mapCE);
busLink = bCF.getAsURIString(cEList, true);
}
boolean success = false;
try {
String first = getIdentity().getUser().getProperty(UserConstants.FIRSTNAME, null);
String last = getIdentity().getUser().getProperty(UserConstants.LASTNAME, null);
String sender = first + " " + last;
String[] bodyArgs = new String[] { busLink, sender };
MailContext context = new MailContextImpl(map.getOlatResource(), null, getWindowControl().getBusinessControl().getAsString());
MailBundle bundle = new MailBundle();
bundle.setContext(context);
bundle.setFrom(WebappHelper.getMailConfig("mailReplyTo"));
bundle.setContactList(contactList);
bundle.setContent(translate("map.share.invitation.mail.subject"), translate("map.share.invitation.mail.body", bodyArgs));
MailerResult result = mailManager.sendMessage(bundle);
success = result.isSuccessful();
} catch (Exception e) {
logError("Error on sending invitation mail to contactlist, invalid address.", e);
}
if (success) {
showInfo("map.share.invitation.mail.success");
} else {
showError("map.share.invitation.mail.failure");
}
}
use of org.olat.core.util.mail.MailerResult in project OpenOLAT by OpenOLAT.
the class RegistrationController 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 == emailSendForm) {
if (event == Event.DONE_EVENT) {
// form
// validation
// was ok
wizInfoController.setCurStep(2);
// Email requested for tempkey
// save the fields somewhere
String email = emailSendForm.getEmailAddress();
myContent.contextPut("email", email);
myContent.contextPut("text", translate("step2.reg.text", email));
// ef.setVisible(false);
regarea.setVisible(false);
// get remote address
String ip = ureq.getHttpReq().getRemoteAddr();
String serverpath = Settings.getServerContextPathURI();
String today = DateFormat.getDateInstance(DateFormat.LONG, ureq.getLocale()).format(new Date());
String[] whereFromAttrs = new String[] { serverpath, today, ip };
boolean isMailSent = false;
if (registrationManager.isRegistrationPending(email) || UserManager.getInstance().isEmailAllowed(email)) {
TemporaryKey tk = null;
if (userModule.isEmailUnique()) {
tk = registrationManager.loadTemporaryKeyByEmail(email);
}
if (tk == null)
tk = registrationManager.loadOrCreateTemporaryKeyByEmail(email, ip, RegistrationManager.REGISTRATION);
myContent.contextPut("regKey", tk.getRegistrationKey());
String link = serverpath + "/dmz/registration/index.html?key=" + tk.getRegistrationKey() + "&language=" + i18nModule.getLocaleKey(ureq.getLocale());
String[] bodyAttrs = new String[] { // 0
serverpath, // 1
tk.getRegistrationKey(), // 2
i18nModule.getLocaleKey(ureq.getLocale()), // 3
"<a href=\"" + link + "\">" + link + "</a>" };
String body = translate("reg.body", bodyAttrs);
boolean htmlBody = StringHelper.isHtml(body);
if (!htmlBody) {
body += SEPARATOR + translate("reg.wherefrom", whereFromAttrs);
}
try {
MailBundle bundle = new MailBundle();
bundle.setTo(email);
bundle.setContent(translate("reg.subject"), body);
MailerResult result = mailManager.sendExternMessage(bundle, null, htmlBody);
if (result.isSuccessful()) {
isMailSent = true;
}
} catch (Exception e) {
// nothing to do, emailSent flag is false, errors will be reported to user
}
} else {
// if users with this email address exists, they are informed.
List<Identity> identities = UserManager.getInstance().findIdentitiesByEmail(Collections.singletonList(email));
for (Identity identity : identities) {
String body = translate("login.body", identity.getName()) + SEPARATOR + translate("reg.wherefrom", whereFromAttrs);
try {
MailBundle bundle = new MailBundle();
bundle.setTo(email);
bundle.setContent(translate("login.subject"), body);
MailerResult result = mailManager.sendExternMessage(bundle, null, true);
if (result.isSuccessful()) {
isMailSent = true;
}
} catch (Exception e) {
// nothing to do, emailSent flag is false, errors will be reported to user
}
}
}
if (isMailSent) {
showInfo("email.sent");
} else {
showError("email.notsent");
}
} else if (event == Event.CANCELLED_EVENT) {
fireEvent(ureq, Event.CANCELLED_EVENT);
}
} else if (source == langChooserController) {
if (event == Event.DONE_EVENT) {
wizInfoController.setCurStep(2);
createEmailForm(ureq);
ureq.getUserSession().removeEntry(LocaleNegotiator.NEGOTIATED_LOCALE);
} else if (event == Event.CANCELLED_EVENT) {
ureq.getDispatchResult().setResultingMediaResource(new RedirectMediaResource(Settings.getServerContextPathURI()));
} else if (event instanceof LanguageChangedEvent) {
LanguageChangedEvent lcev = (LanguageChangedEvent) event;
setLocale(lcev.getNewLocale(), true);
myContent.contextPut("text", translate("select.language.description"));
}
} else if (source == registrationForm) {
// Userdata entered
if (event == Event.DONE_EVENT) {
String lang = registrationForm.getLangKey();
// change language if different then current language
if (!lang.equals(i18nModule.getLocaleKey(ureq.getLocale()))) {
Locale loc = i18nManager.getLocaleOrDefault(lang);
ureq.getUserSession().setLocale(loc);
getTranslator().setLocale(loc);
}
wizInfoController.setCurStep(4);
myContent.contextPut("pwdhelp", "");
myContent.contextPut("loginhelp", "");
myContent.contextPut("text", translate("step4.reg.text"));
removeAsListenerAndDispose(disclaimerController);
disclaimerController = new DisclaimerController(ureq, getWindowControl());
listenTo(disclaimerController);
regarea.setContent(disclaimerController.getInitialComponent());
} else if (event == Event.CANCELLED_EVENT) {
ureq.getDispatchResult().setResultingMediaResource(new RedirectMediaResource(Settings.getServerContextPathURI()));
}
} else if (source == disclaimerController) {
if (event == Event.DONE_EVENT) {
// finalize the registration by creating the user
Identity persitedIdentity = createNewUserAfterRegistration();
// display step5
displayFinalStep(persitedIdentity);
} else if (event == Event.CANCELLED_EVENT) {
ureq.getDispatchResult().setResultingMediaResource(new RedirectMediaResource(Settings.getServerContextPathURI()));
}
}
}
Aggregations