Skip to content

Commit

Permalink
Merge pull request #55 from GoogleCloudPlatform/update-mailgun
Browse files Browse the repository at this point in the history
Add a simple email example to Mailgun
  • Loading branch information
lesv committed Jan 13, 2016
2 parents a46285b + 07a7931 commit cfdbf23
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;

Expand All @@ -38,15 +39,42 @@
public class MailgunServlet extends HttpServlet {

private static final String MAILGUN_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN_NAME");
private static final String MAILGUN_API_KEY= System.getenv("MAILGUN_API_KEY");
private static final String MAILGUN_API_KEY = System.getenv("MAILGUN_API_KEY");

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String type = req.getParameter("submit");
String recipient = req.getParameter("to");
ClientResponse clientResponse;
if (type.equals("Send simple email")) {
clientResponse = sendSimpleMessage(recipient);
} else {
clientResponse = sendComplexMessage(recipient);
}
if (clientResponse.getStatus() == 200) {
resp.getWriter().print("Email sent.");
}
}

private ClientResponse sendSimpleMessage(String recipient) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
WebResource webResource =
client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
+ "/messages");
MultivaluedMapImpl formData = new MultivaluedMapImpl();
formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
formData.add("to", recipient);
formData.add("subject", "Simple Mailgun Example");
formData.add("text", "Plaintext content");
return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,
formData);
}

private ClientResponse sendComplexMessage(String recipient) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
+ "/messages");
FormDataMultiPart formData = new FormDataMultiPart();
formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
formData.field("to", recipient);
Expand All @@ -55,13 +83,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
ClassLoader classLoader = getClass().getClassLoader();
File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
ClientResponse clientResponse =
webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, formData);
if (clientResponse.getStatus() == 200) {
resp.getWriter().print("Email sent.");
} else {
resp.getWriter().print("An error was encountered");
}
return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(ClientResponse.class, formData);
}
}
// [END example]
3 changes: 2 additions & 1 deletion appengine/mailgun/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<!-- [START form] -->
<form method="post" action="/send/email">
<input type="text" name="to" placeholder="Enter recipient email">
<input type="submit" name="submit" value="Send email">
<input type="submit" name="submit" value="Send simple email">
<input type="submit" name="submit" value="Send complex email">
</form>
<!-- [END form] -->
</body>
Expand Down

0 comments on commit cfdbf23

Please sign in to comment.