使用Gmail gem跟踪一些电子邮件



我正在使用gmail gem发送电子邮件,我需要跟踪这些电子邮件。我该怎么做?

我正在尝试使用message_id搜索电子邮件,但它从我的收件箱中带来了所有电子邮件,我只想要特定电子邮件的回复。

这是我的实际代码:

*使用message_id保存电子邮件*

mail = gmail.deliver(email)
Email.create(:message_id => mail.message_id, :from => user.email,
  :to => annotation.to, :body => annotation.content, :title => annotation.title,
  :annotation => annotation, :user => user)

*用message_id搜索邮件*

messages = gmail.inbox.mails(:message_id => email.message_id)

问候

法布里西奥·法拉利·德·坎波斯

你可以

看看Net::IMAP。

uid =  gmail.conn.uid_search(["HEADER", "Message-ID", "<324820.440351247482145930.JavaMail.coremail@bj163app31.163.com>"])[0]
#=> 103
message = Gmail::Message.new(gmail.inbox, uid)
#=>  #<Gmail::Message0x12a72e798 mailbox=INBOX uid=103> 
message.subject
#=> "hello world"
message.message_id
#=> "<324820.440351247482145930.JavaMail.coremail@bj163app31.163.com>"

没有找到一种方法可以通过message_id.via搜索,这样您就可以获得特定的电子邮件。

使用标准的 gmail gem,这似乎工作得很好

messages = gmail.inbox.mails(:query => ['HEADER', 'Message-ID', email.message_id])

我能够使用此Gmail Gem执行此操作(不确定这是否与您正在使用的Gem相同)。

邮件 ID 标头是生成的电子邮件对象的一部分。然后可以使用rfc822msgid进行搜索(在 Gmail 的高级搜索帮助页面中进行了说明)。

下面是一个示例:

def gmail_connect
  Gmail.connect(email_address, password)
end
def send_email
  gmail = gmail_connect
  email = gmail.compose do
    to recipient@mail.internet
    subject 'Hello'
    content_type 'text/html; charset=UTF-8'
    body 'Hello, World'
  end
  gmail.deliver(email)
  gmail.logout
  email.message_id
end
def verify_sent_email(id)
  gmail = gmail_connect
  found = gmail.mailbox('sent').find(rfc822msgid: id).count
  gmail.logout
  ( found > 0 ) ? true : false
end
id = send_email
verify_sent_email(id)

最新更新