-
Notifications
You must be signed in to change notification settings - Fork 1k
/
outgoing_message_prototype.rb
205 lines (176 loc) 路 5.04 KB
/
outgoing_message_prototype.rb
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
require 'resolv'
class OutgoingMessagePrototype
attr_accessor :from
attr_accessor :sender
attr_accessor :to
attr_accessor :cc
attr_accessor :bcc
attr_accessor :subject
attr_accessor :reply_to
attr_accessor :custom_headers
attr_accessor :plain_body
attr_accessor :html_body
attr_accessor :attachments
attr_accessor :tag
attr_accessor :credential
attr_accessor :bounce
def initialize(server, ip, source_type, attributes)
@server = server
@ip = ip
@source_type = source_type
@custom_headers = {}
@attachments = []
@message_id = "#{SecureRandom.uuid}@#{Postal.config.dns.return_path}"
attributes.each do |key, value|
instance_variable_set("@#{key}", value)
end
end
def message_id
@message_id
end
def from_address
Postal::Helpers.strip_name_from_address(@from)
end
def sender_address
Postal::Helpers.strip_name_from_address(@sender)
end
def domain
@domain ||= begin
d = find_domain
d == :none ? nil : d
end
end
def find_domain
@domain ||= begin
domain = @server.authenticated_domain_for_address(@from)
if @server.allow_sender? && domain.nil?
domain = @server.authenticated_domain_for_address(@sender)
end
domain || :none
end
end
def to_addresses
@to.is_a?(String) ? @to.to_s.split(/\,\s*/) : @to.to_a
end
def cc_addresses
@cc.is_a?(String) ? @cc.to_s.split(/\,\s*/) : @cc.to_a
end
def bcc_addresses
@bcc.is_a?(String) ? @bcc.to_s.split(/\,\s*/) : @bcc.to_a
end
def all_addresses
[to_addresses, cc_addresses, bcc_addresses].flatten
end
def create_messages
if valid?
all_addresses.each_with_object({}) do |address, hash|
if address = Postal::Helpers.strip_name_from_address(address)
hash[address] = create_message(address)
end
end
else
false
end
end
def valid?
validate
errors.empty?
end
def errors
@errors || {}
end
def attachments
(@attachments || []).map do |attachment|
{
:name => attachment[:name],
:content_type => attachment[:content_type] || 'application/octet-stream',
:data => attachment[:base64] ? Base64.decode64(attachment[:data]) : attachment[:data]
}
end
end
def validate
@errors = Array.new
if to_addresses.empty? && cc_addresses.empty? && bcc_addresses.empty?
@errors << "NoRecipients"
end
if to_addresses.size > 50
@errors << 'TooManyToAddresses'
end
if cc_addresses.size > 50
@errors << 'TooManyCCAddresses'
end
if bcc_addresses.size > 50
@errors << 'TooManyBCCAddresses'
end
if @plain_body.blank? && @html_body.blank?
@errors << "NoContent"
end
if from.blank?
@errors << "FromAddressMissing"
end
if domain.nil?
@errors << "UnauthenticatedFromAddress"
end
if attachments && !attachments.empty?
attachments.each_with_index do |attachment, index|
if attachment[:name].blank?
@errors << "AttachmentMissingName" unless @errors.include?("AttachmentMissingName")
elsif attachment[:data].blank?
@errors << "AttachmentMissingData" unless @errors.include?("AttachmentMissingData")
end
end
end
@errors
end
def raw_message
@raw_message ||= begin
mail = Mail.new
if @custom_headers.is_a?(Hash)
@custom_headers.each { |key, value| mail[key.to_s] = value.to_s }
end
mail.to = self.to_addresses.join(', ') if self.to_addresses.present?
mail.cc = self.cc_addresses.join(', ') if self.cc_addresses.present?
mail.from = @from
mail.sender = @sender
mail.subject = @subject
mail.reply_to = @reply_to
mail.part :content_type => "multipart/alternative" do |p|
if !@plain_body.blank?
p.text_part = Mail::Part.new
p.text_part.body = @plain_body
end
if !@html_body.blank?
p.html_part = Mail::Part.new
p.html_part.content_type = "text/html; charset=UTF-8"
p.html_part.body = @html_body
end
end
attachments.each do |attachment|
mail.attachments[attachment[:name]] = {
:mime_type => attachment[:content_type],
:content => attachment[:data]
}
end
mail.header['Received'] = "from #{@source_type} (#{self.resolved_hostname} [#{@ip}]) by Postal with HTTP; #{Time.now.utc.rfc2822.to_s}"
mail.message_id = "<#{@message_id}>"
mail.to_s
end
end
def create_message(address)
message = @server.message_db.new_message
message.scope = 'outgoing'
message.rcpt_to = address
message.mail_from = self.from_address
message.domain_id = self.domain.id
message.raw_message = self.raw_message
message.tag = self.tag
message.credential_id = self.credential&.id
message.received_with_ssl = true
message.bounce = @bounce ? 1 : 0
message.save
{:id => message.id, :token => message.token}
end
def resolved_hostname
@resolved_hostname ||= Resolv.new.getname(@ip) rescue @ip
end
end