Ruby script to send email to multiple recipients by Gmail

This works:

require ‘mail’

#Set up Gmail access
options = { :address => “smtp.gmail.com”,
:port => 587,
:domain => ‘localhost’,
:user_name => ‘example@gmail.com’,
:password => ‘passphrase’,
:authentication => ‘plain’,
:enable_starttls_auto => true }

Mail.defaults do
delivery_method :smtp, options
end

#import email addresses from text file
recips = []
IO.foreach(“test.txt”) do |line|
recips << line
end

#Status update
puts “Done with readin\n”

#Format email addresses properly with double quotes and send
recips.each {|value|
puts value
baseadd = value.chomp
address = “\””+baseadd+”\””
mail = Mail.new do
to address
from ‘example@gmail.com’
subject ‘A test email’
body File.read(‘test.txt’)
end
mail.deliver
}

Leave a comment