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

Linux下sendmail命令无法添加附件问题求助

解决sendmail无法添加附件的建议
  • 修正sendmail参数用法
    sendmail的-a参数并非用于添加附件,它的作用是传递额外的邮件头相关选项,直接传入文件路径无效。同时-t参数会自动从邮件内容的To/Cc头读取收件人,命令行中不需要再指定${TO_LIST},避免冲突。

  • 构建正确的MIME多部分邮件结构
    要添加附件,必须将邮件构建为MIME多部分格式,明确区分正文和附件内容。以下是修改后的可用脚本:

send_mail(){
    # 生成唯一的MIME分界符,避免内容冲突
    BOUNDARY="$(date +%Y%m%d%H%M%S)-$(uuidgen)"
    FILE_PATH="${file_path}/test.CSV"

    cat << EOF | /usr/sbin/sendmail -t
From: test@gmail.com
To: test@gmail.com
#Cc: $CC_LIST
Subject: test mail
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$BOUNDARY"

--$BOUNDARY
Content-Type: text/plain; charset="utf-8"

Dear User,
please find the attachment.
$result

$sign

--$BOUNDARY
Content-Type: text/csv; name="test.CSV"
Content-Disposition: attachment; filename="test.CSV"
Content-Transfer-Encoding: base64

$(base64 "$FILE_PATH")

--$BOUNDARY--
EOF
}
  • 额外检查点
    1. 确认系统已安装base64工具(多数Linux发行版默认自带)
    2. 若$newline变量未定义,可直接删除该变量引用,用换行符分隔内容即可
    3. 测试时可给sendmail添加-v参数(/usr/sbin/sendmail -t -v),查看详细发送日志排查潜在问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 15:05:25