在ruby中使用gmail API发送HTML电子邮件

时间:2014-09-23 22:25:45

标签: ruby-on-rails ruby gmail

我正在创建一个ruby脚本,它应该执行上述操作。一天中,我试图破解我发送HTML电子邮件到选定数量的电子邮件地址。没有关于我应该怎么做的明确文件,所以我将非常感谢你的帮助。

这是我的代码,该脚本成功授权用户并选择代码来访问他/她的Gmail帐户。现在我想代表该用户发送HTML电子邮件。

require 'rubygems'
require 'google/api_client'
require 'launchy'

CLIENT_ID = 'my_app_Id_on_gmail_developers_console'
CLIENT_SECRET = 'the_secret_key'
OAUTH_SCOPE = 'https://mail.google.com/'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Create a new API client & load the Google Drive API
client = Google::APIClient.new(:application_name => 'Ruby Gmail sample',
                               :application_version => '1.0.0')
gmail = client.discovered_api('gmail', "v1")

# Request authorization
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.scope = OAUTH_SCOPE
client.authorization.redirect_uri = REDIRECT_URI

uri = client.authorization.authorization_uri
Launchy.open(uri)

# Exchange authorization code for access token
$stdout.write  "Enter authorization code: "
client.authorization.code = gets.chomp
client.authorization.fetch_access_token!

#testing if it is working well by counting the emails.
@emails = client.execute(
    api_method: gmail.users.messages.list,
    parameters: {
        userId: "me"},
    headers: {'Content-Type' => 'application/json'}
)

count = @emails.data.messages.count
puts "you have #{count} emails "
# Pretty print the API result
jj @emails.data.messages

我该怎么做?有没有办法可以将外部html文件作为要发送的电子邮件文件。那我可以用脚本发送这个文件吗?

2 个答案:

答案 0 :(得分:4)

我部分接受上面的答案,因为你可以很容易地通过STMP发送电子邮件,但使用gmail API它会更容易。根据你的代码,它应该是这样的:

message              = Mail.new
message.date         = Time.now
message.subject      = 'Supertramp'
message.body         = "<p>Hi Alex, how's life?</p>"
message.content_type = 'text/html'
message.from         = "Michal Macejko <michal@macejko.sk>"
message.to           = 'supetramp@alex.com'

service = client.discovered_api('gmail', 'v1')

result = client.execute(
  api_method: service.users.messages.to_h['gmail.users.messages.send'],
  body_object: {
    raw: Base64.urlsafe_encode64(message.to_s)
  },
  parameters:  {
    userId: 'michal@macejko.sk'
  },
    headers:   { 'Content-Type' => 'application/json' }
)

response = JSON.parse(result.body)

对于包含附件的多部分电子邮件:

message         = Mail.new
message.date    = Time.now
message.subject = 'Supertramp'
message.from    = "Michal Macejko <michal@macejko.sk>"
message.to      = 'supetramp@alex.com'

message.part content_type: 'multipart/alternative' do |part|
  part.html_part = Mail::Part.new(body: "<p>Hi Alex, how's life?</p>", content_type: 'text/html; charset=UTF-8')
  part.text_part = Mail::Part.new(body: "Hi Alex, how's life?")
end

open('http://google.com/image.jpg') do |file| 
  message.attachments['image.jpg'] = file.read 
end

答案 1 :(得分:2)

只是我的意见。我能够创建一个脚本,通过电子邮件将html通过电子邮件发送给大约100行的多个用户。不使用api。你需要研究使用smtp。这很简单。您定义了一个服务器供它使用,然后您使用它&#34; send_message&#34;方法。这是一个好网站的链接! GOOD SITE

出于安全原因,我无法在此处发布我的整个代码,但这应该可以帮助您开始

class Email_Client

attr_accessor :message_contents, :subject

  def initialize(sender_name, receiver_name, sender_email, receiver_email)
    @sender_name = sender_name
    @receiver_name = receiver_name
    @sender_email = sender_email
    @receiver_email = receiver_email
  end

    def send_html

message = <<MESSAGE
From: #{@sender_name} <#{@sender_email}>
To: #{@receiver_name} <#{@receiver_email}>
MIME-Version: 1.0
Content-type: text/html
Subject: #{subject}

#{message_contents}

MESSAGE

      Net::SMTP.start('SeRvEr_HeRe') do |smtp|
      smtp.send_message message,
      @sender_email, 
      @receiver_email
      end

end