forked from markusklems/aws-lambda-java-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaSendMailFunctionHandler.java
More file actions
103 lines (89 loc) · 3.65 KB
/
Copy pathLambdaSendMailFunctionHandler.java
File metadata and controls
103 lines (89 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package lambda;
import java.util.Map.Entry;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.StreamRecord;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.Body;
import com.amazonaws.services.simpleemail.model.Content;
import com.amazonaws.services.simpleemail.model.Destination;
import com.amazonaws.services.simpleemail.model.Message;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;
public class LambdaSendMailFunctionHandler implements
RequestHandler<DynamodbEvent, Object> {
@Override
public Object handleRequest(DynamodbEvent input, Context context) {
String employeeId = "";
String employeeName = "";
String expenseType = "";
Double amount = 0.0;
for (DynamodbStreamRecord r : input.getRecords()) {
// context.getLogger().log("Event id: "+r.getEventID());
// context.getLogger().log("Event name: "+r.getEventName());
// context.getLogger().log("Event source: "+r.getEventSource());
StreamRecord sr = r.getDynamodb();
for (Entry<String, AttributeValue> entry : sr.getNewImage()
.entrySet()) {
String k = entry.getKey();
AttributeValue v = entry.getValue();
switch (k) {
case "employee_id":
employeeId = v.getS();
break;
case "employee_name":
employeeName = v.getS();
break;
case "expense_type":
expenseType = v.getS();
break;
case "amount":
amount = Double.valueOf(entry.getValue().getN());
break;
default:
context.getLogger().log("Key " + k + " is unknown.");
}
}
}
context.getLogger().log(
"ENTRY: " + employeeId + " | " + employeeName + " | "
+ expenseType + " | " + amount);
String from = "klems@tu-berlin.de"; // TODO Replace with your "From" address.
// This address must be verified.
String to = "klems@tu-berlin.de"; // TODO Replace with a "To" address. If you
// have not yet requested production
// access, this address must be
// verified.
String subject = String.format("Expense reimbursment request by %s",
employeeName);
// TODO Replace with your own approval URL
String approvalUrl = String
.format("https://.......execute-api.eu-west-1.amazonaws.com/test/reimbursment?id=%s",
employeeId);
String body = String
.format("Hello boss,\n\nplease approve my expense reimbursment:\n%s\n\nExpense type: %s\nAmount: %s EUR\n\nThanks!\n%s\nEmployee ID: %s ",
approvalUrl, expenseType, amount, employeeName,
employeeId);
sendMail(from, to, subject, body);
context.getLogger().log("Email sent from " + from + " to " + to);
return null;
}
private void sendMail(final String from, final String to,
final String subjectStr, final String bodyStr) {
// Construct an object to contain the recipient address.
Destination destination = new Destination()
.withToAddresses(new String[] { to });
// Create the subject and body of the message.
Content subject = new Content().withData(subjectStr);
Content textBody = new Content().withData(bodyStr);
Body body = new Body().withText(textBody);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subject).withBody(body);
// TODO Assemble the email.
// TODO Send the email.
}
}