Skip to content

Commit

Permalink
Fixes issue with emails not being sent (#178)
Browse files Browse the repository at this point in the history
* rfr

* removes try
  • Loading branch information
GrantComm authored Aug 7, 2020
1 parent fb42253 commit 95c552e
Showing 16 changed files with 208 additions and 190 deletions.
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@
import com.google.sps.data.ScheduledInterviewDao;
import com.google.sps.data.SendgridEmailSender;
import com.google.sps.utils.EmailUtils;
import com.google.sps.utils.SendgridEmailUtils;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
@@ -50,7 +49,6 @@ public class IntervieweeFeedbackServlet extends HttpServlet {
private ScheduledInterviewDao scheduledInterviewDao;
private PersonDao personDao;
private EmailSender emailSender;
private EmailUtils emailUtils;
static final Email sender = new Email("[email protected]");
private Path emailsPath =
Paths.get(
@@ -64,22 +62,14 @@ public void init() {
} catch (IOException e) {
throw new RuntimeException(e);
}
init(
new DatastoreScheduledInterviewDao(),
new DatastorePersonDao(),
emailSender,
new SendgridEmailUtils());
init(new DatastoreScheduledInterviewDao(), new DatastorePersonDao(), emailSender);
}

public void init(
ScheduledInterviewDao scheduledInterviewDao,
PersonDao personDao,
EmailSender emailSender,
EmailUtils emailUtils) {
ScheduledInterviewDao scheduledInterviewDao, PersonDao personDao, EmailSender emailSender) {
this.scheduledInterviewDao = scheduledInterviewDao;
this.personDao = personDao;
this.emailSender = emailSender;
this.emailUtils = emailUtils;
}

@Override
@@ -148,8 +138,8 @@ private void sendFeedback(String intervieweeEmail, HashMap<String, String> answe
String subject = "Your Interviewer has submitted feedback for your interview!";
Email recipient = new Email(intervieweeEmail);
String contentString =
emailUtils.fileContentToString(emailsPath + "/feedbackToInterviewee.txt");
Content content = new Content("text/plain", emailUtils.replaceAllPairs(answers, contentString));
EmailUtils.fileContentToString(emailsPath + "/feedbackToInterviewee.txt");
Content content = new Content("text/plain", EmailUtils.replaceAllPairs(answers, contentString));
emailSender.sendEmail(recipient, subject, content);
}
}
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@
import com.google.sps.data.ScheduledInterviewDao;
import com.google.sps.data.SendgridEmailSender;
import com.google.sps.utils.EmailUtils;
import com.google.sps.utils.SendgridEmailUtils;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
@@ -50,7 +49,6 @@ public class InterviewerFeedbackServlet extends HttpServlet {
private ScheduledInterviewDao scheduledInterviewDao;
private PersonDao personDao;
private EmailSender emailSender;
private EmailUtils emailUtils;
static final Email sender = new Email("[email protected]");
private Path emailsPath =
Paths.get(
@@ -64,22 +62,14 @@ public void init() {
} catch (IOException e) {
throw new RuntimeException(e);
}
init(
new DatastoreScheduledInterviewDao(),
new DatastorePersonDao(),
emailSender,
new SendgridEmailUtils());
init(new DatastoreScheduledInterviewDao(), new DatastorePersonDao(), emailSender);
}

public void init(
ScheduledInterviewDao scheduledInterviewDao,
PersonDao personDao,
EmailSender emailSender,
EmailUtils emailUtils) {
ScheduledInterviewDao scheduledInterviewDao, PersonDao personDao, EmailSender emailSender) {
this.scheduledInterviewDao = scheduledInterviewDao;
this.personDao = personDao;
this.emailSender = emailSender;
this.emailUtils = emailUtils;
}

@Override
@@ -147,8 +137,8 @@ private void sendFeedback(String interviewerEmail, HashMap<String, String> answe
String subject = "Your Interviewee has submitted feedback for your interview!";
Email recipient = new Email(interviewerEmail);
String contentString =
emailUtils.fileContentToString(emailsPath + "/feedbackToInterviewer.txt");
Content content = new Content("text/plain", emailUtils.replaceAllPairs(answers, contentString));
EmailUtils.fileContentToString(emailsPath + "/feedbackToInterviewer.txt");
Content content = new Content("text/plain", EmailUtils.replaceAllPairs(answers, contentString));
emailSender.sendEmail(recipient, subject, content);
}
}
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@
import com.google.sps.data.SendgridEmailSender;
import com.google.sps.data.TimeRange;
import com.google.sps.utils.EmailUtils;
import com.google.sps.utils.SendgridEmailUtils;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
@@ -81,12 +80,8 @@ public class ScheduledInterviewServlet extends HttpServlet {
private EmailSender emailSender;
private CalendarAccess calendarAccess;
private Calendar service;
private EmailUtils emailUtils;
private final UserService userService = UserServiceFactory.getUserService();
static final Email sender = new Email("[email protected]");
private Path emailsPath =
Paths.get(
System.getProperty("user.home") + "/InterviewMe/src/main/resources/templates/email");

@Override
public void init() {
@@ -113,23 +108,20 @@ public void init() {
new DatastoreAvailabilityDao(),
new DatastorePersonDao(),
calendar,
emailSender,
new SendgridEmailUtils());
emailSender);
}

public void init(
ScheduledInterviewDao scheduledInterviewDao,
AvailabilityDao availabilityDao,
PersonDao personDao,
CalendarAccess calendarAccess,
EmailSender emailSender,
EmailUtils emailUtils) {
EmailSender emailSender) {
this.scheduledInterviewDao = scheduledInterviewDao;
this.availabilityDao = availabilityDao;
this.personDao = personDao;
this.calendarAccess = calendarAccess;
this.emailSender = emailSender;
this.emailUtils = emailUtils;
}

// Gets the current user's email and returns the ScheduledInterviews for that person.
@@ -447,22 +439,21 @@ private void sendParticipantEmail(
}

String subject = "You have been requested to conduct a mock interview!";
String contentString =
emailUtils.fileContentToString(emailsPath + "/NewInterview_Interviewer.txt");
String contentString = EmailUtils.fileContentToString("NewInterview_Interviewer.txt");

if (participantId.equals(scheduledInterview.intervieweeId())) {
subject = "You have been registered for a mock interview!";
contentString = emailUtils.fileContentToString(emailsPath + "/NewInterview_Interviewee.txt");
contentString = EmailUtils.fileContentToString("NewInterview_Interviewee.txt");
}

if (participantId.equals(scheduledInterview.shadowId())) {
subject = "You have been registered for a mock interview!";
contentString = emailUtils.fileContentToString(emailsPath + "/NewInterview_Shadow.txt");
contentString = EmailUtils.fileContentToString("NewInterview_Shadow.txt");
}

Email recipient = new Email(recipientEmail);
Content content =
new Content("text/plain", emailUtils.replaceAllPairs(emailedDetails, contentString));
new Content("text/plain", EmailUtils.replaceAllPairs(emailedDetails, contentString));
emailSender.sendEmail(recipient, subject, content);
}
// Formats the position string that is sent in an email. For example SOFTWARE_ENGINEER -> Software
@@ -471,7 +462,7 @@ private static String formatPositionString(String str) {
String splitString[] = str.split("_", 0);
String formattedPositionString = "";
for (String s : splitString) {
formattedPositionString += s.substring(0, 1) + s.substring(1).toLowerCase();
formattedPositionString += s.substring(0, 1) + s.substring(1).toLowerCase() + " ";
}
return formattedPositionString.trim();
}
46 changes: 40 additions & 6 deletions src/main/java/com/google/sps/utils/EmailUtils.java
Original file line number Diff line number Diff line change
@@ -14,18 +14,36 @@

package com.google.sps.utils;

// using SendGrid's Java Library
// https://github.com/sendgrid/sendgrid-java
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.SendGrid;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import java.util.Map;
import java.util.HashMap;
import java.lang.ClassLoader;
import java.net.URI;
import java.net.URL;
import java.net.URISyntaxException;

/** EmailUtils includes the basic methods used when formatting emails. */
public interface EmailUtils {
// Used to format contents of an email
public class EmailUtils {

// Returns the contents of the file specified at filePath as a String. Useful for converting
// predefined email templates to text.
public String fileContentToString(String filePath) throws IOException;
public static String fileContentToString(String fileName) throws IOException {
StringBuilder contentBuilder = new StringBuilder();
Stream<String> stream =
Files.lines(Paths.get(getEmailTemplateResource(fileName)), StandardCharsets.UTF_8);
stream.forEach(s -> contentBuilder.append(s).append("\n"));
return contentBuilder.toString();
}

/**
* Modifies and returns @param str. Replaces all occurences in @param str of each key in @param
@@ -34,5 +52,21 @@ public interface EmailUtils {
// Ex. str = "You will be mock interviewing {{interviewee_full_name}} on {{formatted_date}}."
// toReplace = { ("{{interviewee_full_name}}","Tess"), ("{{formatted_date}}", "June 6, 2022") }
// Returned: "You will be mock interviewing Tess on June 6, 2022."
public String replaceAllPairs(HashMap<String, String> toReplace, String str);
public static String replaceAllPairs(HashMap<String, String> toReplace, String str) {
for (Map.Entry<String, String> entry : toReplace.entrySet()) {
str = str.replace(entry.getKey(), entry.getValue());
}
return str;
}

private static URI getEmailTemplateResource(String fileName) {
URL resource = EmailUtils.class.getResource("/templates/email/" + fileName);
URI result;
try {
result = resource.toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return result;
}
}
55 changes: 0 additions & 55 deletions src/main/java/com/google/sps/utils/FakeEmailUtils.java

This file was deleted.

59 changes: 0 additions & 59 deletions src/main/java/com/google/sps/utils/SendgridEmailUtils.java

This file was deleted.

Loading

0 comments on commit 95c552e

Please sign in to comment.