-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailer.rb
More file actions
62 lines (49 loc) · 1.55 KB
/
mailer.rb
File metadata and controls
62 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Mailer
require 'mail'
require_relative 'logging'
include Logging
require_relative 'envs'
def self.send_message(subject,body,attachments=nil)
mail_secrets = ["MAIL_PASSWORD","MAIL_USERNAME","BFS_EMAILS"]
mail_envs = Envs::get_envs(mail_secrets)
if mail_envs.nil?
logger.info "Going to skip sending email since environment variables are not set, MAIL_USERNAME,MAIL_PASSWORD, and BFS_EMAILS"
return
end
if ENV["SKIP_EMAILS"]
logger.info "Going to skip sending email since environment variable SKIP_EMAIL is set"
return
end
logger.info "Going to send email"
from_email = "lib-noreply@berkeley.edu"
options = {:address => "smtp.gmail.com",
:port => 465,
:user_name => mail_envs["MAIL_USERNAME"],
:password => mail_envs["MAIL_PASSWORD"],
:authentication => 'plain',
:tls => true,
:return_response => true
}
Mail.defaults do
delivery_method :smtp, options
end
mail_envs["BFS_EMAILS"].split(/,/).each do |to_email|
begin
Mail.deliver do
to to_email.gsub(/\n/,'')
from from_email
subject subject
body body
attachments.each do |attachment|
add_file attachment if File.file?(attachment)
end
end
logger.info "Email sent"
rescue StandardError => e
logger.error "Error sending email: #{e}"
end
end
#sleeping for 2 seconds so emails don't get flagged for spamming
sleep 2
end
end