discourse/app/mailers/group_smtp_mailer.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class GroupSmtpMailer < ActionMailer::Base
include Email::BuildEmailHelper
def send_mail(from_group, to_address, post, cc_addresses: nil, bcc_addresses: nil)
raise "SMTP is disabled" if !SiteSetting.enable_smtp
op_incoming_email = post.topic.first_post.incoming_email
recipient_user = User.find_by_email(to_address, primary: true)
delivery_options = {
address: from_group.smtp_server,
port: from_group.smtp_port,
FEATURE: Use group SMTP settings for sending user notification emails (initial) (#13220) This PR changes the `UserNotification` class to send outbound `user_private_message` using the group's SMTP settings, but only if: * The first allowed_group on the topic has SMTP configured and enabled * SiteSetting.enable_smtp is true * The group does not have IMAP enabled, if this is enabled the `GroupSMTPMailer` handles things The email is sent using the group's `email_username` as both the `from` and `reply-to` address, so when the user replies from their email it will go through the group's SMTP inbox, which needs to have email forwarding set up to send the message on to a location (such as a hosted site email address like meta@discoursemail.com) where it can be POSTed into discourse's handle_mail route. Also includes a fix to `EmailReceiver#group_incoming_emails_regex` to include the `group.email_username` so the group does not get a staged user created and invited to the topic (which was a problem for IMAP), as well as updating `Group.find_by_email` to find using the `email_username` as well for inbound emails with that as the TO address. #### Note This is safe to merge without impacting anyone seriously. If people had SMTP enabled for a group they would have IMAP enabled too currently, and that is a very small amount of users because IMAP is an alpha product, and also because the UserNotification change has a guard to make sure it is not used if IMAP is enabled for the group. The existing IMAP tests work, and I tested this functionality by manually POSTing replies to the SMTP address into my local discourse. There will probably be more work needed on this, but it needs to be tested further in a real hosted environment to continue.
2021-06-03 00:47:32 -04:00
domain: from_group.email_username_domain,
user_name: from_group.email_username,
password: from_group.email_password,
authentication: GlobalSetting.smtp_authentication,
enable_starttls_auto: from_group.smtp_ssl,
return_response: true,
open_timeout: GlobalSetting.group_smtp_open_timeout,
read_timeout: GlobalSetting.group_smtp_read_timeout,
}
group_name = from_group.name_full_preferred
build_email(
to_address,
message: post.raw,
url: post.url(without_slug: SiteSetting.private_email?),
post_id: post.id,
topic_id: post.topic_id,
context: "",
username: post.user.username,
group_name: group_name,
allow_reply_by_email: true,
only_reply_by_email: true,
use_from_address_for_reply_to: SiteSetting.enable_smtp && from_group.smtp_enabled?,
private_reply: post.topic.private_message?,
participants: UserNotifications.participants(post, recipient_user, reveal_staged_email: true),
include_respond_instructions: true,
template: "user_notifications.user_posted_pm",
use_topic_title_subject: true,
topic_title: op_incoming_email&.subject || post.topic.title,
add_re_to_subject: !post.is_first_post?,
locale: SiteSetting.default_locale,
delivery_method_options: delivery_options,
from: from_group.smtp_from_address,
from_alias: I18n.t("email_from_without_site", group_name: group_name),
html_override: html_override(post),
cc: cc_addresses,
bcc: bcc_addresses,
)
end
private
def html_override(post)
UserNotificationRenderer.render(
template: "email/notification",
format: :html,
locals: {
context_posts: nil,
reached_limit: nil,
post: post,
in_reply_to_post: nil,
classes: Rtl.new(nil).css_class,
first_footer_classes: "",
reply_above_line: true,
},
)
end
end