Bash Mutt 邮件附件错误:无法统计:没有这个文件或目录。

4
所以我看了一些其他帖子并尝试了答案,但仍然遇到了这个问题,所以我想在这里发布一个问题,并看看是否还有其他人有什么其他的想法。请注意,我对bash很陌生,所以我不确定当前为我寻找什么内容提供了什么。
我正在尝试自动化一个创建文件然后将其发送给我自己的过程。除此之外都没问题,但当我尝试自动向自己发送电子邮件时,则出现问题。
我有下面这行代码:
echo "report" | mutt -- "$USEREMAIL" -s "report" -a "my_scripts/cid_reports/trb345432.csv"

当尝试运行此命令时,会出现以下错误:

无法查找my_scripts/cid_reports/trb345432.csv文件或目录不存在 my_scripts/cid_reports/trb345432.csv:无法附加文件。

你有任何修复的想法吗?我认为mutt很好用,可以处理这个问题,我将尝试使用邮件功能并查看是否能够成功。

系统看起来正在运行中。

Mutt 1.4.2.2i (2006-07-14)  
Copyright (C) 1996-2002 Michael R. Elkins and others.  
Mutt comes with ABSOLUTELY NO WARRANTY; for details type `mutt -v  

您的错误消息引用了与您在代码中指定的不同文件。 - chepner
哎呀,我忘记编辑它来匹配了,我把代码缩短了一点。文件名应该完全相同。 - Rob
4个回答

8
mutt -h

-a <file> [...] -- attach file(s) to the message

文件列表必须以“--”序列结尾,因此,
echo "hello world" | mutt -s "title" -a /home/test.txt -- ***@**.com

您需要添加 "--"。

2

简单回答

export EMAIL="sender_mail@example.com" | mutt -e "set content_type=text/html" -s "Test Mail" -c "cc_recipient@example.com" -a /tmp/attachment.txt -- "recipient_mail@example.com" < /tmp/message.html

Please use the following syntax while attaching files with 'mutt'.

# mutt -a file -- user@domain

For example;

# mutt -a /tmp/test.txt -- john@abc.com

Relevant snippets from the man page of mutt is given below.

Raw -a file [...] Attach a file to your message using MIME. When attaching single or multiple files, separating filenames and recipient addresses with "--" is mandatory,

e.g. mutt -a image.jpg -- addr1 or mutt -a img.jpg *.png -- addr1 addr2. The -a option must be placed at the end of command line options.

Ref: https://access.redhat.com/solutions/43567


1
一般而言,“no such file or directory”意味着文件找不到。由于您使用的是相对路径,可能是因为您处于不同的目录中?如果您从运行脚本的同一目录中键入“cat my_scripts/cid_reports/trb345432.csv”,那么该文件是否存在?或者,如果您使用绝对路径(通常为“/home/'uid'/my_scripts/cid_reports/trb345432.csv”,但您的路径可能不同),脚本是否能够找到该文件?(或者这应该是一个评论而不是答案,尽管它试图通过指导找到错误来解决问题?)

这就是令人烦恼的部分,如果我直接放入文件名本身,例如my_scripts/cid_reports/trb345432.csv,那么就没问题。但如果我将其作为变量引用,就会抛出错误。 - Rob
所以,你的命令实际上是 echo "report" | mutt -- "$USEREMAIL" -s "report" -a "$some_filename"?为了调试目的,在调用 MUTT 之前尝试放置 echo $some_filename ; ls $some_filename - Ljm Dullaart
最好使用 declare -p some_filename - chepner
我不确定我是否正确地完成了您所要求的工作,但当我做类似以下的操作时: FILE="~/my_scripts/cid_reports/file.csv" declare -p FILE ls $FILE 我得到的结果是: declare -- FILE="~/my_scripts/cid_reports/file.csv" ls: ~/my_scripts/cid_reports/file.csv: 没有那个文件或目录但如果我手动运行 ls ~/my_scripts/cid_reports/ 或者 cat ~/my_scripts/cid_reports/file.csv,我会看到目录内容,它甚至会输出文件。 - Rob

0
从您对Ljm Dullaart答案的评论中可以看出,您实际上是将文件路径存储在一个变量中,然后像这样使用它:
FILE="~/my_scripts/cid_reports/file.csv"
echo "report" | mutt -- "$USEREMAIL" -s "report" -a "$FILE"

如果您正在这样做,问题在于~被双引号包含,因此不会扩展为实际的主目录路径。因此,它正在当前工作目录下查找一个名为“~”的子目录,并未找到。为了使~扩展为主目录路径,请将其及其后面的/保持未引用状态,如下所示:
file=~/"my_scripts/cid_reports/file.csv"
echo "report" | mutt -- "$useremail" -s "report" -a "$file"

请注意,我还将变量名称更改为小写。这是一种最佳实践,以避免与具有特殊含义的各种环境变量发生冲突。特殊变量都是大写的,因此如果您使用小写或混合大小写,就永远不会意外地绊倒它们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接