Created
February 25, 2010 07:28
-
-
Save rjungemann/314331 to your computer and use it in GitHub Desktop.
Send an email over IMAP using Ruby
This file contains hidden or 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
# http://www.tutorialspoint.com/ruby/ruby_sending_email.htm | |
# http://www.devco.net/archives/2009/10/27/using_ruby_netimap_with_plain_auth.php | |
require 'net/smtp' | |
require 'net/imap' | |
message = <<MESSAGE_END | |
From: Private Person <[email protected]> | |
To: A Test User <[email protected]> | |
MIME-Version: 1.0 | |
Content-type: text/html | |
Subject: SMTP e-mail test | |
This is an e-mail message to be sent in HTML format | |
<b>This is HTML message.</b> | |
<h1>This is headline.</h1> | |
MESSAGE_END | |
Net::SMTP.start('mail.your-domain.com', 25, 'localhost', 'username', 'password') do |smtp| | |
smtp.send_message message, '[email protected]', '[email protected]' | |
end | |
class ImapPlainAuthenticator | |
def process(data) | |
return "#{@user}\0#{@user}\0#{@password}" | |
end | |
def initialize(user, password) | |
@user = user | |
@password = password | |
end | |
end | |
Net::IMAP::add_authenticator('PLAIN', ImapPlainAuthenticator) | |
imap = Net::IMAP.new('imap.your.com') | |
imap.authenticate('PLAIN', 'username', 'pass') | |
imap.examine('INBOX/subfolder') | |
imap.search(["ALL"]).each do |message_id| | |
envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"] | |
puts "#{envelope.from[0].mailbox}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment