使用 Action Mailer 在 Ruby 中解析电子邮件

发布于 2024-08-30 08:57:22 字数 469 浏览 17 评论 0原文

我使用 net/pop 检索邮件,但我还需要解析电子邮件以从地址和电子邮件正文获取主题。 对 Action Mailer 有什么想法吗? 我应该使用第 3 方宝石。(不,甚至不是 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

干杯!

I retrieve mail using net/pop , but I also need to parse through the email to obtain subject,from address and email body.
Any ideas with Action Mailer?
I'm supposed to use 3rd party gems.(No,not even 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

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城仅有旧梦在 2024-09-06 08:57:22

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

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

Basically, you just need to write your email handler, and all the parsing will be done for you behind the scenes:

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

When you get emails using Net::POP3, just hand them off to your handler:

Net::POP3.start(server, port, username, password) do |pop|
  pop.each_mail { |mail| MailHandler.receive(mail.pop) }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文