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

6
我正在使用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)

敬礼,

Fabrício Ferrari de Campos


你使用的是哪个 Gmail gem?提供仓库链接会更有帮助。 - FelixD
3个回答

3

使用标准的gmail gem,这似乎可以很好地工作

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

3

你可以看一下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搜索的方法。通过这种方式,您可以获取特定的电子邮件。


0

我能够使用这个 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接