FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
frontend/admin/app/services/EmailService.scala at team-values · devhttps/frontend · GitHub
devhttps
/
frontend
Public
forked from
guardian/frontend
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
frontend
/
admin
/
app
/
services
/
EmailService.scala
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (77 loc) · 2.93 KB
Breadcrumbs
frontend
/
admin
/
app
/
services
/
EmailService.scala
Copy path
File metadata and controls
102 lines (77 loc) · 2.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
services
import
java
.
util
.
concurrent
.
TimeoutException
import
com
.
amazonaws
.
handlers
.
AsyncHandler
import
com
.
amazonaws
.
services
.
simpleemail
.
_
import
com
.
amazonaws
.
services
.
simpleemail
.
model
.{
Destination
=>
EmailDestination
,
_
}
import
common
.{
AkkaAsync
,
Logging
}
import
conf
.
Configuration
.
aws
.
mandatoryCredentials
import
scala
.
collection
.
JavaConverters
.
_
import
scala
.
concurrent
.
duration
.
_
import
scala
.
concurrent
.{
ExecutionContext
,
Future
,
Promise
}
import
scala
.
util
.
control
.
NonFatal
import
scala
.
util
.{
Failure
,
Success
}
class
EmailService
(
akkaAsync
:
AkkaAsync
)
extends
Logging
{
private
lazy
val
client
:
AmazonSimpleEmailServiceAsync
=
AmazonSimpleEmailServiceAsyncClient
.asyncBuilder()
.withCredentials(mandatoryCredentials)
.withRegion(conf.
Configuration
.aws.region)
.build()
val
sendAsync
=
client.sendAsyncEmail(akkaAsync)_
def
shutdown
()
:
Unit
=
client.shutdown()
def
send
(
from
:
String
,
to
:
Seq
[
String
],
cc
:
Seq
[
String
]
=
Nil
,
subject
:
String
,
textBody
:
Option
[
String
]
=
None
,
htmlBody
:
Option
[
String
]
=
None
)(
implicit
executionContext
:
ExecutionContext
)
:
Future
[
SendEmailResult
]
=
{
log.info(
s
"
Sending email from
$from
to
$to
about
$subject
"
)
def
withText
(
body
:
Body
)
:
Body
=
{
textBody map { text
=>
body.withText(
new
Content
().withData(text))
} getOrElse body
}
def
withHtml
(
body
:
Body
)
:
Body
=
{
htmlBody map { html
=>
body.withHtml(
new
Content
().withData(html))
} getOrElse body
}
val
body
=
withHtml(withText(
new
Body
()))
val
message
=
new
Message
()
.withSubject(
new
Content
().withData(subject))
.withBody(body)
val
request
=
new
SendEmailRequest
()
.withSource(from)
.withDestination(
new
EmailDestination
().withToAddresses(to.asJava).withCcAddresses(cc.asJava))
.withMessage(message)
val
futureResponse
=
sendAsync(request)
futureResponse.foreach {
response
=>
log.info(
s
"
Sent message ID
${response.getMessageId}
"
)
}
futureResponse.failed.foreach {
case
NonFatal
(e)
=>
log.error(
s
"
Email send failed:
${e.getMessage}
"
)
}
futureResponse
}
private
implicit
class
RichEmailClient
(
client
:
AmazonSimpleEmailServiceAsync
) {
def
sendAsyncEmail
(
akkaAsync
:
AkkaAsync
)(
request
:
SendEmailRequest
)
:
Future
[
SendEmailResult
]
=
{
val
promise
=
Promise
[
SendEmailResult
]()
akkaAsync.after(
1
.minute) {
promise.tryFailure(
new
TimeoutException
(
s
"
Timed out
"
))
}
val
handler
=
new
AsyncHandler
[
SendEmailRequest
,
SendEmailResult
] {
override
def
onSuccess
(
request
:
SendEmailRequest
,
result
:
SendEmailResult
)
:
Unit
=
promise.complete(
Success
(result))
override
def
onError
(
exception
:
Exception
)
:
Unit
=
promise.complete(
Failure
(exception))
}
try
{
client.sendEmailAsync(request, handler)
promise.future
}
catch
{
case
NonFatal
(e)
=>
Future
.failed(e)
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL