Search in sources :

Example 1 with Personalization

use of com.sendgrid.Personalization in project CollectiveOneWebapp by CollectiveOne.

the class EmailService method sendSegmentedPerUserAndInitiativeNotifications.

private String sendSegmentedPerUserAndInitiativeNotifications(List<Notification> notifications, AppUser receiver, Initiative initiative) throws IOException {
    if (env.getProperty("collectiveone.webapp.send-email-enabled").equalsIgnoreCase("true")) {
        if (notifications.size() > 0) {
            Request request = new Request();
            Mail mail = new Mail();
            Email fromEmail = new Email();
            fromEmail.setName(env.getProperty("collectiveone.webapp.from-mail-name"));
            fromEmail.setEmail(env.getProperty("collectiveone.webapp.from-mail"));
            mail.setFrom(fromEmail);
            mail.setSubject("Recent activity in your initiatives");
            Personalization personalization = new Personalization();
            Email toEmail = new Email();
            toEmail.setEmail(receiver.getEmail());
            personalization.addTo(toEmail);
            personalization.addSubstitution("$INITIATIVE_NAME$", initiative.getMeta().getName());
            personalization.addSubstitution("$INITIATIVE_ANCHOR$", getInitiativeAnchor(initiative));
            personalization.addSubstitution("$UNSUSCRIBE_FROM_ALL_HREF$", getUnsuscribeFromAllHref());
            mail.addPersonalization(personalization);
            String rows = "";
            for (Notification notification : notifications) {
                rows += "<tr>" + "<td class=\"avatar-box\">" + "<img class=\"avatar-img\" src=\"" + notification.getActivity().getTriggerUser().getProfile().getPictureUrl() + "\"></img>" + "</td>" + "<td class=\"time-box\">" + getTimeSinceStr(notification.getActivity().getTimestamp()) + "</td>" + "<td class=\"activity-text\">" + getActivityMessage(notification) + "</td>" + "</tr>";
            }
            personalization.addSubstitution("$ROWS$", rows);
            mail.setTemplateId(env.getProperty("collectiveone.webapp.initiative-and-user-activity-template"));
            try {
                request.method = Method.POST;
                request.endpoint = "mail/send";
                request.body = mail.build();
                Response response = sg.api(request);
                if (response.statusCode == 202) {
                    System.out.println("emails sent!");
                    for (Notification notification : notifications) {
                        notification.setEmailState(NotificationEmailState.DELIVERED);
                        notificationRepository.save(notification);
                    }
                    return "success";
                } else {
                    return response.body;
                }
            } catch (IOException ex) {
                throw ex;
            }
        }
    }
    return "success";
}
Also used : Response(com.sendgrid.Response) Mail(com.sendgrid.Mail) Email(com.sendgrid.Email) Personalization(com.sendgrid.Personalization) Request(com.sendgrid.Request) IOException(java.io.IOException)

Example 2 with Personalization

use of com.sendgrid.Personalization in project CollectiveOneWebapp by CollectiveOne.

the class EmailService method prepareWantToContributeEmail.

private Mail prepareWantToContributeEmail(List<WantToContributeNotification> notifications) {
    Mail mail = new Mail();
    Email fromEmail = new Email();
    fromEmail.setName(env.getProperty("collectiveone.webapp.from-mail-name"));
    fromEmail.setEmail(env.getProperty("collectiveone.webapp.from-mail"));
    mail.setFrom(fromEmail);
    mail.setSubject("Request to contribute");
    for (WantToContributeNotification notification : notifications) {
        String toEmailString = notification.getAdmin().getEmail();
        Initiative initiative = notification.getInitiative();
        String acceptRequestUrl = env.getProperty("collectiveone.webapp.baseurl") + "/#/app/inits/" + initiative.getId().toString() + "/people/addMember/" + notification.getUser().getC1Id().toString();
        Personalization personalization = new Personalization();
        Email toEmail = new Email();
        toEmail.setEmail(toEmailString);
        personalization.addTo(toEmail);
        personalization.addSubstitution("$INITIATIVE_ANCHOR$", getInitiativeAnchor(initiative));
        personalization.addSubstitution("$USER_NICKNAME$", notification.getUser().getProfile().getNickname());
        personalization.addSubstitution("$USER_PICTURE$", notification.getUser().getProfile().getPictureUrl());
        personalization.addSubstitution("$USER_EMAIL$", notification.getUser().getEmail());
        personalization.addSubstitution("$ACCEPT_AS_MEMBER_URL$", acceptRequestUrl);
        mail.addPersonalization(personalization);
        notification.setEmailState(NotificationEmailState.DELIVERED);
        wantToContributeRepository.save(notification);
    }
    mail.setTemplateId(env.getProperty("collectiveone.webapp.want-to-contribute-template"));
    return mail;
}
Also used : Mail(com.sendgrid.Mail) Email(com.sendgrid.Email) Personalization(com.sendgrid.Personalization) Initiative(org.collectiveone.modules.initiatives.Initiative)

Example 3 with Personalization

use of com.sendgrid.Personalization in project teammates by TEAMMATES.

the class SendgridService method parseToEmail.

/**
 * {@inheritDoc}
 */
@Override
public Mail parseToEmail(EmailWrapper wrapper) {
    Mail email = new Mail();
    Email sender;
    if (wrapper.getSenderName() == null || wrapper.getSenderName().isEmpty()) {
        sender = new Email(wrapper.getSenderEmail());
    } else {
        sender = new Email(wrapper.getSenderEmail(), wrapper.getSenderName());
    }
    email.setFrom(sender);
    email.setReplyTo(new Email(wrapper.getReplyTo()));
    Personalization personalization = new Personalization();
    personalization.addTo(new Email(wrapper.getRecipient()));
    if (wrapper.getBcc() != null && !wrapper.getBcc().isEmpty()) {
        personalization.addBcc(new Email(wrapper.getBcc()));
    }
    email.addPersonalization(personalization);
    email.setSubject(wrapper.getSubject());
    email.addContent(new Content("text/plain", Jsoup.parse(wrapper.getContent()).text()));
    email.addContent(new Content("text/html", wrapper.getContent()));
    return email;
}
Also used : Mail(com.sendgrid.Mail) Email(com.sendgrid.Email) Personalization(com.sendgrid.Personalization) Content(com.sendgrid.Content)

Example 4 with Personalization

use of com.sendgrid.Personalization in project CollectiveOneWebapp by CollectiveOne.

the class EmailService method basicInitiativePersonalization.

private Personalization basicInitiativePersonalization(Notification notification) {
    String toEmailString = notification.getSubscriber().getUser().getEmail();
    String triggeredByUsername = notification.getActivity().getTriggerUser().getProfile().getNickname();
    String triggerUserPictureUrl = notification.getActivity().getTriggerUser().getProfile().getPictureUrl();
    Initiative initiative = notification.getActivity().getInitiative();
    Personalization personalization = new Personalization();
    Email toEmail = new Email();
    toEmail.setEmail(toEmailString);
    personalization.addTo(toEmail);
    personalization.addSubstitution("$INITIATIVE_NAME$", initiative.getMeta().getName());
    personalization.addSubstitution("$TRIGGER_USER_NICKNAME$", triggeredByUsername);
    personalization.addSubstitution("$TRIGGER_USER_PICTURE$", triggerUserPictureUrl);
    personalization.addSubstitution("$INITIATIVE_ANCHOR$", getInitiativeAnchor(initiative));
    personalization.addSubstitution("$INITIATIVE_PICTURE$", "http://guillaumeladvie.com/wp-content/uploads/2014/04/ouishare.jpg");
    personalization.addSubstitution("$UNSUSCRIBE_FROM_INITIATIVE_HREF$", getUnsuscribeFromInitiativeHref(initiative));
    personalization.addSubstitution("$UNSUSCRIBE_FROM_ALL_HREF$", getUnsuscribeFromAllHref());
    return personalization;
}
Also used : Personalization(com.sendgrid.Personalization) Email(com.sendgrid.Email) Initiative(org.collectiveone.modules.initiatives.Initiative)

Example 5 with Personalization

use of com.sendgrid.Personalization in project CollectiveOneWebapp by CollectiveOne.

the class EmailService method prepareActivitySendNowEmail.

private Mail prepareActivitySendNowEmail(List<Notification> notifications) {
    Mail mail = new Mail();
    Email fromEmail = new Email();
    fromEmail.setName(env.getProperty("collectiveone.webapp.from-mail-name"));
    fromEmail.setEmail(env.getProperty("collectiveone.webapp.from-mail"));
    mail.setFrom(fromEmail);
    mail.setSubject("Activity in CollectiveOne");
    for (Notification notification : notifications) {
        if (notification.getSubscriber().getUser().getEmailNotificationsEnabled()) {
            Personalization personalization = basicInitiativePersonalization(notification);
            String message = getActivityMessage(notification);
            personalization.addSubstitution("$MESSAGE$", message);
            mail.addPersonalization(personalization);
        }
        notification.setEmailState(NotificationEmailState.DELIVERED);
        notificationRepository.save(notification);
    }
    mail.setTemplateId(env.getProperty("collectiveone.webapp.initiative-activity-template"));
    return mail;
}
Also used : Mail(com.sendgrid.Mail) Email(com.sendgrid.Email) Personalization(com.sendgrid.Personalization)

Aggregations

Email (com.sendgrid.Email)5 Personalization (com.sendgrid.Personalization)5 Mail (com.sendgrid.Mail)4 Initiative (org.collectiveone.modules.initiatives.Initiative)2 Content (com.sendgrid.Content)1 Request (com.sendgrid.Request)1 Response (com.sendgrid.Response)1 IOException (java.io.IOException)1