Mass Mail using Ruby

  1. To generate multiple emails we use two files which have to be passed into the script (below), these are set by the variables filename and lettername, filename contains the email address, one per line
  2. This script also requires the Fastercsv gem.
#!ruby
#

require 'fastercsv'
require 'net/smtp'

#filename = 'addr.txt'
filename = 'emaillist.csv'
lettername = 'jasola_letter.txt'

def sendMsg(name,email,body)
puts "To:#{email}"
#puts "Hello #{name},"
#puts body.join(' ')
smtp = Net::SMTP.new('mail.mnain.org')
#p smtp
smtp.start('mnain.org','madan@mnain.org','wak89giv')
msg = "Subject: Sell and Buy real estate\r\n" +
      "From: Geeta Kassal <jasola@mnain.org>\r\n" +
	  "\r\n" +
	  "Hello #{name},\r\n"
body.each { |l|
		msg += l
	}
#puts msg


#p smtp
smtp.sendmail(msg, 'madan@mnain.org', email)
smtp.finish()
sleep 2
end

fp = nil
begin
fp = File.open(lettername, 'r')
rescue
end

if fp.nil? then
	STDERR.puts "Unable to access #{lettername}"
	exit -1
end

if !File.exists? filename then
	STDERR.puts "Unable to access #{filename}"
	exit -2
end

letterBody = Array.new
fp.each_line { |l|
	letterBody.push l
}
fp.close

#sendMsg('Madan','mnain@yahoo.com',letterBody)
#sendMsg('Geeta','geetanain@gmail.com',letterBody)
#exit

#p letterBody
FasterCSV.foreach(filename) { |row|
	sendMsg(row[0],row[1],letterBody)
	#puts "#{row[0]} : #{row[1]}"
}

# puts letterBody.join(' ')

rubymassmail.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0