Class MailingListMailer
In: app/models/mailing_list_mailer.rb
Parent: ActionMailer::Base

Send email to mailing list. Also receives email from Mailman for archives. Old, but battle-tested, code.

Methods

post   private_reply   receive  

Public Instance methods

[Source]

    # File app/models/mailing_list_mailer.rb, line 18
18:   def post(post)
19:     # Not thread-safe. Won't work for multiple associations.
20:     ActionMailer::Base.default_url_options[:host] = RacingAssociation.current.rails_host
21:     
22:     @subject    = post.subject
23:     @body       = post.body
24:     @recipients = post.mailing_list.name
25:     @from       = post.sender
26:     @sent_on    = post.date
27:     @headers    = {}
28:   end

Reply just to sender of post, not the whole list

[Source]

    # File app/models/mailing_list_mailer.rb, line 5
 5:   def private_reply(post, to)
 6:     # Not thread-safe. Won't work for multiple associations.
 7:     ActionMailer::Base.default_url_options[:host] = RacingAssociation.current.rails_host
 8:     
 9:     raise("'To' cannot be blank") if to.blank?
10:     @subject    = post.subject
11:     @body       = post.body
12:     @recipients = to
13:     @from       = post.sender
14:     @sent_on    = post.date
15:     @headers    = {}
16:   end

Expects raw email from Mailman archiver Really need tricky sender logic for web posts? Shouldn‘t web posts be forwarded through list, too? If so, update test data

[Source]

     # File app/models/mailing_list_mailer.rb, line 33
 33:   def receive(email)
 34:     post = Post.new
 35: 
 36:     # Will fail if no matches. Rely on validation
 37:     list_post_header = email.header_string("List-Post")
 38:     matches = list_post_header.match(/<mailto:(\S+)@/) if list_post_header
 39:     if matches
 40:       mailing_list_name = matches[1]
 41:     else
 42:       mailing_list_name = email.to.first.to_s
 43:     end
 44:     post.mailing_list = MailingList.find_by_name(mailing_list_name)
 45: 
 46:     post.subject = email.subject
 47:     
 48:     if email.multipart?
 49:       plain_text_part = nil
 50: 
 51:       # Outlook
 52:       related_part = email.parts.find { |part| 
 53:         part.content_type == "multipart/related"
 54:       }
 55:       if related_part
 56:         alt_part = related_part.parts.find { |part| 
 57:           part.content_type == "multipart/alternative"
 58:         }
 59:       else
 60:         alt_part = email.parts.find { |part| 
 61:           part.content_type == "multipart/alternative"
 62:         }
 63:       end
 64:       
 65:       # OS X rich text email
 66:       if alt_part 
 67:         plain_text_part = alt_part.parts.find { |part| 
 68:           part.content_type == "text/plain"
 69:         }
 70:       end
 71: 
 72:       plain_text_part = email.parts.find { |part| 
 73:         part.content_type == "text/plain"
 74:       } unless plain_text_part
 75:       
 76:       plain_text_part = email.parts.find { |part| 
 77:         part.content_type == "text/html"
 78:       } unless plain_text_part
 79:       
 80:       post.body = plain_text_part.body
 81:     end
 82:     
 83:     if post.body.blank?
 84:       post.body = email.body
 85:     end
 86:     
 87:     post.from_name = email.friendly_from
 88:     post.from_email_address = email.from.first
 89:     post.date = email.date
 90:     begin
 91:       post.save!
 92:     rescue => save_error
 93:       Rails.logger.error("Could not save post: #{save_error}")
 94:       if post && post.errors.any?
 95:         Rails.logger.error(post.errors.full_messages)
 96:       end
 97:       raise
 98:     end
 99:     post
100:   end

[Validate]