Last active
March 9, 2020 11:34
-
-
Save mh61503891/aeed2aacd38608cf3a44d261c040e5ca to your computer and use it in GitHub Desktop.
A ruby script to get a message by Message-Id via IMAP from Gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # get-message-by-id-via-imap-from-gmail.rb | |
# | |
# ## Usage | |
# | |
# 1. Put the decryption key in a file | |
# | |
# ```console | |
# $ bundle install sekrets | |
# $ echo $your_decryption_key > .sekrets.key | |
# ``` | |
# | |
# 2. Edit your settings file | |
# | |
# ```console | |
# $ sekrets edit settings.yml.enc | |
# ``` | |
# | |
# ```settings.yml.enc | |
# imap: | |
# host: imap.gmail.com | |
# port: 993 | |
# usessl: true | |
# auth: | |
# user: <%= your_user_name %> | |
# password: <%= your_app_password %> | |
# ``` | |
# | |
# 3. Run nyancat.rb | |
# | |
# ```console | |
# $ ruby get-message-by-id-via-imap-from-gmail.rb $message_id | |
# ``` | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'mail' | |
gem 'colorize' | |
gem 'activesupport' | |
gem 'sekrets' | |
gem 'pry' | |
end | |
require 'net/imap' | |
require 'nkf' | |
require 'active_support/core_ext' | |
class NyanCat | |
def initialize(settings_file_path:, message_id:) | |
@settings_file_path = settings_file_path | |
@message_id = message_id | |
end | |
def run | |
if @message_id.blank? | |
STDERR.puts("error: :message_id cannot be blank") | |
return | |
end | |
@imap = Net::IMAP.new( | |
settings.dig(:imap, :host), | |
settings.dig(:imap, :port), | |
settings.dig(:imap, :usessl) | |
) | |
STDERR.puts @imap.login( | |
settings.dig(:imap, :auth, :user), | |
settings.dig(:imap, :auth, :password), | |
).raw_data.strip.green | |
STDERR.puts @imap.examine( | |
Net::IMAP.encode_utf7('[Gmail]/すべてのメール') | |
).raw_data.strip.green | |
uids = @imap.uid_search(['HEADER', 'MESSAGE-ID', @message_id]) | |
@imap.uid_fetch(uids, 'BODY.PEEK[]').each do |data| | |
if data.attr.has_key?('UID') && data.attr.has_key?('BODY[]') | |
mail = Mail.new(data.attr['BODY[]']) | |
puts_mail(STDOUT, mail) | |
else | |
STDERR.puts("error: #{data}") | |
end | |
end | |
STDERR.puts @imap.logout( | |
).raw_data.strip.green | |
@imap.disconnect | |
end | |
private | |
def settings | |
@settings ||= Sekrets | |
.settings_for(@settings_file_path) | |
.deep_symbolize_keys | |
end | |
def puts_mail(stream, mail) | |
stream.puts '```' | |
stream.puts "Date: #{mail.date}" | |
stream.puts "From: #{mail.from_addrs.join(', ')}" | |
stream.puts "To: #{mail.to_addrs.join(', ')}" | |
stream.puts "Cc: #{mail.cc_addrs.join(', ')}" | |
stream.puts "Bcc: #{mail.bcc_addrs.join(', ')}" | |
stream.puts "Message-Id: <#{mail.message_id}>" | |
stream.puts "Subject: #{mail.subject}" | |
stream.puts | |
if mail.multipart? | |
if mail.text_part | |
body = mail.text_part.decoded | |
stream.puts body | |
elsif mail.html_part | |
body = mail.html_part.decoded | |
stream.puts body | |
end | |
else | |
body = mail.body.to_s | |
stream.puts body | |
end | |
stream.puts '```' | |
end | |
end | |
NyanCat.new( | |
settings_file_path: './settings.yml.enc', | |
message_id: ARGV.first | |
).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment