You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R语言blastula包批量发送Excel文件报错求助

解决blastula包create_smtp_creds_file函数报错问题

问题描述

我已将制作好的Excel文件放在桌面文件夹中,同时整理了文件名与对应收件人邮箱的表格,希望用R语言的blastula包批量发送对应Excel文件给指定收件人,但运行代码时在create_smtp_creds_file环节报错:

Error in writeLines(serialized, file) : 'con' is not a connection

测试代码如下:

rm(list=ls())

df <- data.frame(
  file_name = c("AARCHER", "AJOHNSON", "JGOODALL"),
  email = c("email", "email", "email")
)

library(blastula)

# Set the path to the folder containing the Excel files
folder_path <- 'folder/path'

# Create SMTP credentials
create_smtp_creds_file(
  file,
  user = "email",
  provider = NULL,
  host = "blank for post",
  port = 25,
  use_ssl = TRUE
)

# Iterate over each row in the data frame and send emails
for (i in 1:nrow(df)) {
  file_name <- paste0(df$file_name[i], ".xlsx")
  email <- df$email[i]
  
  file_path <- file.path(folder_path, file_name)
  
  if (file.exists(file_path)) {
    # Create an email
    email_obj <- compose_email(
      body = "Please find attached the Excel file.",
      title = "Excel file attachment",
      template = blastula_template
    )
    
    # Add the Excel file attachment
    email_obj <- add_attachment(email_obj, file_path)
    
    # Set the recipient and subject, and send the email
    smtp_send(
      email_obj,
      to = email,
      subject = "Excel file attachment",
      credentials = creds_file("smtp_creds.txt")
    )
    
    cat("Email sent successfully to", email, "\n")
  } else {
    cat("File", file_name, "not found.\n")
  }
}

数据与文件示例

  • 收件人表格数据:
    file_nameemail
    AARCHERaarcher@bla.com
    JGOODALLjgoodall@bla.com
  • 对应Excel文件名:AARCHER.xlsx、JGOODALL.xlsx

错误原因

create_smtp_creds_file函数的第一个参数file需要传入具体的凭证文件名字符串(比如"smtp_creds.txt"),而原代码中直接写了未定义的变量file,导致函数无法识别文件连接,触发报错。此外,原代码中SMTP服务器参数(host、port等)未填写正确值,也会导致后续邮件发送失败。

修正后的完整代码

rm(list=ls())

# 实际收件人数据
df <- data.frame(
  file_name = c("AARCHER", "AJOHNSON", "JGOODALL"),
  email = c("aarcher@bla.com", "ajohnson@bla.com", "jgoodall@bla.com"),
  stringsAsFactors = FALSE
)

library(blastula)

# 设置桌面Excel文件路径(替换成你的实际路径)
# Windows示例:folder_path <- "C:/Users/你的用户名/Desktop/Excel文件"
# Mac示例:folder_path <- "/Users/你的用户名/Desktop/Excel文件"
folder_path <- "替换为你的桌面文件夹路径"

# 生成SMTP凭证文件(只需运行一次,生成后可注释掉)
create_smtp_creds_file(
  file = "smtp_creds.txt",  # 指定凭证文件名
  user = "你的发件邮箱@xxx.com",  # 替换为发件邮箱
  provider = NULL,
  host = "你的SMTP服务器地址",  # 比如企业邮箱host、QQ邮箱是smtp.qq.com
  port = 465,  # 对应服务器端口,QQ邮箱用465,部分企业邮箱用25
  use_ssl = TRUE  # 根据服务器要求设置
)

# 批量发送邮件
for (i in 1:nrow(df)) {
  file_name <- paste0(df$file_name[i], ".xlsx")
  email <- df$email[i]
  
  file_path <- file.path(folder_path, file_name)
  
  if (file.exists(file_path)) {
    # 构建邮件内容
    email_obj <- compose_email(
      body = "您好,附件是您需要的Excel文件,请查收。",
      title = "Excel文件附件",
      template = blastula_template
    )
    
    # 添加附件
    email_obj <- add_attachment(email_obj, file_path)
    
    # 发送邮件
    smtp_send(
      email_obj,
      to = email,
      from = "你的发件邮箱@xxx.com",  # 补充发件人信息
      subject = "Excel文件附件",
      credentials = creds_file("smtp_creds.txt")
    )
    
    cat("邮件已成功发送至:", email, "\n")
  } else {
    cat("未找到文件:", file_name, "\n")
  }
}

关键注意事项

  • 替换folder_path为你的桌面Excel文件夹实际路径,注意路径分隔符在Windows用/或\\,Mac用/
  • 填写正确的SMTP服务器信息:不同邮箱服务商的SMTP地址、端口不同,比如QQ邮箱host="smtp.qq.com"、port=465,企业邮箱需咨询IT部门获取参数
  • 第一次运行create_smtp_creds_file时,R会弹出输入框要求输入发件邮箱密码(或授权码,比如QQ邮箱需用授权码代替密码)
  • 确保Excel文件名与df$file_name完全匹配(大小写敏感),否则会提示文件未找到

内容的提问来源于stack exchange,提问作者Ryan Wallick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 23:46:59