2020-07-10 05:05:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class GroupSmtpMailer < ActionMailer::Base
|
|
|
|
include Email::BuildEmailHelper
|
|
|
|
|
2023-01-04 17:07:50 -05:00
|
|
|
def send_mail(from_group, to_address, post, cc_addresses: nil, bcc_addresses: nil)
|
2020-07-10 05:05:55 -04:00
|
|
|
raise "SMTP is disabled" if !SiteSetting.enable_smtp
|
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
op_incoming_email = post.topic.first_post.incoming_email
|
2021-10-19 01:26:22 -04:00
|
|
|
recipient_user = User.find_by_email(to_address, primary: true)
|
2020-07-10 05:05:55 -04:00
|
|
|
|
|
|
|
delivery_options = {
|
|
|
|
address: from_group.smtp_server,
|
|
|
|
port: from_group.smtp_port,
|
2021-06-03 00:47:32 -04:00
|
|
|
domain: from_group.email_username_domain,
|
2020-07-10 05:05:55 -04:00
|
|
|
user_name: from_group.email_username,
|
|
|
|
password: from_group.email_password,
|
2024-07-16 02:21:14 -04:00
|
|
|
# NOTE: Might be better at some point to store this authentication method in the database
|
|
|
|
# against the group.
|
|
|
|
authentication: SmtpProviderOverrides.authentication_override(from_group.smtp_server),
|
2024-07-17 20:33:14 -04:00
|
|
|
enable_starttls_auto: from_group.smtp_ssl_mode == Group.smtp_ssl_modes[:starttls],
|
|
|
|
enable_ssl: from_group.smtp_ssl_mode == Group.smtp_ssl_modes[:ssl_tls],
|
2022-06-14 20:28:30 -04:00
|
|
|
return_response: true,
|
2023-11-28 00:32:59 -05:00
|
|
|
open_timeout: GlobalSetting.group_smtp_open_timeout,
|
|
|
|
read_timeout: GlobalSetting.group_smtp_read_timeout,
|
2020-07-10 05:05:55 -04:00
|
|
|
}
|
|
|
|
|
2021-06-27 20:42:06 -04:00
|
|
|
group_name = from_group.name_full_preferred
|
2022-04-15 06:13:37 -04:00
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
build_email(
|
|
|
|
to_address,
|
2020-07-10 05:05:55 -04:00
|
|
|
message: post.raw,
|
|
|
|
url: post.url(without_slug: SiteSetting.private_email?),
|
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
2021-06-27 20:42:06 -04:00
|
|
|
context: "",
|
2020-07-10 05:05:55 -04:00
|
|
|
username: post.user.username,
|
2021-06-27 18:55:13 -04:00
|
|
|
group_name: group_name,
|
2020-07-10 05:05:55 -04:00
|
|
|
allow_reply_by_email: true,
|
|
|
|
only_reply_by_email: true,
|
2021-06-27 18:55:13 -04:00
|
|
|
use_from_address_for_reply_to: SiteSetting.enable_smtp && from_group.smtp_enabled?,
|
2020-07-10 05:05:55 -04:00
|
|
|
private_reply: post.topic.private_message?,
|
2022-04-20 12:05:17 -04:00
|
|
|
participants: UserNotifications.participants(post, recipient_user, reveal_staged_email: true),
|
2020-07-10 05:05:55 -04:00
|
|
|
include_respond_instructions: true,
|
|
|
|
template: "user_notifications.user_posted_pm",
|
|
|
|
use_topic_title_subject: true,
|
2021-06-27 18:55:13 -04:00
|
|
|
topic_title: op_incoming_email&.subject || post.topic.title,
|
2023-06-19 09:22:00 -04:00
|
|
|
add_re_to_subject: !post.is_first_post?,
|
2020-07-10 05:05:55 -04:00
|
|
|
locale: SiteSetting.default_locale,
|
|
|
|
delivery_method_options: delivery_options,
|
2022-02-06 22:52:01 -05:00
|
|
|
from: from_group.smtp_from_address,
|
2022-04-15 06:13:37 -04:00
|
|
|
from_alias: I18n.t("email_from_without_site", group_name: group_name),
|
2021-06-27 20:42:06 -04:00
|
|
|
html_override: html_override(post),
|
2023-01-04 17:07:50 -05:00
|
|
|
cc: cc_addresses,
|
|
|
|
bcc: bcc_addresses,
|
2020-07-10 05:05:55 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-27 20:42:06 -04:00
|
|
|
def html_override(post)
|
2020-07-10 05:05:55 -04:00
|
|
|
UserNotificationRenderer.render(
|
|
|
|
template: "email/notification",
|
|
|
|
format: :html,
|
|
|
|
locals: {
|
2021-06-27 20:42:06 -04:00
|
|
|
context_posts: nil,
|
2020-07-10 05:05:55 -04:00
|
|
|
reached_limit: nil,
|
|
|
|
post: post,
|
2021-06-27 23:19:17 -04:00
|
|
|
in_reply_to_post: nil,
|
2020-07-10 05:05:55 -04:00
|
|
|
classes: Rtl.new(nil).css_class,
|
2021-06-27 20:42:06 -04:00
|
|
|
first_footer_classes: "",
|
|
|
|
reply_above_line: true,
|
2020-07-10 05:05:55 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|