joseph lebrech
2006-Jun-16 12:19 UTC
[Rails] Polling pop3 and adding emails to ticket system
Hi guys,
Im trying this code below to poll the pop3 mailbox, which i can do as a
standalone .rb file which copies the files to inbox/ but now im trying
to do the same for the controller to open the emails and add the content
to the ticket system.
but all i get is error, can somebody enlighten me?
class IncomingMail < ActionMailer::Base
def receive(email)
@ticket = Ticket.new
@ticket.name = email.from[0]
@ticket.title = email.subject
@ticket.description = email.body
@ticket.save
end
end
class TicketsController < ApplicationController
def index
list
render :action => ''list''
end
# GETs should be safe (see
http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@ticket_pages, @tickets = paginate :tickets, :per_page => 10
end
def show
@ticket = Ticket.find(params[:id])
#@comment = params[:id]
end
def new
@ticket = Ticket.new
end
def create
@ticket = Ticket.new(params[:ticket])
if @ticket.save
flash[:notice] = ''Ticket was successfully created.''
redirect_to :action => ''list''
else
render :action => ''new''
end
end
def edit
@ticket = Ticket.find(params[:id])
end
def update
@ticket = Ticket.find(params[:id])
if @ticket.update_attributes(params[:ticket])
flash[:notice] = ''Ticket was successfully updated.''
redirect_to :action => ''show'', :id => @ticket
else
render :action => ''edit''
end
end
def destroy
Ticket.find(params[:id]).destroy
redirect_to :action => ''list''
end
def add_comment
@comment =
Ticket.find(params[:id]).comments.create(params[:comment])
redirect_to :action => ''show'', :id => params[:id],
:comment=>
@comment
end
def poll_pop3
require ''net/pop''
pop = Net::POP3.new(''mail'')
pop.start(''me@company'', ''password'')
# (1)
if pop.mails.empty?
else
i = 0
pop.each_mail do |m| # or "pop.mails.each ..." # (2)
IncomingMail.receive(m.pop)
i += 1
end
end
pop.finish
end
end
--
Posted via http://www.ruby-forum.com/.
