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
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: