1

我使用 net/pop 检索邮件,但我还需要解析电子邮件以获取主题、地址和电子邮件正文。Action Mailer 有什么想法吗?我应该使用 3rd 方宝石。(不,甚至不是 Tmail)

require 'rubygems'
require 'net/pop'
require 'pop_ssl'

Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)  

def pull_mail
  Net::POP3.start("pop.gmail.com", 995, "uname","pass") do |pop|  

    if pop.mails.empty?  
      puts 'No mails.'  
    else  
      pop.each_mail do |mail|  
      puts mail_header
    end  
  end

end

干杯!

4

1 回答 1

1

基本上,您只需要编写您的电子邮件处理程序,所有的解析都将在幕后为您完成:

class MailHandler < ActionMailer::Base
  def receive(email)
    # here you will have an email object and will be able to call methods like
    # email.subject and email.attachments

    puts "from: #{email.from}, subject: '#{email.subject}'"
  end
end

当您使用 Net::POP3 收到电子邮件时,只需将它们交给您的处理程序:

Net::POP3.start(server, port, username, password) do |pop|
  pop.each_mail { |mail| MailHandler.receive(mail.pop) }
end
于 2010-04-21T11:20:19.817 回答