2014-09-23 35 views
1

我正在創建一個ruby腳本,它應該執行上述操作。在這一天,我試圖破解我的方式發送一個HTML電子郵件到選定數量的電子郵件地址。關於我應該怎麼做沒有明確的文件,所以請我會感謝您的幫助。在ruby中使用gmail API發送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

只是我的輸入。我能夠創建一個腳本,通過電子郵件將HTML發送給約100行的多個用戶。不使用api。你需要考慮使用smtp。這很簡單。你定義一個服務器供它使用,然後你使用它的「send_message」方法。這是一個好網站的鏈接! 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 
3

我非常部分容易接受上面,因爲你可以通過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 <[email protected]>" 
message.to   = '[email protected]' 

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: '[email protected]' 
    }, 
    headers: { 'Content-Type' => 'application/json' } 
) 

response = JSON.parse(result.body) 

對於帶有附件的多部分電子郵件:

message   = Mail.new 
message.date = Time.now 
message.subject = 'Supertramp' 
message.from = "Michal Macejko <[email protected]>" 
message.to  = '[email protected]' 

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