rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » Ruby

在rails中使用POP3接收邮件

上次我们写了如何在rails中发送email,自然接收email也是常常需要的功能,今天给出一段代码实现在rails中接受并处理email,代码如下:
Ruby代码
#!/usr/bin/env ruby   require 'net/pop'   require File.dirname(__FILE__) + '/../config/environment'   logger = RAILS_DEFAULT_LOGGER   logger.info "Running Mail Importer..."    Net::POP3.start("localhost", nil, "username", "password") do |pop|    if pop.mails.empty?       logger.info "NO MAIL"    else       pop.mails.each do |email|       begin        logger.info "receiving mail..."        Notifier.receive(email.pop)        email.delete       rescue Exception => e        logger.error "Error receiving email at " + Time.now.to_s + "::: " + e.message       end       end    end   end   logger.info "Finished Mail Importer."  
做些说明:
你需要在*Net::POP3.start*这行写上你要接收的邮箱的服务器地址以及你的用户名密码,如上配置好了,你就可以在cron中配置调度这个任务了,至于调度的频度你可以按照自己的需求来设定了。如何,还算简单吧。
顶一下
(0)
踩一下
(0)