Use ruby ​​to send and receive emails

xiaoxiao2021-03-06  68

1. Send Email via SMTP

Every week, Holden Glova, Pat Eyler, and Phil Thomson submit a Ruby Weekly News (RWN) article to the Ruby Garden website (http://www.rubygarden.org). A Ruby script receives this article via Email, convert it from the original XML format to HTML and plain text, then publish the HTML format to the website, then send a list of plain text formats to the mailing list. If there is any problem in this, such as an XML document structure is not right), this script will send an email that contains an error message to the sender.

This script sends Email with the Net :: SMTP (Simple Mail Transfer Protocol) library. Listing 1 is a method used to send email in this script that receives 3 parameters: email address, title, and letters content. Because this program is to be used in various control environments, some properties such as senders, forwarding Email, are defined as global constants, not parameters.

Listing 1: Send mail via SMTP

1 from_address = "Dave@pragprog.com"

2 SMTP_HOST = "Localhost"

3

4 Def Reply (To, Subject, MSG)

5 Mail = "to: # {to} / r / n"

6 "from: # {from_address} / r / n"

7 "Subject: # {Subject} / r / n"

8 "/ r / n"

9 msg

10

11 Net :: SMTP.Start (SMTP_HOST) DO | SMTP |

12 SMTP.SEND_MAIL (Mail, From_Address,

13 [TO, 'RURL_ARCHIVE@zip.local.pragprog.com'])

14 END

15 END

An Email message consists of two parts: envelope and content. The envelope tells the SMTP agent how to deliver messages. The content includes the message itself and some header (such as message subject), and some of the Header may be repeated in Envelope (such as "to" address), these duplicate headers are used to display And in envelope is used to deliver. (This is why you will receive the "to" address is not your spam)

You can see that the reply method has separated Envelop and Content, and the 5th line to the 9th line generates Content, which includes 3 headers: to, from, Subject, and then add message content behind an empty line. Pay attention to the header before the message main body must have a carriageway, ie "/ r" and "/ n" combination.

Method NET :: SMTP.Start Clauses and MTA (Mail Transfer Agent), one parameter of this method is the machine name running the MTA, and uses the default port (25), which returns an object to MTA interaction, And passed this object as a parameter to Block (11 lines to 13 lines). With Block, you can ensure that the connection can be closed after the block is completed. In our example, this interaction process is very simple, the 12th line of the line just sent the email content that just sword.

The second parameter of the Send_mail method is the use of the from address, which is a global variable. The third parameter is an array containing the recipient address. We sent this message to two places, a parameter specified to TO, and a local mailbox that archives all messages.

2. Receive and read mail with POP

It is very simple to receive emails from the POP server using Ruby. Suppose we have to give people a sense of language, people who participate in the survey can give a specific address by sending a title I like xxxxx, XXXXX is the name of the language of the sender's favorite language. Listing 3 Ruby script is used to receive results from the POP server and calculate, exist of a normal file, a file in each language.

Listing 3: Fetching Email with POP

1 Require 'Net / POP'

2

3 net :: pop3.delete_all ('pop3.server.address ", 110,

4 'YourAccount', 'Yourpassword') Do | Email |

5 HDR = email.header

6 IF HDR = ~ / Subject: / S I LIKE / S (/ W ) /

7 language = $ 1.upcase

8 else

9 language = "invalid"

10 end

11

12 count = (file.read (Language) Rescue "0")

13 file.open (Language, "W") {| f | f.puts old_count.succ}

14 END

The POP server stores the user's message. When you finish a message, you can choose to delete this letter, or put it on the server, in our example, we will delete it after reading it. Fortunately, Ruby provides a very convenient iterator delete_all, which removes the mail after handling these letters after processing. Delete_all needs to have the address and port of the POP server (the standard port is 110), as well as the username and password.

After this method begins with the specified parameter, a server is connected to the mail, and then the letter (as a net :: Popmail object) is passed to a given Block, when block After processing this letter, this letter will be deleted.

In this block, the 5th line takes all the Header of this letter to put it in a string. Then use a regular expression in the sixth line to find similar I like xxxx lines in the title (Subject), find the language representative of XXXX, then in the 12 and 13 lines selected for the language of the voter The count is updated.

The 12th line has a very interesting structure. Every time we get a vote for a language, we use a file to store the number of votes obtained in this language. We can read this value and add it, then write back to the file. However, when someone invoked a language, this file did not exist. When we read this file, you will get an exception, very lucky Ruby provides an exception mechanism (keyword rescue), but there is an abnormal time Because the file does not exist, it captures this exception, returns a default value 0, that is, the number of votes in this language is 0. Another small tip is the end_count.succ of the 13th line, and we use this to add a string. This is allowed in Ruby, if a string contains an integer, then this SUCC method returns a string of the next value containing this integer. ASTRING.SUCC = astring.to_i.succ.to_s.

Translator Note: Old_count.succ may be count.succ

转载请注明原文地址:https://www.9cbs.com/read-84265.html

New Post(0)