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')
|
|
|
|
layout 'mailer'
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
@conversation = conversation
|
2020-05-22 12:44:18 +00:00
|
|
|
@account = @conversation.account
|
2020-01-23 17:29:07 +00:00
|
|
|
@contact = @conversation.contact
|
|
|
|
@agent = @conversation.assignee
|
|
|
|
|
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,
|
|
|
|
subject: mail_subject(@messages.last),
|
|
|
|
message_id: custom_message_id,
|
|
|
|
in_reply_to: in_reply_to_email
|
|
|
|
})
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
def mail_subject(_last_message, _trim_length = 50)
|
|
|
|
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
|
|
|
|
if custom_domain_email_enabled?
|
2020-05-22 12:44:18 +00:00
|
|
|
"reply+to+#{@conversation.uuid}@#{@account.domain}"
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
|
|
|
@agent&.email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_email
|
2020-05-22 12:44:18 +00:00
|
|
|
if custom_domain_email_enabled? && @account.support_email.present?
|
|
|
|
@account.support_email
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
|
|
|
ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
def custom_message_id
|
|
|
|
"<conversation/#{@conversation.uuid}/messages/#{@messages.last.id}@#{current_domain}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_reply_to_email
|
|
|
|
"<account/#{@account.id}/conversation/#{@conversation.uuid}@#{current_domain}>"
|
|
|
|
end
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
def custom_domain_email_enabled?
|
2020-05-22 12:44:18 +00:00
|
|
|
@custom_domain_email_enabled ||= @account.domain_emails_enabled? && @account.domain.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_domain
|
|
|
|
if custom_domain_email_enabled? && @account.domain
|
|
|
|
@account.domain
|
|
|
|
else
|
|
|
|
GlobalConfig.get('FALLBACK_DOMAIN')['FALLBACK_DOMAIN']
|
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|