Class: Jobs::GroupSmtpEmail
- Includes:
- Skippable
- Defined in:
- app/jobs/regular/group_smtp_email.rb
Instance Method Summary collapse
- #execute(args) ⇒ Object
- #quit_email_early? ⇒ Boolean
- #skip(email, post, recipient_user, reason) ⇒ Object
Methods included from Skippable
Methods inherited from Base
acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately
Instance Method Details
#execute(args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/jobs/regular/group_smtp_email.rb', line 22 def execute(args) return if quit_email_early? email = args[:email] recipient_user = User.find_by_email(email, primary: true) post = Post.find_by(id: args[:post_id]) return skip(email, nil, recipient_user, :group_smtp_post_deleted) if post.blank? group = Group.find_by(id: args[:group_id]) return if group.blank? if !group.smtp_enabled return skip(email, post, recipient_user, :group_smtp_disabled_for_group) end if !Topic.exists?(id: post.topic_id) return skip(email, post, recipient_user, :group_smtp_topic_deleted) end cc_addresses = args[:cc_emails].filter { |address| EmailAddressValidator.valid_value?(address) } # Mask the email addresses of non-staged users so # they are not revealed unnecessarily when we are sending # the email notification out. bcc_addresses = User.not_staged.with_email(cc_addresses).pluck(:email) cc_addresses = cc_addresses - bcc_addresses # There is a rare race condition causing the Imap::Sync class to create # an incoming email and associated post/topic, which then kicks off # the PostAlerter to notify others in the PM about a reply in the topic, # but for the OP which is not necessary (because the person emailing the # IMAP inbox already knows about the OP) # # Basically, we should never be sending this notification for the first # post in a topic. # # If the group does not have IMAP enabled then this could be legitimate, # for example in cases where we are creating a new topic to reply to another # group PM and we need to send the participants the group OP email. if post.is_first_post? && group.imap_enabled ImapSyncLog.warn( "Aborting SMTP email for post #{post.id} in topic #{post.topic_id} to #{email}, the post is the OP and should not send an email.", group, ) return end ImapSyncLog.debug( "Sending SMTP email for post #{post.id} in topic #{post.topic_id} to #{email}.", group, ) # The EmailLog record created by the sender will have the raw email # stored, the group smtp ID, and any cc addresses recorded for later # cross referencing. = GroupSmtpMailer.send_mail( group, email, post, cc_addresses: cc_addresses, bcc_addresses: bcc_addresses, ) begin Email::Sender.new(, :group_smtp, recipient_user).send rescue Net::ReadTimeout => err # We can't be sure if the send actually failed or if ENTER . ENTER (to end # the SMTP data sequence) just timed out, as is the case with Gmail occassionaly, # where they can do this if they suspect you are sending spam. Discourse.warn_exception( err, message: "Got SMTP read timeout when sending group SMTP email", env: args, ) end # Create an incoming email record to avoid importing again from IMAP # server. While this may not be technically required if IMAP is not # currently enabled for the group, it will help a lot with the initial # sync if it is turned on at a later date. begin IncomingEmail.create!( user_id: post.user_id, topic_id: post.topic_id, post_id: post.id, raw: .to_s, subject: .subject, message_id: ., to_addresses: .to, cc_addresses: .cc, from_address: .from, created_via: IncomingEmail.created_via_types[:group_smtp], ) rescue => err Discourse.warn_exception( err, message: "Failed to create IncomingEmail record when sending group SMTP email", env: args, ) end end |
#quit_email_early? ⇒ Boolean
126 127 128 |
# File 'app/jobs/regular/group_smtp_email.rb', line 126 def quit_email_early? SiteSetting.disable_emails == "yes" || !SiteSetting.enable_smtp end |
#skip(email, post, recipient_user, reason) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'app/jobs/regular/group_smtp_email.rb', line 130 def skip(email, post, recipient_user, reason) create_skipped_email_log( email_type: :group_smtp, to_address: email, user_id: recipient_user&.id, post_id: post&.id, reason_type: SkippedEmailLog.reason_types[reason], ) end |