Skip to content

Mandrill

Space: VM Last Updated: 2020-02-14T12:59:41.606Z Author: Rachel Goode (Unlicensed) Version: 2 URL: https://vollers.atlassian.net/wiki/spaces/VM/pages/126681089/Mandrill


What is Mandrill?What is Mandrill?

Mandrill is an email infrastructure service offered as an add-on for Mailchimp that you can use to send personalized, one-to-one e-commerce emails, or automated transactional emails.

[!INFO] Mandrill Knowledge Base

The MandrillApp

The MandrillApp is used to communicate with mandrill via their API and acts as a consumer for it.

[!INFO] https://mandrillapp.com

You have to configure the mandrill app to enable communication with mandrill:

protected EntityApplication configureMandrillApp() {
    MandrillApp mandrillApp = new MandrillApp(MandrillConfig.class);
    mandrillApp.setTemplateClass(MandrillTemplate.class);
    return mandrillApp;
}
protected void sendTemplatedMessage(Controller controller, EntityManager manager, TUser basicUser) {

    User user = (User) basicUser;

    emailLog.debug(String.format(this.getUserMessageFormat(), "about to send email", user.getFullName()));

    TTemplate newsletterTemplate = findNewsletterTemplate(manager);

    if (newsletterTemplate == null) {
        emailLog.warn(String.format(getFailedMessageFormat(), user.getEmail(), user.getFullName(),
                "newsletter template not found"));
        return;
    }

    emailLog.debug(
            String.format(this.getUserMessageFormat(), "a newsletter template found %s", newsletterTemplate));

    NewsletterBuilder builder = getBuilder(controller, manager, user);

    String editorNotes = buildEditorNotes(controller, manager, user);

    String companyAddress = buildCompanyAddress(controller, manager);

    String companyName = CONFIG_VALUE("company.name").get("ERIC");
    String from_email = CONFIG_VALUE("admin.address").get("admin@eri-c.com");

    emailLog.debug(String.format("Building newsletter request: %s, %s", user.getEmail(), user.getFullName()));

    MandrillTemplatedMessageRequest request = builder.setTemplateName(newsletterTemplate.getTemplateName())
            .setFrom_email(from_email).setFrom_name(companyName).setSubject(newsletterTemplate.getSubject())
            .addRecipient(user.getFullName(), user.getEmail()).setTrack_clicks(true).setTrack_opens(true)
            .addMessageMergeVar(user.getEmail(), "EDITORS_NOTES", editorNotes).addRecentlyAddedLots()
            .addRecentlyAskedEric().addMessageMergeVar(user.getEmail(), "COMPANY", companyName)
            .addMessageMergeVar(user.getEmail(), "DESCRIPTION", "Independent research platform")
            .addMessageMergeVar(user.getEmail(), "LIST_ADDRESS_HTML", companyAddress).addCategoryRelated()
            .addTagRelated().addFeaturedProviders().build();

    try {
        SendMessageResponse sendMessageResponse = sendRequest(request);
        SendMessageResponse.Info failed = anyFailed(sendMessageResponse);
        if (failed.item1) {
            emailLog.warn(
                    String.format(getFailedMessageFormat(), user.getEmail(), user.getFullName(), failed.item2));
            if (failed.item2 == MessageResponse.REJECT_REASON.SOFT_BOUNCE.toString()) {
                if (failed.item3 == null && failed.item3.length() <= 0) {
                    emailLog.warn(String.format(getFailedMessageFormat(), user.getEmail(), user.getFullName(),
                            "unabled to reschedule as message id is null or empty"));
                } else {
                    rescheduleTemplatedMessage(user, failed.item3, DateUtils.addHours(new Date(), 1));
                }
            }
        }
    } catch (RequestFailedException e) {
        emailLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFullName(), e.getMessage()));
        emailErrorLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFullName(), e.getMessage()));
    } catch (Exception e1) {
        emailLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFullName(), e1.getMessage()));
    }
}
    ...
    ...
    ...
    ...
    ...
    ...

protected void updateUser(EntityManager manager, TUser user, boolean receiveInformation) {
    if (user != null) {
        user.setReceiveInformation(receiveInformation);
        manager.persist(user);
    }
}

public void disableSender(Controller controller, EntityManager manager, TUser user) {

    if(!isValidEmail(user.getEmail())){
        emailLog.warn(
                String.format(getFailedMessageFormat(),
                        user.getEmail(), user.getFirstName(),
                        "invalid email, unable to add to rejection list"));
        return;
    }

    Controller c = controller;
    if (c == null) {
        c = getController();
    }

    configureRequest(manager);

    RequestRejectsAdd requestRejectsAdd = new RequestRejectsAdd();
    requestRejectsAdd.setEmail(user.getEmail());
    requestRejectsAdd.setComment(String.format("blacklisting %s", user.getEmail()));
    try {
        ResponseRejectsAdd response = rejectsRequest.disableSender(requestRejectsAdd);
    } catch (RequestFailedException e) {
        emailLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFirstName(), e.getMessage()));
        emailErrorLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFirstName(), e.getMessage()));
    } catch (Exception e1) {
        emailLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFirstName(), e1.getMessage()));
    }
}

public void reEnableSender(Controller controller, EntityManager manager, TUser user) {

    if(!isValidEmail(user.getEmail())){
        emailLog.warn(
                String.format(getFailedMessageFormat(),
                        user.getEmail(), user.getFirstName(),
                        "invalid email, unable to remove from rejection list"));
        return;
    }

    Controller c = controller;
    if (c == null) {
        c = getController();
    }

    configureRequest(manager);

    RequestRejectsDelete requestRejectsDelete = new RequestRejectsDelete();
    requestRejectsDelete.setEmail(user.getEmail());
    try {
        ResponseRejectsDelete response = rejectsRequest.reEnableSender(requestRejectsDelete);
    } catch (RequestFailedException e) {
        emailLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFirstName(), e.getMessage()));
        emailErrorLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFirstName(), e.getMessage()));
    } catch (Exception e1) {
        emailLog.error(
                String.format(getFailedMessageFormat(), user.getEmail(), user.getFirstName(), e1.getMessage()));
    }
}
    ...
    ...
    ...
    ...