use of com.sendgrid.Mail 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";
}
use of com.sendgrid.Mail 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";
}
use of com.sendgrid.Mail 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";
}
use of com.sendgrid.Mail 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;
}
use of com.sendgrid.Mail 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;
}
Aggregations