2

I want to send mail through Gmail Api. This my code:

@client = Google::APIClient.new
@client.authorization.access_token = access_token
@service = @client.discovered_api('gmail')

mail = Mail.new
mail.subject = subject
mail.from= from_email
mail.to= to_email
mail.part content_type: 'multipart/alternative' do |part|
   part.html_part = Mail::Part.new(body: message_html, content_type: 'text/html; charset=UTF-8')
   part.text_part = Mail::Part.new(body: message_text)
end

open('https://example.com/image.png') do |file| 
   mail.attachments['image.png'] = { mime_type: 'image/png', content:  file.read  }
end
results = @client.execute(
            api_method: @service.users.messages.to_h['gmail.users.messages.send'],
            parameters: { userId: 'me' },
            body_object: {
              raw: Base64.urlsafe_encode64(mail.to_s)
            },
          )

when i send mail with above code, i only recipient content with message_htmt without attachments.

Please guide me send mail with attachments.

Christian
  • 4,902
  • 4
  • 24
  • 42
bav ko ten
  • 502
  • 7
  • 24
  • Possible duplicate of [Rails: Send email using Gmail API with attachment return only encoded file not](https://stackoverflow.com/questions/48497583/rails-send-email-using-gmail-api-with-attachment-return-only-encoded-file-not) – Christian Aug 26 '19 at 22:25

1 Answers1

1

Use below code -

open('https://example.com/image.png') do |file| 
  mail.attachments['image.jpg'] = file.read 
end

For more info, check out this question.

fidato
  • 719
  • 5
  • 22