Search in sources :

Example 6 with MailerResult

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

the class MailTest method testMailToCcBccTogether.

/**
 * Test for the mail template and the context variable methods
 */
@Test
public void testMailToCcBccTogether() {
    String subject = "Together Subject: Hello everybody";
    String body = "Together Body: \n\n You should go to \n\n'$coursename' @ $courseURL";
    final String coursename = "my course";
    final String courseURL = "http://www.mytrashmail.com/";
    MailTemplate template = new MailTemplate(subject, body, null) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
            // identity is null in this mode - template parsed only once not for
            // everybody
            // 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 : 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)

Example 7 with MailerResult

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

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 8 with MailerResult

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

the class GroupController method doAddIdentitiesToGroup.

/**
 * Add users from the identites array to the group if they are not guest users
 * and not already in the group
 *
 * @param ureq
 * @param choosenIdentities
 */
private void doAddIdentitiesToGroup(UserRequest ureq, List<Identity> choosenIdentities, MailTemplate mailTemplate) {
    // additional security check
    if (!mayModifyMembers)
        throw new AssertException("not allowed to add member!");
    IdentitiesAddEvent identitiesAddedEvent = new IdentitiesAddEvent(choosenIdentities);
    // process workflow to BusinessGroupManager via BusinessGroupEditController
    fireEvent(ureq, identitiesAddedEvent);
    if (!identitiesAddedEvent.getAddedIdentities().isEmpty()) {
        // update table model
        reloadData();
    }
    // build info message for identities which could be added.
    StringBuilder infoMessage = new StringBuilder();
    for (Identity identity : identitiesAddedEvent.getIdentitiesWithoutPermission()) {
        infoMessage.append(translate("msg.isingroupanonymous", userManager.getUserDisplayName(identity))).append("<br />");
    }
    for (Identity identity : identitiesAddedEvent.getIdentitiesAlreadyInGroup()) {
        infoMessage.append(translate("msg.subjectalreadyingroup", userManager.getUserDisplayName(identity))).append("<br />");
    }
    // send the notification mail fro added users
    StringBuilder errorMessage = new StringBuilder();
    if (mailTemplate != null) {
        // means no sender in footer
        Identity sender = null;
        if (showSenderInAddMailFooter) {
            sender = ureq.getIdentity();
        }
        String metaId = UUID.randomUUID().toString();
        MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
        MailerResult result = new MailerResult();
        MailBundle[] bundles = mailManager.makeMailBundles(context, identitiesAddedEvent.getAddedIdentities(), mailTemplate, sender, metaId, result);
        result.append(mailManager.sendMessage(bundles));
        if (mailTemplate.getCpfrom()) {
            MailBundle ccBundle = mailManager.makeMailBundle(context, ureq.getIdentity(), mailTemplate, sender, metaId, result);
            result.append(mailManager.sendMessage(ccBundle));
        }
        MailHelper.appendErrorsAndWarnings(result, errorMessage, infoMessage, ureq.getUserSession().getRoles().isOLATAdmin(), ureq.getLocale());
    }
    // report any errors on screen
    if (infoMessage.length() > 0)
        getWindowControl().setWarning(infoMessage.toString());
    if (errorMessage.length() > 0)
        getWindowControl().setError(errorMessage.toString());
}
Also used : AssertException(org.olat.core.logging.AssertException) MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) IdentitiesAddEvent(org.olat.admin.securitygroup.gui.IdentitiesAddEvent) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Example 9 with MailerResult

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

the class ProjectGroupController method handleCandidateGroupEvent.

private void handleCandidateGroupEvent(UserRequest urequest, Event event) {
    if (event instanceof IdentitiesAddEvent) {
        IdentitiesAddEvent identitiesAddEvent = (IdentitiesAddEvent) event;
        List<Identity> addedIdentities = projectGroupManager.addCandidates(identitiesAddEvent.getAddIdentities(), project);
        identitiesAddEvent.setIdentitiesAddedEvent(addedIdentities);
        fireEvent(urequest, Event.CHANGED_EVENT);
    } else if (event instanceof IdentitiesRemoveEvent) {
        projectGroupManager.removeCandidates(((IdentitiesRemoveEvent) event).getRemovedIdentities(), project);
        fireEvent(urequest, Event.CHANGED_EVENT);
    } else if (event instanceof IdentitiesMoveEvent) {
        final IdentitiesMoveEvent identitiesMoveEvent = (IdentitiesMoveEvent) event;
        // OLAT-6342: check identity not in group first!
        List<Identity> moveIdents = identitiesMoveEvent.getChosenIdentities();
        BusinessGroupAddResponse response = projectGroupManager.acceptCandidates(moveIdents, project, urequest.getIdentity(), projectBrokerModuleConfiguration.isAutoSignOut(), projectBrokerModuleConfiguration.isAcceptSelectionManually());
        identitiesMoveEvent.setMovedIdentities(response.getAddedIdentities());
        identitiesMoveEvent.setNotMovedIdentities(response.getIdentitiesAlreadyInGroup());
        // send mail for all of them
        MailTemplate mailTemplate = identitiesMoveEvent.getMailTemplate();
        if (mailTemplate != null) {
            MailContext context = new MailContextImpl(getWindowControl().getBusinessControl().getAsString());
            String metaId = UUID.randomUUID().toString().replace("-", "");
            MailerResult result = new MailerResult();
            MailBundle[] bundles = mailManager.makeMailBundles(context, identitiesMoveEvent.getMovedIdentities(), mailTemplate, null, metaId, result);
            result.append(mailManager.sendMessage(bundles));
            if (mailTemplate.getCpfrom()) {
                MailBundle ccBundle = mailManager.makeMailBundle(context, urequest.getIdentity(), mailTemplate, null, metaId, result);
                result.append(mailManager.sendMessage(ccBundle));
            }
            MailHelper.printErrorsAndWarnings(result, getWindowControl(), urequest.getUserSession().getRoles().isOLATAdmin(), urequest.getLocale());
        }
        fireEvent(urequest, Event.CHANGED_EVENT);
        // Participant and waiting-list were changed => reload both
        projectMemberController.reloadData();
        // Do only reload data in case of IdentitiesMoveEvent (IdentitiesAddEvent and reload data resulting in doublicate values)
        projectCandidatesController.reloadData();
    }
}
Also used : MailContextImpl(org.olat.core.util.mail.MailContextImpl) MailContext(org.olat.core.util.mail.MailContext) MailerResult(org.olat.core.util.mail.MailerResult) IdentitiesRemoveEvent(org.olat.admin.securitygroup.gui.IdentitiesRemoveEvent) MailTemplate(org.olat.core.util.mail.MailTemplate) IdentitiesAddEvent(org.olat.admin.securitygroup.gui.IdentitiesAddEvent) BusinessGroupAddResponse(org.olat.group.BusinessGroupAddResponse) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle) IdentitiesMoveEvent(org.olat.admin.securitygroup.gui.IdentitiesMoveEvent)

Example 10 with MailerResult

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

the class ProjectBrokerMailerImpl method sendEmailProjectChanged.

private MailerResult sendEmailProjectChanged(List<Identity> group, Identity changer, Project project, String subject, String body, Locale locale) {
    MailTemplate enrolledMailTemplate = this.createProjectChangeMailTemplate(project, changer, subject, body, locale);
    // loop over all project manger
    StringBuilder identityNames = new StringBuilder();
    for (Identity identity : group) {
        if (identityNames.length() > 0)
            identityNames.append(",");
        identityNames.append(identity.getName());
    }
    MailContext context = new MailContextImpl(project.getProjectBroker(), null, null);
    MailerResult result = new MailerResult();
    MailBundle[] bundles = mailManager.makeMailBundles(context, group, enrolledMailTemplate, null, null, result);
    result.append(mailManager.sendMessage(bundles));
    log.audit("ProjectBroker: sendEmailToGroup: identities=" + identityNames.toString() + " , mailerResult.returnCode=" + result.getReturnCode());
    return 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) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle)

Aggregations

MailerResult (org.olat.core.util.mail.MailerResult)140 MailBundle (org.olat.core.util.mail.MailBundle)100 Identity (org.olat.core.id.Identity)82 MailContext (org.olat.core.util.mail.MailContext)66 MailContextImpl (org.olat.core.util.mail.MailContextImpl)66 MailTemplate (org.olat.core.util.mail.MailTemplate)46 Test (org.junit.Test)26 File (java.io.File)24 ArrayList (java.util.ArrayList)20 ContactList (org.olat.core.util.mail.ContactList)20 Locale (java.util.Locale)16 Translator (org.olat.core.gui.translator.Translator)16 VelocityContext (org.apache.velocity.VelocityContext)14 DBMailLight (org.olat.core.util.mail.model.DBMailLight)14 Date (java.util.Date)12 MailPackage (org.olat.core.util.mail.MailPackage)12 List (java.util.List)8 User (org.olat.core.id.User)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 Property (org.olat.properties.Property)7