Reputation: 1090
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)
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
Upvotes: 6
Views: 1341
Reputation: 311
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 [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)
Upvotes: 0
Reputation: 1145
Using the standard gmail gem, this seems to work quite well
messages = gmail.inbox.mails(:query => ['HEADER', 'Message-ID', email.message_id])
Upvotes: 3
Reputation: 444
you can take Net::IMAP a look.
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]>"
have not find a method can search by message_id.via this way you can get a specific email.
Upvotes: 3