ruby-gmail

Gmail 的 Rubyesque 界面。 通过 IMAP 连接到 Gmail 并操纵电子邮件和标签。 通过 SMTP 使用您的 Gmail 帐户发送电子邮件。 包括完全支持解析和生成 MIME 消息。(A Rubyesque interface to Gmail. Connect to Gmail via IMAP and manipulate emails and labels. Send email with your Gmail account via SMTP. Includes full support for parsing and generating MIME messages.)

  • Owner: dcparker/ruby-gmail
  • Platform: Linux, Mac, Windows
  • License::
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

请注意

作者已将 verion 0.3.1 推送到 rubygems.org(是的,感谢 Nick Quaranto 的帮助),这是一个直接来自 master 的构建。

其次,该 GEM 正在回归正轨。 有关详细信息,请参阅此处的此问题

描述

Gmail 的 Rubyesque 界面,包含您需要的所有工具。 搜索,阅读和发送 multipart 电子邮件; 存档,标记为已读/未读,删除电子邮件; 并管理标签。

特性

  • 搜索电子邮件
  • 阅读电子邮件(处理附件)
  • 电子邮件:标记,存档,删除,标记为已读/未读/垃圾邮件
  • 创建和删除标签
  • 使用内嵌图像和附件以纯文本和/或html创建和发送多部分电子邮件
  • 利用Gmail的IMAP和SMTP,MIME类型检测和解析并正确生成MIME。

问题:

  • 可能无法正确读取格式错误的MIME邮件。 这可以通过让IMAP解析MIME结构来纠正。
  • 如果没有抓取附件,也无法获取普通或HTML消息。 lazy-[down] 加载附件可能会很好。

Main metrics

Overview
Name With Ownerdcparker/ruby-gmail
Primary LanguageRuby
Program languageRuby (Language Count: 1)
PlatformLinux, Mac, Windows
License:
所有者活动
Created At2009-11-19 04:32:25
Pushed At2019-06-27 22:07:08
Last Commit At2019-01-22 02:06:46
Release Count11
Last Release Namev0.3.1 (Posted on )
First Release Namev0.0.1 (Posted on )
用户参与
Stargazers Count791
Watchers Count41
Fork Count123
Commits Count93
Has Issues Enabled
Issues Count73
Issue Open Count23
Pull Requests Count14
Pull Requests Open Count3
Pull Requests Close Count7
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Notice

I have pushed verion 0.3.1 to rubygems.org (Yay, thanks Nick Quaranto for the help) which is a build straight from master.

Second, this gem is getting back on track. See this issue here for more information.

ruby-gmail

Author(s)

  • Daniel Parker of BehindLogic.com
  • Nathan Herald

Extra thanks for specific feature contributions from:

Description

A Rubyesque interface to Gmail, with all the tools you'll need. Search, read and send multipart emails; archive, mark as read/unread, delete emails; and manage labels.

Features

  • Search emails
  • Read emails (handles attachments)
  • Emails: Label, archive, delete, mark as read/unread/spam
  • Create and delete labels
  • Create and send multipart email messages in plaintext and/or html, with inline images and attachments
  • Utilizes Gmail's IMAP & SMTP, MIME-type detection and parses and generates MIME properly.

Problems:

  • May not correctly read malformed MIME messages. This could possibly be corrected by having IMAP parse the MIME structure.
  • Cannot grab the plain or html message without also grabbing attachments. It might be nice to lazy-[down]load attachments.

Example Code:

1) Require gmail

require 'gmail'

2) Start an authenticated gmail session

#    If you pass a block, the session will be passed into the block,
#    and the session will be logged out after the block is executed.
gmail = Gmail.new(username, password)
# ...do things...
gmail.logout

Gmail.new(username, password) do, gmail, # ...do things...
end

3) Count and gather emails!

# Get counts for messages in the inbox
gmail.inbox.count
gmail.inbox.count(:unread)
gmail.inbox.count(:read)

# Count with some criteria
gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
gmail.inbox.count(:on => Date.parse("2010-04-15"))
gmail.inbox.count(:from => "myfriend@gmail.com")
gmail.inbox.count(:to => "directlytome@gmail.com")

# Combine flags and options
gmail.inbox.count(:unread, :from => "myboss@gmail.com")

# Labels work the same way as inbox
gmail.mailbox('Urgent').count

# Getting messages works the same way as counting: optional flag, and optional arguments
# Remember that every message in a conversation/thread will come as a separate message.
gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "myboss@gmail.com")

# Search the same way you do on Gmail's web interface:
gmail.inbox.emails(gm: 'before:2010-02-20 after:2010-03-20')
gmail.inbox.emails(gm: 'on:2010-04-15')
gmail.inbox.emails(gm: 'from:myfriend@gmail.com')
gmail.inbox.emails(gm: 'to:myfriend@gmail.com')
gmail.inbox.emails(gm: 'is:read from:myboss@gmail.com "you are a great employee"')

# Get messages without marking them as read on the server.
gmail.peek = true
gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "myboss@gmail.com")

4) Work with emails!

# any news older than 4-20, mark as read and archive it...
gmail.inbox.emails(:before => Date.parse("2010-04-20"), :from => "news@nbcnews.com").each do, email, email.mark(:read) # can also mark :unread or :spam
  email.archive!
end

# delete emails from X...
gmail.inbox.emails(:from => "x-fiancé@gmail.com").each do, email, email.delete!
end

# Save all attachments in the "Faxes" label to a folder
folder = "/where/ever"
gmail.mailbox("Faxes").emails.each do, email, email.attachments.each do, attachment, file = File.new(folder + attachment.filename, "w+")
    file << attachment.decoded
    file.close
  end
end

# Add a label to a message
email.label("Faxes")

# Or "move" the message to a label
email.move_to("Faxes")

5) Create new emails!

Creating emails now uses the amazing Mail rubygem. See its documentation here. Ruby-gmail will automatically configure your Mail emails to be sent via your Gmail account's SMTP, so they will be in your Gmail's "Sent" folder. Also, no need to specify the "From" email either, because ruby-gmail will set it for you.

gmail.deliver do
  to "email@example.com"
  subject "Having fun in Puerto Rico!"
  text_part do
    body "Text of plaintext message."
  end
  html_part do
    content_type 'text/html; charset=UTF-8'
    body "<p>Text of <em>html</em> message.</p>"
  end
  add_file "/path/to/some_image.jpg"
end
# Or, generate the message first and send it later
email = gmail.generate_message do
  to "email@example.com"
  subject "Having fun in Puerto Rico!"
  body "Spent the day on the road..."
end
email.deliver!
# Or...
gmail.deliver(email)

Requirements

  • ruby
  • net/smtp
  • net/imap
  • tmail
  • shared-mime-info rubygem (for MIME-detection when attaching files)

Install

gem install ruby-gmail

License

(The MIT License)

Copyright (c) 2009 BehindLogic

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.