问题描述
|
我正在使用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\",\"<[email protected]>\"])[0]
#=> 103
message = Gmail::Message.new(gmail.inbox,uid)
#=> #<Gmail::Message0x12a72e798 mailbox=INBOX uid=103>
message.subject
#=> \"hello world\"
message.message_id
#=> \"<[email protected]>\"
找不到方法可以通过message_id搜索。通过这种方式,您可以获得特定的电子邮件。
,使用标准的gmail gem,这似乎效果很好
messages = gmail.inbox.mails(:query => [\'HEADER\',\'Message-ID\',email.message_id])
,我可以使用此Gmail宝石来执行此操作(不确定是否与您使用的宝石相同)。
消息ID标头是所生成的电子邮件对象的一部分。然后可以使用rfc822msgid
进行搜索(在Gmail的“高级搜索”帮助页面中进行了介绍)。
这是一个例子:
def gmail_connect
Gmail.connect(email_address,password)
end
def send_email
gmail = gmail_connect
email = gmail.compose do
to [email protected]
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)