Anna Yashina
Anna Yashina

Reputation: 534

Attach files to MIME object in gmailr

I'm struggling to define a proper mime type while trying to attach a file to a letter. I'm testing the function with png-image but eventually I'll need to attach a pdf.

library(gmailr)

test_email <- mime(
To = "[email protected]",
From = sender_account,
Subject = "this is just a gmailr test",
body = "Can you hear me now?") %>% 
attach_file(file = "health.png", type = "image/png")
send_message(test_email)

And get something like this instead of file attached:

Can you hear me now? --29c4c91341434848f627ac9c696d9ed9--

What am I doing wrong?

Upvotes: 1

Views: 970

Answers (2)

Yuriy Barvinchenko
Yuriy Barvinchenko

Reputation: 1595

Following discussion here: here now its fixed. You need to update gmailr to version 1.0.0 and higher and update your code like this

pkgload::load_all("~/p/gmailr")
write.csv(iris, "iris.csv")
gm_mime() %>%
  gm_from("[email protected]") %>%
  gm_to("[email protected]") %>%
  gm_subject("I bought you") %>%
  gm_text_body('Some flowers!') %>%
  gm_attach_file("iris.csv") %>%
  gm_create_draft()

Many function names changed by just adding 'gm_' (for example mime is gm_mime now) but pay attention to authentication, these functions changed much more.

Upvotes: 1

David
David

Reputation: 1979

This issue is discussed here:

https://github.com/jimhester/gmailr/issues/60

If it is acceptable that the body still contains the --numbers-- string, the following solution worked for me:

import(gmailr)

msg <- "This is the message body.\n\n"

email <- mime(
      To = "[email protected]",
      From = "[email protected]",
      Subject = "Important subject",
      body = msg)

email <- attach_part(email, msg)
email <- attach_file(email, file = "att1.txt", type = "text/plain")
email <- attach_file(email, file = "att2.txt", type = "text/plain")
email <- attach_file(email, file = "att3.pdf", type = "application/pdf")

send_message(email)

Upvotes: 4

Related Questions