I'm trying to send an email with the Microsoft365R package. There's an embedded image I need in it. I've tried two ways:
library(Microsoft365R)
emails <- c("myemail@example.com")
for(i in emails){
my_outlook <- get_business_outlook()
my_email <- my_outlook$create_email()$
set_body("<img src=`https://raw.githubusercontent.com/iagocnunes/test/main/002.png` alt=`image`>", content_type="html")$
set_subject("Test")$
set_recipients(to = i)$
send()
}
And:
for(i in emails){
my_outlook <- get_business_outlook()
my_email <- my_outlook$create_email()$
set_body("<b>test testing", content_type="html")$
add_image("https://raw.githubusercontent.com/iagocnunes/test/main/002.png")$
set_subject("Test")$
set_recipients(to = i)$
send()
}
In both the image fails to embed. Why is this happening and how can I get it right?
A few things could cause an image not to render as expected.
In this case, the backtick characters should be replaced with '
single quotes and it should work. E.g. this should work
library(Microsoft365R)
emails <- c("myemail@example.com")
for(i in emails){
my_outlook <- get_business_outlook()
my_email <- my_outlook$create_email()$
set_body("<img src='https://raw.githubusercontent.com/iagocnunes/test/main/002.png' alt='image'>", content_type="html")$
set_subject("Test")$
set_recipients(to = i)$
send()
}
A second thing that can cause images not to render as expected is if the image's mimetype isn't what it appears to be. Basically, we could reasonably assume that if a file has the extension .png
, that its mimetype is also png. However, sometimes someone will have an unsupported image type, they'll rename it (giving it a different extension), but it won't change it's mimetype and so it won't render (this happened to me once with the webp
file type). I checked your image's mimetype here and confirm it's mimetype is image/png
, which is a supported type.
One last thing that could cause an image not to render (you already have this) is if the content_type="html"
argument is missing in set_body()
(see microsoft365R manual):
The object argument should be a filename, and the message content type will be set to "html"