Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple email example to Mailgun #55

Merged
merged 1 commit into from
Jan 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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