Skip to content

Commit 980a739

Browse files
committed
Merge pull request #102 from GoogleCloudPlatform/ae-mailgun
Add appengine mailgun sample
2 parents 4d1ff19 + 4cc54e2 commit 980a739

File tree

7 files changed

+296
-0
lines changed

7 files changed

+296
-0
lines changed

appengine/.gitignore

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2015 Google Inc. All Rights Reserved.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
# Google App Engine generated folder
13+
appengine-generated/
14+
15+
# Java
16+
*.class
17+
18+
# Mobile Tools for Java (J2ME)
19+
.mtj.tmp/
20+
21+
# Package Files #
22+
*.jar
23+
*.war
24+
*.ear
25+
26+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
27+
hs_err_pid*
28+
29+
# maven
30+
target/
31+
pom.xml.tag
32+
pom.xml.releaseBackup
33+
pom.xml.versionsBackup
34+
pom.xml.next
35+
release.properties
36+
dependency-reduced-pom.xml
37+
buildNumber.properties
38+
39+
service-account.json
40+
41+
#eclipse
42+
.classpath
43+
.settings
44+
.project

appengine/mailgun/pom.xml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2015 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.appengine</groupId>
23+
<artifactId>appengine-mailgun</artifactId>
24+
<parent>
25+
<groupId>com.google.cloud</groupId>
26+
<artifactId>doc-samples</artifactId>
27+
<version>1.0.0</version>
28+
<relativePath>../..</relativePath>
29+
</parent>
30+
<dependencies>
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>javax.servlet-api</artifactId>
34+
<version>3.1.0</version>
35+
<type>jar</type>
36+
<scope>provided</scope>
37+
</dependency>
38+
<!-- [START dependencies] -->
39+
<dependency>
40+
<groupId>com.sun.jersey</groupId>
41+
<artifactId>jersey-core</artifactId>
42+
<version>1.19</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.sun.jersey</groupId>
46+
<artifactId>jersey-client</artifactId>
47+
<version>1.19</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.sun.jersey.contribs</groupId>
51+
<artifactId>jersey-multipart</artifactId>
52+
<version>1.19</version>
53+
</dependency>
54+
<!-- [END dependencies] -->
55+
</dependencies>
56+
<build>
57+
<!-- for hot reload of the web application -->
58+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<version>3.3</version>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<configuration>
65+
<source>1.7</source>
66+
<target>1.7</target>
67+
</configuration>
68+
</plugin>
69+
<plugin>
70+
<groupId>com.google.appengine</groupId>
71+
<artifactId>appengine-maven-plugin</artifactId>
72+
<version>1.9.31</version>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.mailgun;
18+
19+
import com.sun.jersey.api.client.Client;
20+
import com.sun.jersey.api.client.ClientResponse;
21+
import com.sun.jersey.api.client.WebResource;
22+
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
23+
import com.sun.jersey.core.util.MultivaluedMapImpl;
24+
import com.sun.jersey.multipart.FormDataMultiPart;
25+
import com.sun.jersey.multipart.file.FileDataBodyPart;
26+
27+
import java.io.File;
28+
import java.io.IOException;
29+
30+
import javax.servlet.annotation.WebServlet;
31+
import javax.servlet.http.HttpServlet;
32+
import javax.servlet.http.HttpServletRequest;
33+
import javax.servlet.http.HttpServletResponse;
34+
import javax.ws.rs.core.MediaType;
35+
36+
// [START example]
37+
@SuppressWarnings("serial")
38+
@WebServlet(name = "mailgun", value = "/send/email")
39+
public class MailgunServlet extends HttpServlet {
40+
41+
private static final String MAILGUN_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN_NAME");
42+
private static final String MAILGUN_API_KEY = System.getenv("MAILGUN_API_KEY");
43+
44+
@Override
45+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
46+
String type = req.getParameter("submit");
47+
String recipient = req.getParameter("to");
48+
ClientResponse clientResponse;
49+
if (type.equals("Send simple email")) {
50+
clientResponse = sendSimpleMessage(recipient);
51+
} else {
52+
clientResponse = sendComplexMessage(recipient);
53+
}
54+
if (clientResponse.getStatus() == 200) {
55+
resp.getWriter().print("Email sent.");
56+
}
57+
}
58+
59+
// [START simple]
60+
private ClientResponse sendSimpleMessage(String recipient) {
61+
Client client = Client.create();
62+
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
63+
WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
64+
+ "/messages");
65+
MultivaluedMapImpl formData = new MultivaluedMapImpl();
66+
formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
67+
formData.add("to", recipient);
68+
formData.add("subject", "Simple Mailgun Example");
69+
formData.add("text", "Plaintext content");
70+
return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,
71+
formData);
72+
}
73+
// [END simple]
74+
75+
// [START complex]
76+
private ClientResponse sendComplexMessage(String recipient) {
77+
Client client = Client.create();
78+
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
79+
WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
80+
+ "/messages");
81+
FormDataMultiPart formData = new FormDataMultiPart();
82+
formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
83+
formData.field("to", recipient);
84+
formData.field("subject", "Complex Mailgun Example");
85+
formData.field("html", "<html>HTML <strong>content</strong></html>");
86+
ClassLoader classLoader = getClass().getClassLoader();
87+
File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
88+
formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
89+
return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
90+
.post(ClientResponse.class, formData);
91+
}
92+
// [END complex]
93+
}
94+
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-ID</version>
19+
<threadsafe>true</threadsafe>
20+
<env-variables>
21+
<env-var name="MAILGUN_DOMAIN_NAME" value="YOUR-MAILGUN-DOMAIN-NAME" />
22+
<env-var name="MAILGUN_API_KEY" value="YOUR-MAILGUN-API-KEY" />
23+
</env-variables>
24+
</appengine-web-app>
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
17+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
18+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19+
version="2.5">
20+
<servlet>
21+
<servlet-name>mailgun</servlet-name>
22+
<servlet-class>com.example.appengine.mailgun.MailgunServlet</servlet-class>
23+
</servlet>
24+
<servlet-mapping>
25+
<servlet-name>mailgun</servlet-name>
26+
<url-pattern>/send/email</url-pattern>
27+
</servlet-mapping>
28+
</web-app>
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!doctype html>
2+
<!--
3+
Copyright 2015 Google Inc. All Rights Reserved.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<html>
15+
<head>
16+
<title>Mailgun on Google App Engine Managed VMs</title>
17+
</head>
18+
<body>
19+
<!-- [START form] -->
20+
<form method="post" action="/send/email">
21+
<input type="text" name="to" placeholder="Enter recipient email">
22+
<input type="submit" name="submit" value="Send simple email">
23+
<input type="submit" name="submit" value="Send complex email">
24+
</form>
25+
<!-- [END form] -->
26+
</body>
27+
</html>

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<modules>
2626
<module>appengine/appidentity</module>
2727
<module>appengine/helloworld</module>
28+
<module>appengine/mailgun</module>
2829
<module>bigquery</module>
2930
<module>datastore</module>
3031
<module>logging</module>

0 commit comments

Comments
 (0)