PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/README.markdown

https://github.com/hbdev/ruby-gmail
Markdown | 183 lines | 137 code | 46 blank | 0 comment | 0 complexity | 51c7cb2ad7f62a458b10ceae10097bde MD5 | raw file
  1. # Notice
  2. I am looking for for a new project owner for this project. Please contact me if you are interested in maintaining this
  3. project or being added as a contributor.
  4. Sincerely,
  5. [Joshavne Potter](mailto:yourtech@gmail.com?subject=ruby-gmail%20gem%20support)
  6. # ruby-gmail
  7. * Homepage: [http://dcparker.github.com/ruby-gmail/](http://dcparker.github.com/ruby-gmail/)
  8. * Code: [http://github.com/dcparker/ruby-gmail](http://github.com/dcparker/ruby-gmail)
  9. * Gem: [http://gemcutter.org/gems/ruby-gmail](http://gemcutter.org/gems/ruby-gmail)
  10. ## Author(s)
  11. * Daniel Parker of BehindLogic.com
  12. Extra thanks for specific feature contributions from:
  13. * [Justin Perkins](http://github.com/justinperkins)
  14. * [Mikkel Malmberg](http://github.com/mikker)
  15. * [Julien Blanchard](http://github.com/julienXX)
  16. * [Federico Galassi](http://github.com/fgalassi)
  17. ## Description
  18. 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.
  19. ## Features
  20. * Search emails
  21. * Read emails (handles attachments)
  22. * Emails: Label, archive, delete, mark as read/unread/spam
  23. * Create and delete labels
  24. * Create and send multipart email messages in plaintext and/or html, with inline images and attachments
  25. * Utilizes Gmail's IMAP & SMTP, MIME-type detection and parses and generates MIME properly.
  26. ## Problems:
  27. * May not correctly read malformed MIME messages. This could possibly be corrected by having IMAP parse the MIME structure.
  28. * Cannot grab the plain or html message without also grabbing attachments. It might be nice to lazy-[down]load attachments.
  29. ## Example Code:
  30. ### 1) Require gmail
  31. require 'gmail'
  32. ### 2) Start an authenticated gmail session
  33. # If you pass a block, the session will be passed into the block,
  34. # and the session will be logged out after the block is executed.
  35. gmail = Gmail.new(username, password)
  36. # ...do things...
  37. gmail.logout
  38. Gmail.new(username, password) do |gmail|
  39. # ...do things...
  40. end
  41. ### 3) Count and gather emails!
  42. # Get counts for messages in the inbox
  43. gmail.inbox.count
  44. gmail.inbox.count(:unread)
  45. gmail.inbox.count(:read)
  46. # Count with some criteria
  47. gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
  48. gmail.inbox.count(:on => Date.parse("2010-04-15"))
  49. gmail.inbox.count(:from => "myfriend@gmail.com")
  50. gmail.inbox.count(:to => "directlytome@gmail.com")
  51. # Combine flags and options
  52. gmail.inbox.count(:unread, :from => "myboss@gmail.com")
  53. # Labels work the same way as inbox
  54. gmail.mailbox('Urgent').count
  55. # Getting messages works the same way as counting: optional flag, and optional arguments
  56. # Remember that every message in a conversation/thread will come as a separate message.
  57. gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "myboss@gmail.com")
  58. # Get messages without marking them as read on the server.
  59. gmail.peek = true
  60. gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "myboss@gmail.com")
  61. ### 4) Work with emails!
  62. # any news older than 4-20, mark as read and archive it...
  63. gmail.inbox.emails(:before => Date.parse("2010-04-20"), :from => "news@nbcnews.com").each do |email|
  64. email.mark(:read) # can also mark :unread or :spam
  65. email.archive!
  66. end
  67. # delete emails from X...
  68. gmail.inbox.emails(:from => "x-fiancé@gmail.com").each do |email|
  69. email.delete!
  70. end
  71. # Save all attachments in the "Faxes" label to a folder
  72. folder = "/where/ever"
  73. gmail.mailbox("Faxes").emails.each do |email|
  74. if !email.message.attachments.empty?
  75. email.message.save_attachments_to(folder)
  76. end
  77. end
  78. # Save just the first attachment from the newest unread email (assuming pdf)
  79. # For #save_to_file:
  80. # + provide a path - save to attachment filename in path
  81. # + provide a filename - save to file specified
  82. # + provide no arguments - save to attachment filename in current directory
  83. email = gmail.inbox.emails(:unread).first
  84. email.attachments[0].save_to_file("/path/to/location")
  85. # Add a label to a message
  86. email.label("Faxes")
  87. # Or "move" the message to a label
  88. email.move_to("Faxes")
  89. ### 5) Create new emails!
  90. Creating emails now uses the amazing [Mail](http://rubygems.org/gems/mail) rubygem. See its [documentation here](http://github.com/mikel/mail). 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.
  91. gmail.deliver do
  92. to "email@example.com"
  93. subject "Having fun in Puerto Rico!"
  94. text_part do
  95. body "Text of plaintext message."
  96. end
  97. html_part do
  98. body "<p>Text of <em>html</em> message.</p>"
  99. end
  100. add_file "/path/to/some_image.jpg"
  101. end
  102. # Or, generate the message first and send it later
  103. email = gmail.generate_message do
  104. to "email@example.com"
  105. subject "Having fun in Puerto Rico!"
  106. body "Spent the day on the road..."
  107. end
  108. email.deliver!
  109. # Or...
  110. gmail.deliver(email)
  111. ## Requirements
  112. * ruby
  113. * net/smtp
  114. * net/imap
  115. * tmail
  116. * shared-mime-info rubygem (for MIME-detection when attaching files)
  117. ## Install
  118. gem install ruby-gmail
  119. ## License
  120. (The MIT License)
  121. Copyright (c) 2009 BehindLogic
  122. Permission is hereby granted, free of charge, to any person obtaining
  123. a copy of this software and associated documentation files (the
  124. 'Software'), to deal in the Software without restriction, including
  125. without limitation the rights to use, copy, modify, merge, publish,
  126. distribute, sublicense, and/or sell copies of the Software, and to
  127. permit persons to whom the Software is furnished to do so, subject to
  128. the following conditions:
  129. The above copyright notice and this permission notice shall be
  130. included in all copies or substantial portions of the Software.
  131. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  132. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  133. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  134. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  135. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  136. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  137. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.