Skip to content

Commit

Permalink
Add syntax highlighting to the README. From: dcparker#75
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Hanks, PMP committed Nov 12, 2015
1 parent 35f617f commit 7746623
Showing 1 changed file with 104 additions and 88 deletions.
192 changes: 104 additions & 88 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ Second, this gem is getting back on track. See [this issue here](https://github.

Extra thanks for specific feature contributions from:

* [Justin Perkins](http://github.com/justinperkins)
* [Mikkel Malmberg](http://github.com/mikker)
* [Julien Blanchard](http://github.com/julienXX)
* [Federico Galassi](http://github.com/fgalassi)
* [Justin Perkins](https://github.com/justinperkins)
* [Mikkel Malmberg](https://github.com/mikker)
* [Julien Blanchard](https://github.com/julienXX)
* [Federico Galassi](https://github.com/fgalassi)
* [Foo Bar](https://github.com/strcmp)
* [Ryan Jacobs](https://github.com/ryanmjacobs)
* [Gabriel Engel](https://github.com/gabrielengel)
* [Steve Cockram](https://github.com/scockram)

## Description

Expand All @@ -43,101 +47,111 @@ A Rubyesque interface to Gmail, with all the tools you'll need. Search, read and

### 1) Require gmail

require 'gmail'

```ruby
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
```ruby
# 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
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 => "[email protected]")
gmail.inbox.count(:to => "[email protected]")

# Combine flags and options
gmail.inbox.count(:unread, :from => "[email protected]")

# 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 => "[email protected]")

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


```ruby
# 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 => "[email protected]")
gmail.inbox.count(:to => "[email protected]")

# Combine flags and options
gmail.inbox.count(:unread, :from => "[email protected]")

# 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 => "[email protected]")

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

### 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 => "[email protected]").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")
```ruby
# any news older than 4-20, mark as read and archive it...
gmail.inbox.emails(:before => Date.parse("2010-04-20"), :from => "[email protected]").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](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.

gmail.deliver do
to "[email protected]"
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 protected]"
subject "Having fun in Puerto Rico!"
body "Spent the day on the road..."
end
email.deliver!
# Or...
gmail.deliver(email)
```ruby
gmail.deliver do
to "[email protected]"
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 protected]"
subject "Having fun in Puerto Rico!"
body "Spent the day on the road..."
end
email.deliver!
# Or...
gmail.deliver(email)
```

## Requirements

Expand All @@ -149,7 +163,9 @@ Creating emails now uses the amazing [Mail](http://rubygems.org/gems/mail) rubyg

## Install

gem install ruby-gmail
```ruby
gem install ruby-gmail
```

## License

Expand Down

0 comments on commit 7746623

Please sign in to comment.