Sha256: ae68a78384c6f9be90bcab9d10aa986650d9acf1be23570637c2c157c897c00e

Contents?: true

Size: 1.63 KB

Versions: 18

Compression:

Stored size: 1.63 KB

Contents

# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'google/apis/gmail_v1'
require 'base_cli'
require 'rmail'

module Samples
  # Examples for the Gmail API
  #
  # Sample usage:
  #
  #     $ ./google-api-samples gmail send 'Hello there!' \
  #       --to='recipient@example.com' --from='user@example.com' \
  #       --subject='Hello'
  #
  class Gmail < BaseCli
    Gmail = Google::Apis::GmailV1

    desc 'send TEXT', 'Send a message with the gmail API'
    method_option :to, type: :string, required: true
    method_option :from, type: :string, required: true
    method_option :subject, type: :string, required: true
    def send(body)
      gmail = Gmail::GmailService.new
      gmail.authorization = user_credentials_for(Gmail::AUTH_SCOPE)

      message = RMail::Message.new
      message.header['To'] = options[:to]
      message.header['From'] = options[:from]
      message.header['Subject'] = options[:subject]
      message.body = body

      gmail.send_user_message('me',
                              upload_source: StringIO.new(message.to_s),
                              content_type: 'message/rfc822')
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
google-api-client-0.9.20 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.19 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.18 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.15 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.14 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.13 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.12 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.11 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.9 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.8 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.6 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.5 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.4 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.3 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.2 samples/cli/lib/samples/gmail.rb
google-api-client-0.9.1 samples/cli/lib/samples/gmail.rb
google-api-client-0.9 samples/lib/samples/gmail.rb
google-api-client-0.9.pre5 samples/lib/samples/gmail.rb