Track some emails with gmail gem
I'm using gmail gem to send emails and I need track these emails. How can I do this?
I'm trying search the email with the message_id, but it bring all emails from my inbox and I want just the responses of a specific email.
Here is my actual code:
*save email with the message_id*
mail = gmail.deliver(email开发者_JS百科)
Email.create(:message_id => mail.message_id, :from => user.email,
:to => annotation.to, :body => annotation.content, :title => annotation.title,
:annotation => annotation, :user => user)
*search the mails with message_id*
messages = gmail.inbox.mails(:message_id => email.message_id)
Regards,
Fabrício Ferrari de Campos
you can take Net::IMAP a look.
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>"
have not find a method can search by message_id.via this way you can get a specific email.
Using the standard gmail gem, this seems to work quite well
messages = gmail.inbox.mails(:query => ['HEADER', 'Message-ID', email.message_id])
I was able to do this using this Gmail Gem (not sure if that's the same gem you're using).
The Message ID header is part of the email object that is generated. It is then searchable using rfc822msgid
(described in Gmail's Advanced Search help page).
Here's an example:
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)
精彩评论