【问题标题】:how to send message using Gmail API with Ruby Google API Client?如何使用带有 Ruby Google API 客户端的 Gmail API 发送消息?
【发布时间】:2014-10-13 07:46:09
【问题描述】:

我在使用 API 时遇到了几个问题,

第一:

send 方法询问“id”(消息 id 或线程 id).. 但是为什么? 我正在发送新消息,所以它不应该需要 .根据 Gmail Api 文档 它是可选的。 https://developers.google.com/gmail/api/v1/reference/users/messages/send

ArgumentError: Missing required parameters: id.

秒:

即使在指定消息 ID 后,它也会返回此消息。

 Your client has issued a malformed or illegal request.

代码

 require 'mime'
 include MIME



msg = Mail.new
msg.date = Time.now
msg.subject = 'This is important'
msg.headers.set('Priority', 'urgent')

msg.body = Text.new('hello, world!', 'plain', 'charset' => 'us-ascii')
msg.from = {'hi@gmail.com' => 'Boss Man'}
msg.to   = {
    'list@example.com' => nil,
    'john@example.com' => 'John Doe',
    'jane@example.com' => 'Jane Doe',
}

@email = @google_api_client.execute(
    api_method: @gmail.users.messages.send(:get),
    body_object: {
        raw: Base64.urlsafe_encode64(msg.to_s)
    },
    parameters: {
        userId: 'me'
    }
)

当然身份验证工作正常。

其他一些方法也可以正常工作

喜欢:

get list of messages(Users.messages.list)

get single message(Users.messages.get)

但是

发送消息不起作用。

【问题讨论】:

    标签: ruby google-api gmail-api


    【解决方案1】:

    我认为

    @gmail.users.messages.send(:get) is equal to @gmail.users.messages.get
    

    因为“.send”是ruby方法

    所以现在这个方法可以用了

    @gmail.users.messages.to_h['gmail.users.messages.send']
    

    示例:

    msg = Mail.new
    msg.date = Time.now
    msg.subject = options[:subject]
    msg.body = Text.new(options[:message])
    msg.from = {@_user.email => @_user.full_name}
    msg.to   = {
        options[:to] => options[:to_name]
    }
    @email = @google_api_client.execute(
        api_method: @gmail.users.messages.to_h['gmail.users.messages.send'],
        body_object: {
            raw: Base64.urlsafe_encode64(msg.to_s)
        },
        parameters: {
            userId: 'me',
        }
    ) 
    

    谢谢。

    【讨论】:

    • 我的要点gist.github.com/joselo/8e7464df88344c69a42b 知道这个错误吗?数据:{"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"invalidArgument", "message"=>"Invalid to header"}], "code" =>400, "消息"=>"对标头无效"}}
    • @gmail 是从什么初始化的?
    【解决方案2】:

    我想您可能会看看我刚刚构建的这个 gem,它使用 Gmail API 而不是像其他 gem 那样使用 IMAP 和 SMTP:

    gem install gmail-api-ruby
    m = Gmail::Message.new(to: test@test.com, subject: "hello", html: "<b>this is html part<b>, text: "this is the text part")
    m.deliver
    

    gmail-api-ruby

    它带有许多您在 Gmail 界面中使用的有用方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2014-08-30
      • 2022-01-16
      • 2015-05-18
      • 1970-01-01
      • 2017-03-03
      相关资源
      最近更新 更多