Bash Mutt email with attachment error Can't stat : No such file or directory
Asked Answered
H

4

5

So i,ve read some other posts and tried out the answers, but i still keep running into this problem so i wanted to post a question here and see if anyone else had any other ideas. Keeping in mind i am pretty new to bash so i am iffy on whats available currently for what i,m looking for.

I am trying to automate a process that creates a file then sends it to me. All the above is fine, until i try to automatically email myself the file.

I have this line of code for it

echo "report" | mutt -- "$USEREMAIL" -s "report" -a "my_scripts/cid_reports/trb345432.csv"

When it tries to run this command it throws an error like

Can't stat my_scripts/cid_reports/trb345432.csv: No such file or directory my_scripts/cid_reports/trb345432.csv: unable to attach file.

Any ideas on how i can fix this? I thought mutt was good to handle this, I am going to play with the mail feature and see if i can get any success with that.

The system looks to be running

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  
Hiers answered 10/2, 2017 at 15:50 Comment(2)
Your error message references a different file than the one you specify in your code.Ridgeling
Bah, i forgot to edit that to match, i shortened the code a little. The file names should be exactly the same.Hiers
O
9
mutt -h

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

The list of files must be terminated with the "--" sequence, so,

echo "hello world" | mutt -s "title" -a /home/test.txt -- ***@**.com

You need to add "--".

Outmoded answered 10/1, 2019 at 16:9 Comment(0)
N
3

SIMPLE ANSWER

export EMAIL="[email protected]" | mutt -e "set content_type=text/html" -s "Test Mail" -c "[email protected]" -a /tmp/attachment.txt -- "[email protected]" < /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 -- [email protected]

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

Nanosecond answered 13/3, 2018 at 17:42 Comment(0)
J
1

The no such file or directory in general means that the file cannot be found. Because you're using a relative path, it might be that you are in a different directory?

If you type cat my_scripts/cid_reports/trb345432.csv from the same directory as you run the script, is the file there?

Or otherwise. if you use an absolute path (usually '/home/'uid'/my_scripts/cid_reports/trb345432.csv` but your path may be duifferent), does the script find the file?

(or should this have been a comment in stead of an answer, eventhough it tries to guide through finding the error?)

Jacklin answered 10/2, 2017 at 18:25 Comment(4)
Thats the annoying part, IF i put in the file name itself IE my_scripts/cid_reports/trb345432.csv its fine, If i reference it as a variable it throws that error.Hiers
So, your command is actually echo "report" | mutt -- "$USEREMAIL" -s "report" -a "$some_filename"? Try put, for debugging purposes, an echo $some_filename ; ls $some_filename before your call to MUTT.Jacklin
Better yet, use declare -p some_filename.Ridgeling
So i,m not sure if i am doing it correctly for what you were asking, but when i did something like 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: No such file or directory But if i manually run the ls ~/my_scripts/cid_reports/ or even cat ~/my_scripts/cid_reports/file.csv i will see the directory contents, and it will even spit out the file.Hiers
P
0

From your comments to Ljm Dullaart's answer, you're actually storing the file path in a variable, then using it something like this:

FILE="~/my_scripts/cid_reports/file.csv"
echo "report" | mutt -- "$USEREMAIL" -s "report" -a "$FILE"

If that's what you're doing, the problem is that ~ is in double-quotes, and therefore doesn't get expanded to the actual path to your home directory. Thus, it's looking for a sub directory literally named "~" under the current working directory, and not finding it. In order for ~ to be expanded to the path to your home directory, leave it and the following / unquoted, like this:

file=~/"my_scripts/cid_reports/file.csv"
echo "report" | mutt -- "$useremail" -s "report" -a "$file"

Note that I've also switched the variable names to lowercase. This is a best practice to avoid conflicts with the various environment variables that have special meanings; the special ones are all caps, so if you use lower- or mixed-case, you'll never accidentally trip over them.

Picador answered 11/2, 2017 at 4:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.