Search in sources :

Example 1 with Request

use of com.sendgrid.Request 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 Request

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

the class EmailService method sendSegmentedPerActivityNotifications.

private String sendSegmentedPerActivityNotifications(List<Notification> notifications) throws IOException {
    if (env.getProperty("collectiveone.webapp.send-email-enabled").equalsIgnoreCase("true")) {
        if (notifications.size() > 0) {
            Request request = new Request();
            Mail mail = prepareActivitySendNowEmail(notifications);
            if (mail != null) {
                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!");
                        return "success";
                    } else {
                        return response.body;
                    }
                } catch (IOException ex) {
                    throw ex;
                }
            } else {
                return "error bulding email";
            }
        }
    }
    /* if email is disabled */
    return "success";
}
Also used : Response(com.sendgrid.Response) Mail(com.sendgrid.Mail) Request(com.sendgrid.Request) IOException(java.io.IOException)

Example 3 with Request

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

the class EmailService method sendWantToContributeNotifications.

public String sendWantToContributeNotifications(List<WantToContributeNotification> notifications) throws IOException {
    if (env.getProperty("collectiveone.webapp.send-email-enabled").equalsIgnoreCase("true")) {
        if (notifications.size() > 0) {
            Request request = new Request();
            Mail mail = prepareWantToContributeEmail(notifications);
            if (mail != null) {
                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!");
                        return "success";
                    } else {
                        return response.body;
                    }
                } catch (IOException ex) {
                    throw ex;
                }
            } else {
                return "error bulding email";
            }
        }
    }
    /* if email is disabled */
    return "success";
}
Also used : Response(com.sendgrid.Response) Mail(com.sendgrid.Mail) Request(com.sendgrid.Request) IOException(java.io.IOException)

Example 4 with Request

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

the class SendgridService method sendEmailWithService.

@Override
protected void sendEmailWithService(EmailWrapper wrapper) throws IOException {
    Mail email = parseToEmail(wrapper);
    SendGrid sendgrid = new SendGrid(Config.SENDGRID_APIKEY);
    Request request = new Request();
    request.setMethod(Method.POST);
    request.setEndpoint("mail/send");
    request.setBody(email.build());
    Response response = sendgrid.api(request);
    if (isNotSuccessStatus(response.getStatusCode())) {
        log.severe("Email failed to send: " + response.getBody());
    }
}
Also used : Response(com.sendgrid.Response) SendGrid(com.sendgrid.SendGrid) Mail(com.sendgrid.Mail) Request(com.sendgrid.Request)

Aggregations

Mail (com.sendgrid.Mail)4 Request (com.sendgrid.Request)4 Response (com.sendgrid.Response)4 IOException (java.io.IOException)3 Email (com.sendgrid.Email)1 Personalization (com.sendgrid.Personalization)1 SendGrid (com.sendgrid.SendGrid)1