使用 mailx 发送邮件附件的 Bash 脚本
我有一个在 postgres 中运行查询的 bash 脚本,它输出到 csv。我想添加到该脚本以使用 mailx 将该 .csv 文件通过电子邮件发送到特定电子邮件。
我遇到的问题是它不会通过电子邮件发送文件。我可以收到电子邮件,所以我知道 mailx 设置正确。我只是无法将其作为附件通过电子邮件发送。它还可以通过电子邮件将输出发送到电子邮件正文中。
这是代码。
#!/bin/bash
NOWDATE=`date +%m-%d-%Y`
PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o /tmp/folder/file-$NOWDATE.csv <<EOF
Query is here
# remove the first 2 lines of the report as they are headers
sed -i '2d' /tmp/folder/file-$NOWDATE.csv
uuencode /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]
我已经尝试过 mailx 部分:
uuencode /tmp/folder/file-$NOWDATE.csv /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]
所以
uuencode /tmp/folder/file-$NOWDATE.csv file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]
我遇到的问题是当我运行 .sh 文件时它会吐出此错误。
uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
I have a bash script that runs a query in postgres, it outputs to csv. I want to add to that script to use mailx to email that .csv file to a particular email.
The problem I am having is it will not email the file. I can get the email so I know mailx is setup correctly. I just cannot get it to email it as an attachment. It can also email the output in the body of the email.
So here is the code.
#!/bin/bash
NOWDATE=`date +%m-%d-%Y`
PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o /tmp/folder/file-$NOWDATE.csv <<EOF
Query is here
# remove the first 2 lines of the report as they are headers
sed -i '2d' /tmp/folder/file-$NOWDATE.csv
uuencode /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]
I have tried the mailx part with:
uuencode /tmp/folder/file-$NOWDATE.csv /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]
and
uuencode /tmp/folder/file-$NOWDATE.csv file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]
So the problem I get is it spits out this error when I run the .sh file.
uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果问题出在 uuencode 上...为什么不能尝试 mailx -a 选项,该选项也可以将文件附加到邮件中。
检查此链接了解更多信息。
If the problem is with uuencode...why cant you try mailx -a option which can also attach the files to the mail.
Check this link for more info.
这取决于您,但请考虑使用 ISO-8601 格式 YYYY-MM-DD (
%Y-%m-%d
)。除了其他优点之外,它的分类也很好。这不会删除前两行,只是删除第二行。将
'2d'
更改为'1,2d'
(但见下文)。请注意,这会就地修改文件。
如果
uuencode
只给出一个文件名,它会从标准输入中读取并将该名称放入其输出中。您的以下文本“我已尝试使用 mailx 部分:”...,表明您可能已经意识到这一点 - 但除了片段之外,您还没有向我们展示解决该问题的代码。您收到的错误消息:
不是文件不存在时您通常会收到的消息。我不知道什么会导致这样的“未知系统错误”。
但这里有一个替代方案,(a) 更干净一点,恕我直言,(b) 不需要
uuencode
来尝试读取文件:It's up to you, but consider using ISO-8601 format, YYYY-MM-DD (
%Y-%m-%d
). Among other advantages, it sorts nicely.This doesn't remove the first two lines, it just removes the second line. Change
'2d'
to'1,2d'
(but see below).Note that this modifies the file in place.
If
uuencode
is given only one file name, it reads from standard input and puts the name into its output. Your following text, "I have tried the mailx part with:" ..., indicates that you're probably aware of this -- but you haven't shown us the code that fixes that issue other than in snippets.The error message you're getting:
isn't what you'd normally get if the file doesn't exist. I don't know what would cause an "Unknown system error" like that.
But here's an alternative that (a) is a bit cleaner IMHO, and (b) doesn't require
uuencode
to attempt to read the file:我有同样的问题。执行查询、保存 csv 文件并邮寄的 bash 脚本。就我而言,它给出了
uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
当我使用 ksh shell 执行脚本时,它运行得很好很好,没有任何问题。像这样 -
ksh script.sh
这只是另一个指针。如果 uuencode 出错,请尝试使用 ksh 执行它;它可能对你有用。I had the same issue. A bash script executing the query, saving the csv file and mailing it. In my case it it gave the
uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
When I executed the script using the ksh shell, it worked perfectly fine without any issues. Like this -
ksh script.sh
This is just another pointer. In case uuencode gives error, then try executing it using ksh; it might work for you.