/
conversation_reply_mailer_helper.rb
91 lines (74 loc) 路 2.57 KB
/
conversation_reply_mailer_helper.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
module ConversationReplyMailerHelper
def prepare_mail(cc_bcc_enabled)
@options = {
to: to_emails,
from: email_from,
reply_to: email_reply_to,
subject: mail_subject,
message_id: custom_message_id,
in_reply_to: in_reply_to_email
}
if cc_bcc_enabled
@options[:cc] = cc_bcc_emails[0]
@options[:bcc] = cc_bcc_emails[1]
end
ms_smtp_settings
set_delivery_method
Rails.logger.info("Email sent from #{email_from} to #{to_emails} with subject #{mail_subject}")
mail(@options)
end
private
def ms_smtp_settings
return unless @inbox.email? && @channel.imap_enabled && @inbox.channel.provider == 'microsoft'
smtp_settings = {
address: 'smtp.office365.com',
port: 587,
user_name: @channel.imap_login,
password: @channel.provider_config['access_token'],
domain: 'smtp.office365.com',
tls: false,
enable_starttls_auto: true,
openssl_verify_mode: 'none',
authentication: 'xoauth2'
}
@options[:delivery_method] = :smtp
@options[:delivery_method_options] = smtp_settings
end
def set_delivery_method
return unless @inbox.inbox_type == 'Email' && @channel.smtp_enabled
smtp_settings = {
address: @channel.smtp_address,
port: @channel.smtp_port,
user_name: @channel.smtp_login,
password: @channel.smtp_password,
domain: @channel.smtp_domain,
tls: @channel.smtp_enable_ssl_tls,
enable_starttls_auto: @channel.smtp_enable_starttls_auto,
openssl_verify_mode: @channel.smtp_openssl_verify_mode,
authentication: @channel.smtp_authentication
}
@options[:delivery_method] = :smtp
@options[:delivery_method_options] = smtp_settings
end
def email_smtp_enabled
@inbox.inbox_type == 'Email' && @channel.smtp_enabled
end
def email_imap_enabled
@inbox.inbox_type == 'Email' && @channel.imap_enabled
end
def email_microsoft_auth_enabled
@inbox.inbox_type == 'Email' && @channel.provider == 'microsoft'
end
def email_from
email_microsoft_auth_enabled || email_smtp_enabled ? channel_email_with_name : from_email_with_name
end
def email_reply_to
email_imap_enabled ? @channel.email : reply_email
end
# Use channel email domain in case of account email domain is not set for custom message_id and in_reply_to
def channel_email_domain
return @account.inbound_email_domain if @account.inbound_email_domain.present?
email = @inbox.channel.try(:email)
email.present? ? email.split('@').last : raise(StandardError, 'Channel email domain not present.')
end
end