Ruby implements the active mail push trigger program, and ruby implements mail triggering
After receiving the email, the mail server pushes the notification program. Is there any way to achieve this?
1. Client polling
2. The server actively pushes data.
First, familiarize yourself with the protocol for sending and receiving emails:
Net: SMTP (send mail)
Net: POP3 (receive mail)
Net: IMAP (receive mail)
There are many examples of using pop3 to receive mails on the Internet. However, using pop3 to receive mails can only obtain all emails in the inbox, and the mark of whether the emails are read cannot be obtained. Using the imap Protocol avoids this embarrassment, imap not only obtains the details of an email (such as whether the email has been read or replied), but also allows you to change the mail tag. Currently, there are not many email servers that support the imap protocol, I only know 21cn and gmail. The following example uses proxy and SSL authentication. For more information, see.
Imap emails are all requested on demand. That is to say, when you get a Message object, there is actually no information in it. When you get information in this object using the get method, for example, if getSubject is used, the Message object will re-access the mail server to obtain the Message. Therefore, you cannot close the directory or disconnect the connection before obtaining all required information. If you want to operate the Message object after the directory is closed or connected, you need to use the fetch method of the Folder object to obtain the required information.
I. Client polling
Below, pop3 and imap are used to show the example of Polling Access to get Emails:
POP3 round robin:
Copy codeThe Code is as follows:
Loop do
Require 'net/Pop'
Pop = Net: POP3.new ('emailservice ')
Pop. start ('usename', 'Password ')
If pop. mails. empty?
Puts 'no mail .'
Else
Pop. each_mail do | m |
M. pop do | chunk |
P chunk
End
End
Puts "# {pop. mails. size} mails popped ."
End
Pop. finish
Sleep (10)
End
Imap round robin:
Copy codeThe Code is as follows:
Loop do
Require 'net/imap'
Imap = Net: IMAP. new ('emailservice ')
Imap. login "USERNAME", "PASSWORD"
Imap. examine ('inbox ')
Imap. search (["BEFORE", "29-Oct-2014", "SINCE", "28-Oct-2014"]). each do | message_id |
Envelope = imap. fetch (message_id, "ENVELOPE") [0]. attr ["ENVELOPE"]
Puts "# {envelope. from [0]. name }:\ t # {envelope. subject }"
End
Sleep (10)
End
Ii. Active Server push
Below is an active server push method: (IMAP. IDLE)
This is a technology between pull and Persistent TCP/IP: long polling ). The principle is that each request sent by the client to the service is held by the server. After a message is returned or time out, the client initiates a request again, waiting for the message to arrive. This mode does not require heartbeat or persistent TCP usage. It is suitable for timely message pushing on the page end.
Copy codeThe Code is as follows:
SERVER = 'emailservice'
USERNAME = 'username'
PW = 'Password'
Require 'net/imap'
# Extend support for idle command. See online.
# Http://www.ruby-forum.com/topic/50828
# Https://gist.github.com/jem/2783772
# But that was wrong. see/opt/ruby-1.9.1-p243/lib/net/imap. rb.
Class Net: IMAP
Def idle
Cmd = "IDLE"
Synchronize do
@ Idle_tag = generate_tag
Put_string (@ idle_tag + "" + cmd)
Put_string (CRLF)
End
End
Def say_done
Cmd = "DONE"
Synchronize do
Put_string (cmd)
Put_string (CRLF)
End
End
Def await_done_confirmation
Synchronize do
Get_tagged_response (@ idle_tag, nil)
Puts 'Just got confirmation'
End
End
End
Class Remailer
Attr_reader: imap
Public
Def initialize
@ Imap = nil
@ Mailer = nil
Start_imap
End
Def tidy
Stop_imap
End
Def print_pust
Envelope = @ imap. fetch (-1, "ENVELOPE") [0]. attr ["ENVELOPE"]
Puts "From: # {envelope. from [0]. name} \ t Subject: # {envelope. subject }"
End
Def bounce_idle
# Bounces the idle command.
@ Imap. say_done
@ Imap. await_done_confirmation
# Do a manual check, just in case things aren't working properly.
@ Imap. idle
End
Private
Def start_imap
@ Imap = Net: IMAP. new ('pop .i-click.com ')
@ Imap. login USERNAME, PW
@ Imap. select 'inbox'
# Add handler.
@ Imap. add_response_handler do | resp |
If resp. kind_of? (Net: IMAP: UntaggedResponse) and resp. name = "EXISTS"
@ Imap. say_done
Thread. new do
@ Imap. await_done_confirmation
Print_pust
@ Imap. idle
End
End
End
@ Imap. idle
End
Def stop_imap
@ Imap. done
End
End
Begin
Net: IMAP. debug = true
R = Remailer. new
Loop do
Puts 'bouncing ...'
R. bounce_idle
Sleep 15*60
# Generally, no operation is maintained for a long link within 15 minutes.
End
Ensure
R. tidy
End