forked from finagle/finagle-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultEmail.scala
205 lines (182 loc) · 6.01 KB
/
DefaultEmail.scala
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package io.github.finagle.smtp
import com.twitter.util.Time
/**
* Constructs an [[io.github.finagle.smtp.EmailMessage]].
*/
case class DefaultEmail(
override val from: Seq[MailingAddress] = Seq.empty,
override val to: Seq[MailingAddress] = Seq.empty,
override val cc: Seq[MailingAddress] = Seq.empty,
override val bcc: Seq[MailingAddress] = Seq.empty,
override val replyTo: Seq[MailingAddress] = Seq.empty,
override val date: Time = Time.now,
override val subject: String = "",
whoSent: Option[MailingAddress] = None,
body : Seq[String] = Seq.empty
) extends EmailMessage {
/**
* Adds originator addresses, which will appear in the ''From:'' field.
* These addresses are validated when converted to
* [[io.github.finagle.smtp.MailingAddress]]
*
* @param addrs Addresses to be added
*/
def from_(addrs: String*): DefaultEmail = setFrom(from ++ addrs.map(MailingAddress(_)))
/**
* Set the ''From:'' field to contain given originator addresses.
*
* @param addrs Addresses to be set
*/
def setFrom(addrs: Seq[MailingAddress]): DefaultEmail = copy(from = addrs)
/**
* Sets the ''Sender:'' field to contain given mailing address.
* Sender should be specified if there are multiple
* originator addresses.
* If the given address is not included in ''From:'' field,
* it is added there.
* This address is validated when converted to
* [[io.github.finagle.smtp.MailingAddress]]
*
* @param addr Address to be set
*/
def sender_(addr: String): DefaultEmail = {
val mb = MailingAddress(addr)
if(from contains mb) copy(whoSent = Some(mb))
else copy(whoSent = Some(mb), from = from :+ mb)
}
/**
* Gets the sender of this message
*/
override def sender = whoSent getOrElse (from.headOption getOrElse MailingAddress.empty)
/**
* Adds recipient addresses, which will appear in the ''To:'' field.
* These addresses are validated when converted to
* [[io.github.finagle.smtp.MailingAddress]]
*
* @param addrs Addresses to be added
*/
def to_(addrs: String*): DefaultEmail = setTo(to ++ addrs.map(MailingAddress(_)))
/**
* Set the ''To:'' field to contain given recipient addresses.
*
* @param addrs Addresses to be set
*/
def setTo(addrs: Seq[MailingAddress]): DefaultEmail = copy(to = addrs)
/**
* Adds carbon copy addresses, which will appear in the ''Cc:'' field.
* These addresses are validated when converted to
* [[io.github.finagle.smtp.MailingAddress]]
*
* @param addrs Addresses to be added
*/
def cc_(addrs: String*): DefaultEmail = setCc(cc ++ addrs.map(MailingAddress(_)))
/**
* Set the ''Cc:'' field to contain given carbon copy addresses.
*
* @param addrs Addresses to be set
*/
def setCc(addrs: Seq[MailingAddress]): DefaultEmail = copy(cc = addrs)
/**
* Adds blind carbon copy addresses, which will appear in the ''Bcc:'' field.
* These addresses are validated when converted to
* [[io.github.finagle.smtp.MailingAddress]]
*
* @param addrs Addresses to be added
*/
def bcc_(addrs: String*): DefaultEmail = setBcc(bcc ++ addrs.map(MailingAddress(_)))
/**
* Set the ''Bcc:'' field to contain given blind carbon copy addresses.
*
* @param addrs Addresses to be set
*/
def setBcc(addrs: Seq[MailingAddress]): DefaultEmail = copy(bcc = addrs)
/**
* Adds the addresses to reply to, which will appear in the ''Reply-To:'' field.
* These addresses are validated when converted to
* [[io.github.finagle.smtp.MailingAddress]]
*
* @param addrs Addresses to be added
*/
def replyTo_(addrs: String*): DefaultEmail = setReplyTo(replyTo ++ addrs.map(MailingAddress(_)))
/**
* Set the ''Reply-To:'' field to contain given addresses to reply to.
*
* @param addrs Addresses to be set
*/
def setReplyTo(addrs: Seq[MailingAddress]): DefaultEmail = copy(replyTo = addrs)
/**
* Set the date of sending the message.
*
* @param dt Date to be set
*/
def date_(dt: Time): DefaultEmail = copy(date = dt)
/**
* Set the subject of the message.
*
* @param sbj Subject to be set
*/
def subject_(sbj: String): DefaultEmail = copy(subject = sbj)
/**
* Add lines to the message text.
*
* @param lines Lines to be added
*/
def text(lines: String*): DefaultEmail = setText(body ++ lines)
/**
* Add lines to the message text.
*
* @param lines Lines to be added
*/
def setText(lines: Seq[String]): DefaultEmail = copy(body = lines)
/**
* Instantiate an [[io.github.finagle.smtp.EmailMessage]] from the payload.
* If the date of sending the message is not set,
* current date is used. If sender of the message
* is not specified, the first address in ''From:'' is used.
*/
def headers: Seq[(String, String)] = {
from.map(ad => ("from", ad.mailbox)) ++ {
if (from.length > 1)
Seq("sender" -> {
if (!sender.isEmpty) sender.mailbox
else from.head.mailbox
})
else Seq.empty
}++
to.map(ad => ("to", ad.mailbox)) ++
cc.map(ad => ("cc", ad.mailbox)) ++
bcc.map(ad => ("bcc", ad.mailbox)) ++
replyTo.map(ad => ("reply-to", ad.mailbox)) ++
Seq(
"date" -> EmailMessage.DateFormat.format(date),
"subject" -> subject
)
}
}
/**
* Factory for [[io.github.finagle.smtp.DefaultEmail]] instances
*/
object DefaultEmail {
/**
* Creates an empty [[io.github.finagle.smtp.DefaultEmail]].
* This method is for Java compatibility.
*/
def create(): DefaultEmail = DefaultEmail()
/**
* Creates an [[io.github.finagle.smtp.DefaultEmail]] with payload from given
* [[io.github.finagle.smtp.EmailMessage]].
*
* @param msg The message to copy payload from
*/
def apply(msg: EmailMessage): DefaultEmail = DefaultEmail(
from = msg.from,
whoSent = if (msg.sender.isEmpty) None else Some(msg.sender),
to = msg.to,
cc = msg.cc,
bcc = msg.bcc,
replyTo = msg.replyTo,
date = msg.date,
subject = msg.subject,
body = msg.body
)
}