Track some emails with gmail gem

I use gmail gem to send emails and I need to track these emails. How can i do this?

I am trying to do an email search using message_id, but it will deliver all the emails from my inbox, and I only need the responses of a particular email.

Here is my actual code:

* save email using 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 for messages using message_id *

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

Hi,

Fabrizio Ferrari de Campos

+6
source share
3 answers

you can choose 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 >" 

not find a method that can be searched by message_id.via so you can get a specific email.

+3
source

Using standard gmail gem, this seems to work quite well

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

I was able to do this using this Gmail Gem (not sure if the same stone you are using).

The message identifier header is part of the email object being created. It can then be found using rfc822msgid (described in the Gmail advanced search help page ).

Here is 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) 
0
source

Source: https://habr.com/ru/post/888588/


All Articles