Email (developers) - MikeBlyth/mission_database GitHub Wiki

Information for Developers

The present application uses external services for sending and receiving email, CloudMailin for receiving and SendGrid for sending. These are easy to configure with Heroku (and probably on other systems). Their free level of service is limited, of course, so if more emails are required then either a paid account or some other solution will be required. When using Heroku, start configuration by selecting the add-on from within Heroku, rather than going to the external site (e.g., sendgrid.com).

Incoming Email

Mail is received by the incoming_mails_controller. It’s configured to receive an email as a POST from CloudMailin, which is installed as an add-on in Heroku. The controller requires params 'from', params 'subject', and 'plain' (the plain-text body of the email). If something besides CloudMailin is used, either the parameters need to be configured to those (with the same lower-case format), or the code would need to be changed.

The blog article “Receiving Incoming Email in Rails 3” gives an overview of the options.

Outgoing Email

The application uses SendGrid for sending emails. It is not difficult to configure another service including any SMTP server.

Details

The class Notifier (app/mailers/notifier.rb) is a subclass of ActionMailer::Base. An instance of Notifier is an email object ready to send via its deliver method. Methods within Notifier set up the address and body for different types of messages. The actual content of the message (body) is created by a view in the app/mailers/views. There are text and html versions of the views so that the email can contain both plain text and html.

Take as an example the method that sends out contact updates. The controller sets up the data (contacts and recipients), creates the message, sends it with message.deliver, then creates a log entry.

# app/controllers/admin_controller.rb
def self.autosend_contact_updates
	contacts = Contact.recently_updated.sort
	recipients = SiteSetting.contact_update_recipients
	message = Notifier.contact_updates(recipients, contacts)
	message.deliver
	AppLog.create(:severity=>'info', :code=>'Notice.contact_updates', 
	  :description => "#{contacts.length} updated contacts")
  end 

The notifier method contact_updates just passes on to the reports/contact_updates view the lists of recipients and updated contacts.

# app/views/reports/contact_updates.text.haml
Updated Contacts

These contacts have been recently updated. Not every piece of information
in any contact has been changed, and there may be some contacts where in
fact nothing has changed (if the record was updated in the database but
the information was not changed).

- if [email protected]?
  - @contacts.each do |contact|
	= "#{contact.member.last_name_first} (updated #{contact.updated_at.to_s(:date)})"
	= "Contact type: #{contact.contact_type}"
	= render(:partial => "reports/contact", :object=>contact )
- else
  *** No changes recorded ***
reports#contact_updates  
⚠️ **GitHub.com Fallback** ⚠️