Created
March 16, 2014 11:53
-
-
Save ota42y/9582125 to your computer and use it in GitHub Desktop.
rubyでevernoteにノートを送る ref: http://qiita.com/ota42y/items/6c73164cac6645758206
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
require 'yaml' | |
# これをやらないと警告が沢山出る | |
# http://stackoverflow.com/questions/8783400/warning-already-initialized-constant-after-installing-tlsmail-gem | |
require 'net/smtp' | |
Net.instance_eval {remove_const :SMTPSession} if defined?(Net::SMTPSession) | |
require 'net/pop' | |
Net::POP.instance_eval {remove_const :Revision} if defined?(Net::POP::Revision) | |
Net.instance_eval {remove_const :POP} if defined?(Net::POP) | |
Net.instance_eval {remove_const :POPSession} if defined?(Net::POPSession) | |
Net.instance_eval {remove_const :POP3Session} if defined?(Net::POP3Session) | |
Net.instance_eval {remove_const :APOPSession} if defined?(Net::APOPSession) | |
require 'tlsmail' | |
require 'mail' | |
class SendEvernote | |
def initialize(evernote_mail, gmail_account, gmail_pass) | |
@smtpserver = Net::SMTP.new('smtp.gmail.com',587) | |
@smtpserver.enable_tls(OpenSSL::SSL::VERIFY_NONE) | |
@evernote_mail = evernote_mail | |
@gmail_account = gmail_account | |
@gmail_pass = gmail_pass | |
# まとめて送るのでそれ用のバッファ | |
@buffer = [] | |
end | |
def addNote(subject, body, files) | |
mail = Mail.new | |
mail[:from] = @gmail_account | |
mail[:to] = @evernote_mail | |
mail.subject = subject | |
mail.body = body | |
files.each do |path| | |
mail.add_file(path) | |
end | |
@buffer << mail | |
end | |
def send | |
@smtpserver.start('gmail.com',@gmail_account, @gmail_pass, :login) do |smtp| | |
@buffer.each do |mail| | |
smtp.send_message(mail.encoded, mail.from, mail.to) | |
end | |
@buffer.clear | |
end | |
end | |
end | |
config = YAML.load_file("evernote.yml") | |
send_evernote = SendEvernote.new(config["evernote_mail"], config["gmail_account"], config["gmail_pass"]) | |
send_evernote.addNote("test", "this is test", ["logo-new.png", "logo.png"]) | |
send_evernote.send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment