These functions use the Microsoft365R R package to send emails via Microsoft 365. They require an Outlook Business account.

mail(
  body,
  subject = "",
  to = NULL,
  cc = read_secret("mail.auto_cc"),
  bcc = read_secret("mail.auto_bcc"),
  reply_to = NULL,
  attachment = NULL,
  header = FALSE,
  footer = FALSE,
  background = certestyle::colourpicker("certeblauw6"),
  send = TRUE,
  markdown = TRUE,
  signature = get_certe_signature(account = account),
  automated_notice = !interactive(),
  save_location = read_secret("mail.export_path"),
  sent_subfolder = read_secret("mail.sent_subfolder"),
  expect = NULL,
  account = connect_outlook(),
  identifier = NULL,
  ...
)

mail_plain(
  body,
  subject = "",
  to = NULL,
  cc = read_secret("mail.auto_cc"),
  bcc = read_secret("mail.auto_bcc"),
  reply_to = read_secret("mail.auto_reply_to"),
  attachment = NULL,
  send = TRUE,
  ...
)

mail_image(image_path, width = NULL, ...)

mail_on_error(expr, to = read_secret("mail.error_to"), ...)

mail_is_sent(project_number, date = Sys.Date(), account = connect_outlook())

Arguments

body

body of email, allows markdown if markdown = TRUE

subject

subject of email

to

field 'to', can be character vector

cc

field 'CC', can be character vector

bcc

field 'BCC', can be character vector

reply_to

field 'reply-to'

attachment

character (vector) of file location(s), or (a list of) data.frames

extra text for header or footer, allows markdown if markdown = TRUE. See blastula::blocks() to build blocks for these sections.

background

background of the surrounding area in the email. Use "", NULL or FALSE to remove background.

send

directly send email, FALSE will show the email in the Viewer pane and will ask whether the email should be saved to the Drafts folder of the current Microsoft 365 user.

markdown

treat body, header and footer as markdown

signature

text to print as email signature, or NULL to omit it, defaults to get_certe_signature()

automated_notice

a logical to print a notice that the mail was sent automatically (default is TRUE if not in interactive() mode)

save_location

location to save email object to, which consists of all email details and can be printed in the R console

sent_subfolder

mail folder within Sent Items in the Microsoft 365 account, to store the mail if !interactive()

expect

expression which should return TRUE prior to sending the email

account

a Microsoft 365 account to use for sending the mail. This has to be an object as returned by connect_outlook() or Microsoft365R::get_business_outlook(). Using account = FALSE is equal to setting send = FALSE.

identifier

a mail identifier to be printed at the bottom of the email. Defaults to project_identifier(). Use FALSE to not print an identifier.

...

arguments for mail()

image_path

path of image

width

required width of image, must be in CSS style such as "200px" or "100%"

expr

expression to test, an email will be sent if this expression returns an error

project_number

Number of a project. Will be used to check the grey identifier in the email.

date

A date, defaults to today. Will be evaluated in as.Date(). Can also be of length 2 for a date range.

Details

mail_on_error() can be used for automated scripts.

mail_plain() sends a plain email, without markdown support and with no signature.

Use mail_is_sent() to check whether a project email was sent on a certain date from any Sent Items (sub)folder. The function will search for the grey identifier in the email body, which is formatted as [-yymmdd][0-9]+[-project_number][^0-9a-z]. It returns TRUE if any email was found, and FALSE otherwise. If TRUE, the name will contain the date(s) and time(s) of the sent email.

Examples

mail("test123", "test456", to = "mail@domain.com", account = NULL)
#> No valid Microsoft 365 account set with argument `account`, forcing `send = FALSE`
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Mail Summary
#> Subject:   test456
#> Body text: test123 *Deze mail is geautomatiseerd verstuurd.*
#> From:      NA
#> To:        mail@domain.com
#> CC:        
#> BCC:       
#> Created:   10 juli 2024 08:36:05

mail_plain("test123", "test456", to = "mail@domain.com", account = NULL)
#> No valid Microsoft 365 account set with argument `account`, forcing `send = FALSE`
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Mail Summary
#> Subject:   test456
#> Body text: test123
#> From:      NA
#> To:        mail@domain.com
#> CC:        
#> BCC:       
#> Created:   10 juli 2024 08:36:05

mail(mail_image(image_path = system.file("test.jpg", package = "certemail")),
    "test456", to = "mail@domain.com", account = NULL)
#> No valid Microsoft 365 account set with argument `account`, forcing `send = FALSE`
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Mail Summary
#> Subject:   test456
#> Body text:             *Deze mail is geautomatiseerd verstuurd.*
#> From:      NA
#> To:        mail@domain.com
#> CC:        
#> BCC:       
#> Created:   10 juli 2024 08:36:05

# data.frames will be transformed with certestyle::plain_html_table()
mail(body = mtcars[1:5, ],
     subject = "Check these cars!",
     to = "somebody@domain.org",
     account = FALSE)
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Mail Summary
#> Subject:   Check these cars!
#> Body text:  mpgcyldisphpdratwtqsecvsamgearcarbMazda RX421.061601103.902.6216.4...
#> From:      NA
#> To:        somebody@domain.org
#> CC:        
#> BCC:       
#> Created:   10 juli 2024 08:36:05

# but better is to add as an attachment - they will become Excel files
mail(body = "Hello there",
     subject = "Check these cars!",
     to = "somebody@domain.org",
     attachment = mtcars[1:5, ],
     account = FALSE)
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: Row names for object 1 (5x11, sheet 'Blad1') added as first column 'rownames'
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Exported data set (5x11) to '/tmp/RtmpCYbS9C/tabel_1.xlsx' (7.4 kB).
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Mail Summary
#> Subject:   Check these cars!
#> Body text: Hello there *Deze mail is geautomatiseerd verstuurd.*
#> From:      NA
#> To:        somebody@domain.org
#> CC:        
#> BCC:       
#> Created:   10 juli 2024 08:36:06
#> Attachments:
#> - /tmp/RtmpCYbS9C/tabel_1.xlsx

# use list() to add multiple data sets
mail(body = "Hello there",
     subject = "Check these cars and flowers!",
     to = "somebody@domain.org",
     attachment = list(mtcars[1:5, ],
                       iris),
     account = FALSE)
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: Row names for object 1 (5x11, sheet 'Blad1') added as first column 'rownames'
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Exported data set (5x11) to '/tmp/RtmpCYbS9C/tabel_1.xlsx' (7.4 kB).
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Exported data set (150x5) to '/tmp/RtmpCYbS9C/tabel_2.xlsx' (10.3 kB).
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Warning: In read_secret(): environmental variable 'secrets_file' not set
#> Mail Summary
#> Subject:   Check these cars and flowers!
#> Body text: Hello there *Deze mail is geautomatiseerd verstuurd.*
#> From:      NA
#> To:        somebody@domain.org
#> CC:        
#> BCC:       
#> Created:   10 juli 2024 08:36:07
#> Attachments:
#> - /tmp/RtmpCYbS9C/tabel_1.xlsx
#> - /tmp/RtmpCYbS9C/tabel_2.xlsx