Created
April 17, 2014 21:31
-
-
Save adkron/11012784 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Email::Mailer < ActionMailer::Base | |
| TIME_WITHOUT_ZONE_FORMAT = "%b %d, %Y at %I:%M %p" | |
| include SendGrid | |
| SendGrid::VALID_OPTIONS << :bcc | |
| BCC_ALL = 'WebAdMIT <[email protected]>' | |
| include Email::Conversions | |
| def email email, preview_address=nil | |
| preview_address = PreviewAddress(preview_address) | |
| applicants = email.applicants | |
| user_identity = email.user_identity | |
| user_address = format_email user_identity | |
| # In non-production enivornments, treat every email as a preview. | |
| preview_address = PreviewAddress(BCC_ALL) unless Rails.env.production? | |
| if preview_address.nil? | |
| cc_header = email.cc.collect{|a| format_email a}.compact | |
| cc_header << user_address if email.cc_user | |
| cc cc_header unless cc_header.empty? | |
| bcc_header = email.bcc.collect{|a| format_email a}.compact | |
| bcc_header << user_address if email.bcc_user | |
| bcc bcc_header unless bcc_header.empty? | |
| all_recipients = cc_header + bcc_header | |
| copies = all_recipients.count | |
| applicants.each {|applicant| all_recipients << format_email(applicant)} | |
| sendgrid_recipients all_recipients | |
| else | |
| applicants = Array(applicants.first) | |
| copies = 0 | |
| sendgrid_recipients [preview_address.address] | |
| end | |
| reply_to_header = email.reply_to.collect{|a| format_email a}.compact | |
| if email.reply_to_user || reply_to_header.empty? | |
| reply_to_header << user_address | |
| end | |
| reply_to reply_to_header | |
| substitutions(email, applicants, copies).each_pair do |token, values| | |
| sendgrid_substitute token, values | |
| end | |
| extra_headers = {} | |
| extra_headers['Importance'] = email.importance | |
| extra_headers['X-Priority'] = email.priority_level | |
| # This should not be necessary, but the SendGrid gem doesn't integrate well | |
| # with Rails 3 ActionMailer when used in legacy mode. | |
| extra_headers['X-SMTPAPI'] = sendgrid_json_headers message | |
| extra_headers['X-SMTPAPI'].merge( | |
| unique_args: { | |
| cycle: Webadmit::Application::CYCLE, email_id: email.id | |
| } | |
| ) | |
| headers extra_headers | |
| # recipients need to be specified, but SendGrid ignores them if | |
| # sendgrid_recipients is set. -dpk | |
| recipients BCC_ALL | |
| #sendgrid_unique_args cycle: Webadmit::Application::CYCLE, email_id: email.id | |
| sendgrid_category email.category | |
| body email.body | |
| content_type 'text/html' | |
| from email.no_reply_address | |
| subject email.subject | |
| end | |
| protected | |
| def format_email record | |
| # Some users have gotten clever with their email addresses; unfortunately, | |
| # this causes problems with ActionMailer. | |
| email = record.email.match(/[!#\$%&'*+\-.\/0-9=?A-Z^_`{|}~]+@[!#\$%&'*+\-.\/0-9=?A-Z^_`{|}~]+/i).to_s | |
| unless email.blank? | |
| name = record.name | |
| name.blank? ? email : "\"#{name}\" <#{email}>" | |
| end | |
| end | |
| def substitutions(email, applicants, copies=0) | |
| bad_tokens = email.substitutions.unknown | |
| field_ids = email.substitutions.known | |
| user_identity = email.user_identity | |
| result = substitute_blanks_for(bad_tokens, applicants.count) | |
| fields = user_identity.fields.find field_ids.map(&:token) | |
| exporter = Export::Base.new user_identity.cas, user_identity.programs, applicants, fields | |
| applicants.each do |applicant| | |
| ActiveRecord::Base.cache do | |
| fields.each do |field| | |
| value = exporter.field_value(applicant, field) | |
| values = Array(value).map do |element| | |
| if element.kind_of?(Time) | |
| # Don't show time zone, as user may be in a different time zone than server. | |
| element.strftime(TIME_WITHOUT_ZONE_FORMAT) | |
| else | |
| element | |
| end | |
| end | |
| (result["{{#{field.id}}}"] ||= []).concat Array.new(copies + 1, values.to_sentence) | |
| end | |
| end | |
| copies = 0 | |
| end | |
| result | |
| end | |
| def substitute_blanks_for(bad_tokens, applicant_count) | |
| return {} if bad_tokens.empty? | |
| empties = Array.new(applicant_count, '') | |
| Hash[bad_tokens.collect {|token| ["{{#{token.token}}}", empties] } ] | |
| end | |
| end | |
| module TMail | |
| class Encoder | |
| def fold | |
| @f << @eol | |
| @lwsp = ' ' | |
| @curlen = 0 | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment