· 8 years ago · Mar 02, 2018, 10:20 PM
1#!/usr/bin/env ruby1.9
2# -*- coding: utf-8 -*-
3
4require 'csv'
5require 'erb'
6require 'kconv'
7require 'net/smtp'
8require 'openssl'
9
10def post(content, config)
11 smtpserver = Net::SMTP.new(config[:smtp], config[:port])
12
13 if config[:tls]
14 if RUBY_VERSION < '1.9.0'
15 smtpserver.enable_tls(OpenSSL::SSL::VERIFY_NONE)
16 else
17 #context = OpenSSL::SSL::SSLContext.new
18 #context.verify_mode = OpenSSL::SSL::VERIFY_NONE
19 #smtpserver.enable_tls(nil)
20 smtpserver.enable_tls
21 end
22 end
23
24 message = ""
25 message << "From: TPF <#{config[:mail]}>\n"
26 message << "To: #{config[:to]}\n"
27 message << "Subject: #{config[:title]}\n"
28 message << "\n"
29 message << content + "\n"
30
31 smtpserver.start(config[:helo], config[:id], config[:pass], :login) { |smtp|
32 #smtp.send_message(message.kconv(Kconv::JIS, Kconv::UTF8), config[:mail], config[:to])
33 smtp.send_message(message, config[:mail], config[:to])
34 }
35end
36
37body = File.read('body.txt')
38car_ = File.read('car.txt')
39config = {
40 :id => 'foo@gmail.com',
41 :pass => 'foobarbaz',
42 :smtp => 'smtp.gmail.com',
43 :helo => 'gmail.com',
44 :port => 465,
45 :mail => 'foo@gmail.com',
46 :title => 'Title',
47 :tls => true
48}
49no = 1
50
51CSV.foreach(ARGV.shift) do |row|
52 car = ''
53 erb = ERB.new(body)
54 name = row[1].force_encoding('utf-8')
55 config[:to] = row[2].force_encoding('utf-8')
56
57 if row[6].force_encoding('utf-8') == 'ã¯ã„'
58 car = car_
59 end
60
61 post(erb.result(binding), config)
62end