Skip to content

Instantly share code, notes, and snippets.

@madhavan-rp
Last active January 19, 2018 03:47
Show Gist options
  • Save madhavan-rp/6608302 to your computer and use it in GitHub Desktop.
Save madhavan-rp/6608302 to your computer and use it in GitHub Desktop.
Mailman gem - IMAP & POP3 Configuration with rails
#!/usr/bin/env ruby
# Program to download emails through imap.
#Uses Gmail's imap extensions to download all emails of a label.
require "rubygems"
require "bundler/setup"
require "mailman"
Mailman.config.ignore_stdin = true
#Mailman.config.logger = Logger.new File.expand_path("mailman.log", __FILE__)
Mailman.config.poll_interval = 15 # Set to zero if you don't want to poll
Mailman.config.imap = {
server: 'imap.gmail.com', port: 993, ssl: true,
username: ENV['GMAIL_USERNAME'],
password: ENV['GMAIL_PASSWORD'],
folder: "MyLabel", # Edit This
filter: "ALL"
}
Mailman::Application.run do
default do
puts "got a new mail " + message.subject
path = "/save/my/emails/here/"
File.open(path + message.message_id, "wb") {|file| file.write(message.text_part.decoded)} if message.text_part
end
end
Mailman.config.rails_root = Rails.root.to_s
Mailman.config.maildir = Rails.root.to_s + "/mailbox"
Mailman.config.logger = Logger.new(Rails.root.to_s + "/log/mailman.log")
Mailman.config.ignore_stdin = true
Mailman.config.imap = {
server: 'imap.gmail.com',
port: 993, # usually 995, 993 for gmail
ssl: true,
username: ENV['ADMIN_GMAIL_USERNAME'],
password: ENV['ADMIN_GMAIL_PASSWORD'],
filter: 'FROM madhavan', #http://tools.ietf.org/html/rfc3501#section-6.4.4
done_flags: nil
}
#Inet reference - http://ruby-doc.org/stdlib-2.0/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-search
#POP3 Configuration
Mailman.config.pop3 = {
username: ENV['ADMIN_GMAIL_USERNAME'],
password: ENV['ADMIN_GMAIL_PASSWORD'],
server: 'pop.gmail.com',
port: 995, # defaults to 110
ssl: true # defaults to false
}
Mailman::Application.run do
from 'madhavan.rp@npcompete.com' do
puts "Yo Yo #{message.subject}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment