2020-07-10 17:05:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_dependency 'email/message_builder'
|
|
|
|
|
|
|
|
class GroupSmtpMailer < ActionMailer::Base
|
|
|
|
include Email::BuildEmailHelper
|
|
|
|
|
2023-01-05 08:50:54 +08:00
|
|
|
def send_mail(from_group, to_address, post, cc_addresses: nil, bcc_addresses: nil)
|
2020-07-10 17:05:55 +08:00
|
|
|
raise 'SMTP is disabled' if !SiteSetting.enable_smtp
|
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
op_incoming_email = post.topic.first_post.incoming_email
|
2021-10-19 13:26:22 +08:00
|
|
|
recipient_user = User.find_by_email(to_address, primary: true)
|
2020-07-10 17:05:55 +08:00
|
|
|
|
|
|
|
delivery_options = {
|
|
|
|
address: from_group.smtp_server,
|
|
|
|
port: from_group.smtp_port,
|
2021-06-03 12:47:32 +08:00
|
|
|
domain: from_group.email_username_domain,
|
2020-07-10 17:05:55 +08:00
|
|
|
user_name: from_group.email_username,
|
|
|
|
password: from_group.email_password,
|
|
|
|
authentication: GlobalSetting.smtp_authentication,
|
|
|
|
enable_starttls_auto: from_group.smtp_ssl
|
|
|
|
}
|
|
|
|
|
|
|
|
user_name = post.user.username
|
|
|
|
if SiteSetting.enable_names && SiteSetting.display_name_on_email_from
|
|
|
|
user_name = post.user.name unless post.user.name.blank?
|
|
|
|
end
|
|
|
|
|
2021-06-28 08:42:06 +08:00
|
|
|
group_name = from_group.name_full_preferred
|
2021-06-28 06:55:13 +08:00
|
|
|
build_email(
|
|
|
|
to_address,
|
2020-07-10 17:05:55 +08:00
|
|
|
message: post.raw,
|
|
|
|
url: post.url(without_slug: SiteSetting.private_email?),
|
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
2021-06-28 08:42:06 +08:00
|
|
|
context: "",
|
2020-07-10 17:05:55 +08:00
|
|
|
username: post.user.username,
|
2021-06-28 06:55:13 +08:00
|
|
|
group_name: group_name,
|
2020-07-10 17:05:55 +08:00
|
|
|
allow_reply_by_email: true,
|
|
|
|
only_reply_by_email: true,
|
2021-06-28 06:55:13 +08:00
|
|
|
use_from_address_for_reply_to: SiteSetting.enable_smtp && from_group.smtp_enabled?,
|
2020-07-10 17:05:55 +08:00
|
|
|
private_reply: post.topic.private_message?,
|
2021-10-19 13:26:22 +08:00
|
|
|
participants: participants(post, recipient_user),
|
2020-07-10 17:05:55 +08:00
|
|
|
include_respond_instructions: true,
|
|
|
|
template: 'user_notifications.user_posted_pm',
|
|
|
|
use_topic_title_subject: true,
|
2021-06-28 06:55:13 +08:00
|
|
|
topic_title: op_incoming_email&.subject || post.topic.title,
|
2020-07-10 17:05:55 +08:00
|
|
|
add_re_to_subject: true,
|
|
|
|
locale: SiteSetting.default_locale,
|
|
|
|
delivery_method_options: delivery_options,
|
|
|
|
from: from_group.email_username,
|
2021-07-20 09:56:04 +08:00
|
|
|
from_alias: I18n.t('email_from_without_site', user_name: group_name),
|
2021-06-28 08:42:06 +08:00
|
|
|
html_override: html_override(post),
|
2023-01-05 08:50:54 +08:00
|
|
|
cc: cc_addresses,
|
|
|
|
bcc: bcc_addresses
|
2020-07-10 17:05:55 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-28 08:42:06 +08:00
|
|
|
def html_override(post)
|
2020-07-10 17:05:55 +08:00
|
|
|
UserNotificationRenderer.render(
|
|
|
|
template: 'email/notification',
|
|
|
|
format: :html,
|
|
|
|
locals: {
|
2021-06-28 08:42:06 +08:00
|
|
|
context_posts: nil,
|
2020-07-10 17:05:55 +08:00
|
|
|
reached_limit: nil,
|
|
|
|
post: post,
|
2021-06-28 11:19:17 +08:00
|
|
|
in_reply_to_post: nil,
|
2020-07-10 17:05:55 +08:00
|
|
|
classes: Rtl.new(nil).css_class,
|
2021-06-28 08:42:06 +08:00
|
|
|
first_footer_classes: '',
|
|
|
|
reply_above_line: true
|
2020-07-10 17:05:55 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-10-19 13:26:22 +08:00
|
|
|
def participants(post, recipient_user)
|
2020-07-10 17:05:55 +08:00
|
|
|
list = []
|
|
|
|
|
|
|
|
post.topic.allowed_groups.each do |g|
|
2021-06-28 08:42:06 +08:00
|
|
|
list.push("[#{g.name_full_preferred}](#{Discourse.base_url}/groups/#{g.name})")
|
2020-07-10 17:05:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
post.topic.allowed_users.each do |u|
|
2021-10-19 13:26:22 +08:00
|
|
|
next if u.id == recipient_user.id
|
|
|
|
|
2020-07-10 17:05:55 +08:00
|
|
|
if SiteSetting.prioritize_username_in_ux?
|
2021-06-28 08:42:06 +08:00
|
|
|
if u.staged?
|
2021-06-28 11:19:17 +08:00
|
|
|
list.push("#{u.email}")
|
2021-06-28 08:42:06 +08:00
|
|
|
else
|
|
|
|
list.push("[#{u.username}](#{Discourse.base_url}/u/#{u.username_lower})")
|
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
else
|
2021-06-28 08:42:06 +08:00
|
|
|
if u.staged?
|
2021-06-28 11:19:17 +08:00
|
|
|
list.push("#{u.email}")
|
2021-06-28 08:42:06 +08:00
|
|
|
else
|
|
|
|
list.push("[#{u.name.blank? ? u.username : u.name}](#{Discourse.base_url}/u/#{u.username_lower})")
|
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
list.join(', ')
|
|
|
|
end
|
|
|
|
end
|