lib/mail_spy/manager.rb in mail_spy-0.0.5 vs lib/mail_spy/manager.rb in mail_spy-0.0.6

- old
+ new

@@ -21,19 +21,49 @@ # Make sure we have someone to send to has_sender = options.keys.select { |option| to_options.include? option.intern }.present? raise "Email instance has no sender (to,cc,bcc were all blank)" unless has_sender - - # Make sure that - (required_options + to_options).each do |option| + # Make sure that all options passed map to a accessor so we don't errantly + # think we are passing something correctly and really its getting silently + # ignored + options.keys.each do |option| unless MailSpy::Email.method_defined? "#{option}=".intern raise "MailSpy::Email doesn't have #{option} as a setter '" end end + # Ensure that a esp (forced or random) exists for the email + forced_esp = options[:email_service_provider] + if forced_esp.present? + raise "No esp configured with name: #{forced_esp}" if MailSpy.esps[forced_esp].blank? + else + raise "No esps configured" if MailSpy.esps.blank? + esp_key = MailSpy.esps.keys[rand(MailSpy.esps.keys.count)] + options[:email_service_provider] = esp_key + end + + # Google Analytics aupport for automatic population of utm_tokens + esp = MailSpy.esps[options[:email_service_provider]] + if esp.options[:enable_auto_google_analytics].present? + options[:utm_source] ||= 'mailspy' + options[:utm_medium] ||= 'email' + options[:utm_campaign] ||= options[:campaign] + options[:utm_term] ||= options[:stream] + options[:utm_content] ||= options[:component] + end + + #Create the email email = MailSpy::Email.create!(options) + + #Enable sendgrid specific enhancements + if esp.options[:enable_sendgrid_event_tracking].present? + header = MailSpy::Sendgrid::SmtpApiHeader.new + header.setUniqueArgs({:eid => email.id}) + email.headers = {'X-SMTPAPI' => header.asJSON}.merge(email.headers) + end + email.save! email end # ------------------------------------------- SEND OUTSTANDING EMAILS @@ -50,33 +80,55 @@ email_key = pony_key if email_key.nil? value = email.send("#{email_key}") pony_hash[pony_key] = value if value.present? end - while (true) - mails = MailSpy::Email. + while true + emails = MailSpy::Email. limit(step).offset(offset). - where(:schedule_at.lte => DateTime.now, :sent => false).all + where(:schedule_at.lte => DateTime.now, :sent => false, :failed => false).all break if mails.blank? - mails.each do |email| - mail = MailSpy::CoreMailer.template(email) - #TODO might be nice to flush mail out in debug mode - mail.deliver - email.update_attribute(:sent, true) - sent += 1 + emails.each do |email| + begin + mail = MailSpy::CoreMailer.template(email) + #TODO might be nice to flush mail out in debug mode + mail.deliver + email.update_attribute(:sent, true) + sent += 1 + rescue Exception => e + email.failed = true + email.error_message = e.message + email.error_backtrace = e.backtrace + email.save! + end end offset += step end sent end # ------------------------------------------- TRACKING - # + # MailSpy will automatically track opens and clicks if the tracking bugs + # and track_link helpers are used. This action allows tracking of + # arbitrary actions. Please be careful to standardize on action names + # email_id: The id from the MailSpy::Email record + # action_type: String denoting the action that occured + # details: Hash of any details of that action (again strive for standards) + # count: how many of this action occurred (defaults to 1) + def track_action(email_id, action_type, details={}, count=1) + raise "track_action missing email_id" if email_id.blank? + raise "track_Action missing action_type" if action_type.blank? - def track_other_action + hash ={} + hash[:action_type] = action_type + hash[:count] = count + hash[:details] = details if details.present? && details.kind_of?(Hash) + # Save it up + email = MailSpy::Email.find(email_id) + email.actions.create!(hash) end end end