gpt4 book ai didi

linux - 发送多个 csv 附件,简单的 Bash 错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:54 26 4
gpt4 key购买 nike

我是 Bash 脚本的新手,正在尝试修改我从网上获取的一些代码。基本上,我试图将多个文件附加到同一封电子邮件。

    #!/bin/bash

function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
declare -a attachments
attachments=("A_PTDIFF.CVS" "A_PTMISS.CVS" "A_PTNPOS.CVS" "A_PTRCON.CVS"
)

# Build headers
{

printf '%s\n'"From: jrpng
To: tom@gmail.com
Subject: Test new Bash script
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"=== Boundary ===\"
--${=== Boundary ===}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
The reports are attached to this email as requested
"
for file in "${attachments[@]}"; do
[ ! -f "$file" ] && echo "Attachment $file not found, omitting file"
>&2 && continue
mimetype=$(get_mimetype "$file")
printf '%s\n' "--${=== Boundary ===}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
"
base64 "$file"
echo
done
# print last boundary with closing --
printf '%s\n' "--${=== Boundary ===}--"
} | /usr/lib/sendmail -t -oi

好的,当通过 bash sendmail54.sh 运行时,我得到以下信息

    sendmail54.sh: line 22: From: jrpng
To: tom@gmail.com
Subject: Test new Bash script
`Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="=== Boundary ==="
--${=== Boundary ===}
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
The reports are attached to this email as requested
: bad substitution
No recipient addresses found in header

不知道错误消息告诉我什么?

如果有更好的方法来实现我想要实现的目标,我非常乐意尝试,正如 bash 脚本的新手所说,所以我在黑暗中摸索着试图让它工作。

最佳答案

你有一些基本的语法错误和误解。

显然原始代码有一个变量 boundary="randomstring",然后用 --${boundary} 在正确的位置插入它。但是您试图在 ${...} 中使用边界字符串本身,这简直是无稽之谈——大括号之间的是变量的名称。

此外,您的邮件消息需要一些空行才能有效。总体结构是

From: whoever
To: wherever
Subject: whatever
Mime-version: 1.0
Content-type: multipart something something; boundary="randomstring"

<- empty line between message headers and first body part

--randomstring
Content-type: of first attachment
x-headers: and etc

<- empty line before here; now the actual attachment data

--randomstring
Content-type: of second attachment
x-headers: and etc

payload of second attachment goes here, again after an empty line

--randomstring--

手动拼凑一封电子邮件需要您了解 MIME,而大多数人根本不想了解。如果您安装了像 mutt 这样的智能邮件程序,则无需了解如何正确格式化 SMTP MIME 消息的详细信息。

无论如何,这是修复当前代码的尝试,包括注释和适当的缩进。

function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
declare -a attachments
attachments=("A_PTDIFF.CVS" "A_PTMISS.CVS" "A_PTNPOS.CVS" "A_PTRCON.CVS")

{
boundary="--random$$string--"
# You were missing a space after '%s\n'
# Also, I'm breaking this into multiple arguments for legibility
printf '%s\n' \
"From: jrpng" \
"To: tom@gmail.com" \
"Subject: Test new Bash script" \
"Mime-Version: 1.0" \
"Content-Type: multipart/mixed; boundary=\"$boundary\"" \
"X-Comment: # notice the empty line after this one" \
"" \
"--${boundary}" \
"Content-Type: text/plain; charset=\"US-ASCII\"" \
"Content-Transfer-Encoding: 7bit" \
"Content-Disposition: inline" \
"X-Comment: # these headers are all superfluous really" \
"X-Comment: # (default is text/plain us-ascii 7bit inline)" \
"X-Comment: # but the next empty line is important" \
"" \
"The reports are attached to this email as requested" \
""

for file in "${attachments[@]}"; do
[ ! -f "$file" ] &&
echo "Attachment $file not found, omitting file" >&2 &&
continue
mimetype=$(get_mimetype "$file")
printf '%s\n' \
"--${boundary}" \
"Content-Type: $mimetype" \
"Content-Transfer-Encoding: base64" \
"Content-Disposition: attachment; filename=\"$file\"" \
""
base64 "$file"
echo
done
printf '%s\n' "--${boundary}--"
} | /usr/lib/sendmail -t -oi

关于linux - 发送多个 csv 附件,简单的 Bash 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com