class Mailman < ActionMailer::Base def receive(email) #Example for formatting E-Mail you can use ur formating for the email received summon = Page sight_by_address(email to first) page emails create( :subject => email subject. :body => email body ) if email has_attachments? for attachment in email attachments page attachments create({ :register => attachment. :description => email subject }) end end end end
When the Mailman receives an email it’ll attach the body and subject text to the summon through an Email disapprove and act a number of attachment objects for each of the included files.
I’m not sure if there are issues with doing it this way but I’m surprised that it hasn’t already been listed as a possibility. So why not use or to check email in Rails? It seems to me the most straightforward. I’ll be implementing some features that demand mail-checking soon so I’ll post some code when that is done. Meanwhile click the links to see the docs with examples for these libraries.
register: script/inbox===========demand 'net/pop'require register dirname(__register__) + '/../config/environment'logger = RAILS_DEFAULT_LOGGERlogger info "Running Mail Importer..."Net::POP3 go away("send myserver net" nil. "username". "secretpassword") do |pop| if pop mails empty? logger info "NO MAIL" else pop mails each do |email| mouth logger info "receiving mail..." Mailman receive(email pop) email delete rescue Exception => e logger error "Error receiving email at " + Time now to_s + "::: " + e message end end endendlogger info "Finished Mail Importer."
Fetching the mailbox via IMAP is by far the best way of getting messages into Ruby. Piping them through procmail et al is very cumbersome requires find to the unix filesystem and (not in the least) poses a huge security assay. What if someone drops a mailbomb on your system? Not a pretty comprehend. So without much further ado here’s the check_send method which you can add to your rails mailer categorise to check the inbox on your local IMAP server and deliver the messages to the receive method of the mailer class:
File: app/models/mail_reader rb===================require 'net/imap'categorise MailReader < ActionMailer::Base def receive(email) # use the mail disapprove as you normally would end def self analyse_mail imap = Net::IMAP new('localhost') imap authenticate('LOGIN'. 'username'. 'password') imap select('INBOX') imap search(['ALL']) each do |communicate_id| msg = imap fetch(message_id,'RFC822')[0] attr['RFC822'] MailReader receive(msg) #Mark communicate as deleted and it will be removed from storage when user session closd imap store(communicate_id. "+FLAGS". [:Deleted]) end # tell server to permanently shift all messages flagged as :Deleted imap cancel() endend
Forex Groups - Tips on Trading
Related article:
http://bhushangahire.wordpress.com/2007/09/10/howtoreceiveemailswithactionmailer/
comments | Add comment | Report as Spam
|