shell 使用bash脚本将文件作为附件发送[duplicate]

busg9geu  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(170)

此问题在此处已有答案

How do I send a file as an email attachment using Linux command line?(25个答案)
8天前关闭。
我有一个目录中的excel文件列表。我试图将这些文件作为附件通过电子邮件发送。但是,当我打开电子邮件时,我只能看到一个文件作为附件。下面的脚本没有将多个文件作为附件附加。

ls *.xls;
#echo "Test Email" | mailx -s "Testing File Conv" -a *.xls test@testmail.com

以上脚本是否有效?

irtuqstp

irtuqstp1#

可以构建一个数组来保存每个文件的所有附件选项,如下所示:

#!/usr/bin/env bash

# Produces no result if no match for pattern
shopt -s nullglob

# Captures recipients from script arguments
recipients=("$@")

# Populates aray with xls file paths
xls_files=(./*.xls)

# If there are xls files
if [ "${#xls_files[@]}" -gt 0 ]; then

  # Declare an array to store mailx attachment options
  attach_option=()

  # Creates an attachment option for each file to be attached
  for file in "${xls_files[@]}"; do
    attach_option+=(-a "$file")
  done

  # Sends mail with all the attachment options
  mailx -s "Testing File Conv" "${attach_option[@]}" "${recipients[@]}"
fi
ocebsuys

ocebsuys2#

您必须为此编写一个脚本。首先创建test.txt

ls *.xls -1 > test.txt

然后运行以下脚本:

while read line
do
mailx -s "Subject" -a ${line} -s "files" ${mail_from} << EOM

Hi, Sent files

EOM

done<test.txt

相关问题