Skip to content

Instantly share code, notes, and snippets.

@gabrielengel
Created August 9, 2014 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielengel/201e9661b4f92b8f543d to your computer and use it in GitHub Desktop.
Save gabrielengel/201e9661b4f92b8f543d to your computer and use it in GitHub Desktop.
New Ruby-Gmail Interface

Require gmail

require 'gmail'

Start an authenticated gmail session

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

Working with lists/mailboxes

Gmail.new(username, password) do 
  in('inbox')
  count # => 100

  search('is:unread')
  count # => 60

  search('is:read')
  count # => 40

  search('before:2010-02-20 after:2010-03-20')
  search('on:2010-04-15')
  search('from:myfriend@gmail.com')
  search('to:myfriend@gmail.com')
  search('is:read from:myboss@gmail.com "you are a great employee"')

  peek = true

  in('Urgent')
  count
end

Working with an email object

Gmail.new(username, password) do
  in('inbox')

  mails.each do |mail|
    mail.read!
    mail.unread!
    mail.archive!
    mail.delete!
    mail.spam!

    mail.move_to('label')
    mail.add_to('label')

    mail.files.each do |file|
      file = File.new('/tmp' + file.filename, "w+")
      file << file.decoded
      file.close
    end
  end
end

Working with labels

Gmail.new(username, password) do
  labels
  # => ['[Gmail] Inbox', '[Gmail] Sent', 'Family', 'BillsToPay', ...]
  labels.add('BotsSpam')
  labels.remove('BillsToPay')
end

Writing e-mails

Gmail.new(username, password) do
  compose do
    to "friend@gmail.com"
    text.body = "Hello"
    html.body = "Cool!"
    html.content_type = 'text/html; charset=UTF-8'
    attach '/tmp/some_image.jpg'
    inline '/tmp/some_other_image.jpg' # Exists today?
  end
end
@jabermohamed
Copy link

شكرا جزيلا علي صدوركم الرحبة وتعاونكم المستمر والذي اوصلني في نهاية المطاف إلى تحرير بريدي الالكتروني من ايدي المتقنعين والمتسترين باسم تطوير المواقع الاكترونية لقد عانيت لأكثر من 18شهرا بسبب شخص لااقول انه مجهول بل هومعروف احتجز حسابي الشخصي وبريدي الاكتروني ووصف صفحتي بحساب التحرك المريب وليبرر هذا الوصف ارسل الي صفحتي بكل مااوتي من القوة والعلم بارسال البرنامج الضار وعمل الف حيلة لوصف حسابي لانه تخرك مشبوه وانا أخذت عهدا مع نفسي لأهزم هذا الشرير والحمد لل
هذا الفضل والنصر اولا اهديهو لشركة قوقل العملاقة والتي كان تعرف ان من يتلاعب بصفتي هوشخصا دخيل ولست انا ومن اشكر جميع المواقع الأصيلة والمامؤمنة
شكرا للجميع
175bd948-ba32-40e8-9f26-bc15ce021ba5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment