【发布时间】:2014-09-23 22:25:45
【问题描述】:
我正在创建一个 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 文件,它是要发送的电子邮件文件。那么我可以使用脚本发送这个文件吗?
【问题讨论】:
标签: ruby-on-rails ruby gmail