Module: Triklemailer
- Defined in:
- lib/triklemailer.rb
Defined Under Namespace
Classes: Options
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
- .log_sent(email, template) ⇒ Object
-
.send_mail(options, recipients, sent) ⇒ Object
It should take: * A list of emails and data * SMTP account details * A template * SMTP Server details.
Class Method Details
.log_sent(email, template) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/triklemailer.rb', line 100 def self.log_sent(email, template) log_file_name = "./#{['sent', template.split('.').first].join('_')}.csv" CSV.open( log_file_name, 'a', write_headers: !File.exists?(log_file_name), headers: ['email', 'sent_at'] ) do |csv| csv << [email, Time.now] end end |
.send_mail(options, recipients, sent) ⇒ Object
It should take:
-
A list of emails and data
-
SMTP account details
-
A template
-
SMTP Server details
and it should send an email to each email address from the csv using the template provided, filled in with the meta data provided for the email address. When each email is sent it should record that in a separate csv called sent_<template-name>.csv. Failures to send a messages should not be recorded. When sending emails it should check the sent_<template-name>.csv and if it exists, only send emails to email address not included in the sent_<template-name>.csv file.
Read provided csv Check for sent_ csv For each email, meta_data in provided csv
If email in sent_ csv
Next email
Read template file
Apply meta_data to template file
Send email with data from cli or meta data with template
If there was an error sending
Next email
Record email sent to template in sent_ csv
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 |
# File 'lib/triklemailer.rb', line 40 def self.send_mail(, recipients, sent) cleaned_template = .template.gsub(/%(?!{)/, '%%') recipients. reject { |recipient| sent.include?(recipient[:email]) }. each do |recipient| mail = Mail.new do from recipient.fetch(:from, .from) to recipient.fetch(:to, recipient.fetch(:email) { raise "Some rows are missing either a 'to' or 'email' column." }) bcc .bcc if .bcc subject recipient.fetch(:subject, .subject) if .is_html html_part do content_type 'text/html; charset=UTF-8' body cleaned_template % recipient end else text_part do body cleaned_template % recipient end end add_file(File.(.)) if . end if block_given? yield(mail, recipient) else oauth2_token = recipient.fetch(:oauth2_token, .oauth2_token) password = recrecipient.fetch(:password, .password) mail.delivery_method :smtp, { address: recipient.fetch(:host, .host), port: recipient.fetch(:port, .port), user_name: recipient.fetch(:username, .username), password: oauth2_token || password, authentication: oauth2_token ? :xoauth2 : :login, enable_starttls_auto: true } end begin mail.deliver! # might fail in here though... log_sent( recipient.fetch(:to, recipient[:email]), .template_name) rescue => e puts "Failed to send email to #{recipient[:email]}." puts e.class puts e puts e.backtrace next end end end |