0

我想将不同扩展名的文件附加到邮件中。我的一些 id 有 pdf 文件,有些有 excel 文件。所以我为这两种类型的文件生成了路径。但是当我附加文件时,它会给我一个错误 attach.files 必须链接到有效文件。这是正确的,该文件不存在。谁能帮助我。

在此处输入代码

data_send<-read.csv('/Users/vaibhav.chawla/Downloads/manual.csv')


smtp<-list(host.name="smtp.gmail.com",smtpPortSMTP=465,user.name="vaibhav@xxxx.com",passwd='xxxx',ssl=T)
i=1

for (i in i:nrow(data_send)) {



  from <- "xxxxx"
  to<-c(paste0(data_send$mail[i]))
  cc<-c(paste0(data_send$amail[i]))
  subject <-paste0("Summary May 01, 2020 to May 27, 2020")
  body <-paste0("Hello")
  filtetype <- c(".xlsx",".pdf")
  path<-c(paste("/Users/vaibhav.chawla/Downloads/Excel/",data_send$id[i],filetype,sep=""))
  mailSend<-send.mail(from=from,to=to,cc=cc,subject=subject,body = body,smtp=smtp,authenticate = T,send = T,html=T,inline = T,attach.files = c(path))
  print(paste0(i,". Mail sent to ",data_send$id[i]))

}
4

1 回答 1

0

使用基本函数file.exists获取一个布尔向量,指定每个文件path是否被发现。如果找到任何文件,请使用attach.file调用中的参数send.mail,再次使用valid_paths向量仅选择找到的路径。如果没有找到文件,则忽略该attach.file参数。

valid_paths <- file.exists(path)
if (any(valid_paths))) {
    mailSend<-send.mail(from=from,to=to,cc=cc,subject=subject,body = body,smtp=smtp,authenticate = T,send = T,html=T,inline = T,attach.files =path[valid_path])
} else {
    mailSend<-send.mail(from=from,to=to,cc=cc,subject=subject,body = body,smtp=smtp,authenticate = T,send = T,html=T,inline = T)
}
于 2020-06-05T06:20:32.727 回答