Skip to content

Instantly share code, notes, and snippets.

@Martin91
Created October 24, 2014 15:35
Show Gist options
  • Select an option

  • Save Martin91/a4f7aa4cd6755914da6a to your computer and use it in GitHub Desktop.

Select an option

Save Martin91/a4f7aa4cd6755914da6a to your computer and use it in GitHub Desktop.
simple example to read email addresses from excel and send notifications to each email address
require 'rubygems'
require 'net/smtp'
require 'roo'
def load_receivers
receivers = []
excel_file = Roo::Excel.new(ENV['EXCEL'])
2.upto(excel_file.last_row).each do |row|
# Assume that the name is in the second column, and email is in the third column.
# e.g. ['martin91', '[email protected]']
receivers.push [excel_file.cell(row, 2), excel_file.cell(row, 3)]
end
receivers
end
def send_email(receiver_name, receiver_email)
message = <<-END_OF_MESSAGE
From: Martin Hong <[email protected]>
To: #{receiver_name} <#{receiver_email}>
Subject: Subject Here
Date: #{Time.now}
Dear#{receiver_name},
xxxxx messages here xxxxx
Best Regards,
Martin
END_OF_MESSAGE
smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start('gmail.com', '[email protected]', ENV['PASSWORD'], :plain) do |smtp|
smtp.send_message message, '[email protected]', receiver_email
end
end
def send_notifications
receivers = load_receivers
receivers.each do |receiver|
name, email = *receiver
puts "Sending email to #{name}: #{email}..."
send_email(name, email)
puts "Email is sent to #{name}!"
end
end
send_notifications
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment