4

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  
ploth
  • 435
  • 8
  • 16
Rob
  • 153
  • 1
  • 3
  • 14
  • Your error message references a different file than the one you specify in your code. – chepner Feb 10 '17 at 15:55
  • Bah, i forgot to edit that to match, i shortened the code a little. The file names should be exactly the same. – Rob Feb 10 '17 at 16:31

4 Answers4

8
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 "--".

haitaoli
  • 81
  • 1
  • 2
2

SIMPLE ANSWER

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

M.S. Arun
  • 1,053
  • 13
  • 22
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?)

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • 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. – Rob Feb 10 '17 at 18:34
  • 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. – Ljm Dullaart Feb 10 '17 at 18:45
  • Better yet, use `declare -p some_filename`. – chepner Feb 10 '17 at 18:57
  • 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. – Rob Feb 10 '17 at 19:50
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.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151