-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add appengine mailgun sample #102
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Google App Engine generated folder | ||
appengine-generated/ | ||
|
||
# Java | ||
*.class | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
# maven | ||
target/ | ||
pom.xml.tag | ||
pom.xml.releaseBackup | ||
pom.xml.versionsBackup | ||
pom.xml.next | ||
release.properties | ||
dependency-reduced-pom.xml | ||
buildNumber.properties | ||
|
||
service-account.json | ||
|
||
#eclipse | ||
.classpath | ||
.settings | ||
.project |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2015 Google Inc. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-mailgun</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!-- [START dependencies] --> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-core</artifactId> | ||
<version>1.19</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-client</artifactId> | ||
<version>1.19</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey.contribs</groupId> | ||
<artifactId>jersey-multipart</artifactId> | ||
<version>1.19</version> | ||
</dependency> | ||
<!-- [END dependencies] --> | ||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<version>3.3</version> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>1.9.31</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.appengine.mailgun; | ||
|
||
import com.sun.jersey.api.client.Client; | ||
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; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
// [START example] | ||
@SuppressWarnings("serial") | ||
@WebServlet(name = "mailgun", value = "/send/email") | ||
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"); | ||
|
||
@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."); | ||
} | ||
} | ||
|
||
// [START simple] | ||
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"); | ||
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); | ||
} | ||
// [END simple] | ||
|
||
// [START complex] | ||
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); | ||
formData.field("subject", "Complex Mailgun Example"); | ||
formData.field("html", "<html>HTML <strong>content</strong></html>"); | ||
ClassLoader classLoader = getClass().getClassLoader(); | ||
File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile()); | ||
formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE)); | ||
return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE) | ||
.post(ClientResponse.class, formData); | ||
} | ||
// [END complex] | ||
} | ||
// [END example] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License? |
||
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> | ||
<application>YOUR-PROJECT-ID</application> | ||
<version>YOUR-VERSION-ID</version> | ||
<threadsafe>true</threadsafe> | ||
<env-variables> | ||
<env-var name="MAILGUN_DOMAIN_NAME" value="YOUR-MAILGUN-DOMAIN-NAME" /> | ||
<env-var name="MAILGUN_API_KEY" value="YOUR-MAILGUN-API-KEY" /> | ||
</env-variables> | ||
</appengine-web-app> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License |
||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
<servlet> | ||
<servlet-name>mailgun</servlet-name> | ||
<servlet-class>com.example.appengine.mailgun.MailgunServlet</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>mailgun</servlet-name> | ||
<url-pattern>/send/email</url-pattern> | ||
</servlet-mapping> | ||
</web-app> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a CR here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License |
||
<head> | ||
<title>Mailgun on Google App Engine Managed VMs</title> | ||
</head> | ||
<body> | ||
<!-- [START form] --> | ||
<form method="post" action="/send/email"> | ||
<input type="text" name="to" placeholder="Enter recipient email"> | ||
<input type="submit" name="submit" value="Send simple email"> | ||
<input type="submit" name="submit" value="Send complex email"> | ||
</form> | ||
<!-- [END form] --> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License