Search code examples
rgmailgmail-apimime

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?


Solution

  • 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)