2020-03-01 13:36:13 +00:00
|
|
|
class ConversationReplyMailer < ApplicationMailer
|
2020-01-23 17:29:07 +00:00
|
|
|
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
|
2020-07-19 17:38:07 +00:00
|
|
|
layout :choose_layout
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2020-03-01 13:36:13 +00:00
|
|
|
def reply_with_summary(conversation, message_queued_time)
|
2020-01-23 17:29:07 +00:00
|
|
|
return unless smtp_config_set_or_development?
|
|
|
|
|
2020-08-13 19:17:24 +00:00
|
|
|
init_conversation_attributes(conversation)
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2020-05-10 07:44:57 +00:00
|
|
|
recap_messages = @conversation.messages.chat.where('created_at < ?', message_queued_time).last(10)
|
|
|
|
new_messages = @conversation.messages.chat.where('created_at >= ?', message_queued_time)
|
2020-01-23 17:29:07 +00:00
|
|
|
|
|
|
|
@messages = recap_messages + new_messages
|
|
|
|
@messages = @messages.select(&:reportable?)
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
mail({
|
|
|
|
to: @contact&.email,
|
|
|
|
from: from_email,
|
|
|
|
reply_to: reply_email,
|
2020-07-15 11:03:52 +00:00
|
|
|
subject: mail_subject,
|
|
|
|
message_id: custom_message_id,
|
|
|
|
in_reply_to: in_reply_to_email
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_without_summary(conversation, message_queued_time)
|
|
|
|
return unless smtp_config_set_or_development?
|
|
|
|
|
2020-08-13 19:17:24 +00:00
|
|
|
init_conversation_attributes(conversation)
|
2020-07-15 11:03:52 +00:00
|
|
|
|
2020-07-20 07:17:02 +00:00
|
|
|
@messages = @conversation.messages.chat.outgoing.where('created_at >= ?', message_queued_time)
|
|
|
|
return false if @messages.count.zero?
|
2020-07-15 11:03:52 +00:00
|
|
|
|
|
|
|
mail({
|
|
|
|
to: @contact&.email,
|
|
|
|
from: from_email,
|
|
|
|
reply_to: reply_email,
|
|
|
|
subject: mail_subject,
|
2020-05-22 12:44:18 +00:00
|
|
|
message_id: custom_message_id,
|
|
|
|
in_reply_to: in_reply_to_email
|
|
|
|
})
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-13 19:17:24 +00:00
|
|
|
def init_conversation_attributes(conversation)
|
|
|
|
@conversation = conversation
|
|
|
|
@account = @conversation.account
|
|
|
|
@contact = @conversation.contact
|
|
|
|
@agent = @conversation.assignee
|
|
|
|
end
|
|
|
|
|
2020-07-31 10:27:19 +00:00
|
|
|
def assignee_name
|
|
|
|
@assignee_name ||= @agent&.available_name || 'Notifications'
|
|
|
|
end
|
|
|
|
|
2020-07-15 11:03:52 +00:00
|
|
|
def mail_subject
|
2020-05-22 12:44:18 +00:00
|
|
|
subject_line = I18n.t('conversations.reply.email_subject')
|
2020-03-30 10:37:01 +00:00
|
|
|
"[##{@conversation.display_id}] #{subject_line}"
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
|
|
|
|
def reply_email
|
2020-07-19 17:38:07 +00:00
|
|
|
if inbound_email_enabled?
|
2020-07-31 10:27:19 +00:00
|
|
|
"#{assignee_name} <reply+#{@conversation.uuid}@#{current_domain}>"
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
|
|
|
@agent&.email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_email
|
2020-07-19 17:38:07 +00:00
|
|
|
if inbound_email_enabled?
|
2020-07-31 10:27:19 +00:00
|
|
|
"#{assignee_name} <#{account_support_email}>"
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
2020-07-31 10:27:19 +00:00
|
|
|
"#{assignee_name} <#{ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')}>"
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
def custom_message_id
|
2020-07-15 11:03:52 +00:00
|
|
|
"<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{current_domain}>"
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def in_reply_to_email
|
|
|
|
"<account/#{@account.id}/conversation/#{@conversation.uuid}@#{current_domain}>"
|
|
|
|
end
|
|
|
|
|
2020-07-19 17:38:07 +00:00
|
|
|
def inbound_email_enabled?
|
|
|
|
@inbound_email_enabled ||= @account.feature_enabled?('inbound_emails') && current_domain.present? && account_support_email.present?
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_domain
|
2020-07-19 10:05:55 +00:00
|
|
|
@current_domain ||= begin
|
|
|
|
@account.domain ||
|
|
|
|
ENV.fetch('MAILER_INBOUND_EMAIL_DOMAIN', false) ||
|
|
|
|
GlobalConfig.get('MAILER_INBOUND_EMAIL_DOMAIN')['MAILER_INBOUND_EMAIL_DOMAIN']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_support_email
|
|
|
|
@account_support_email ||= begin
|
|
|
|
@account.support_email ||
|
|
|
|
GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] ||
|
|
|
|
ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
2020-07-19 17:38:07 +00:00
|
|
|
|
|
|
|
def choose_layout
|
|
|
|
return false if action_name == 'reply_without_summary'
|
|
|
|
|
2020-08-06 09:51:06 +00:00
|
|
|
'mailer/base'
|
2020-07-19 17:38:07 +00:00
|
|
|
end
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|