bash script for attaching file with mutt - bash

i have a bash script with the below contents
#!/bin/bash
mv /test.txt/ /test/date "+%d-%m-%y"`.txt
Need to attach this file using mutt and send an email.
Kindly help

-a to attach a file
mutt -s "mail subject" example#address.com < /path/to/message/body.txt -a /path/to/text.txt

Related

Send mail with mutt in shell script file

I am writing the following command in .sh file and it opens the interactive window to ask me to input an email address to send the email. The email is then sent with the attached file without issue.
mutt -s 'risk items' -a file.xlsx
But if I try the following command it fails to send it bypassing the interactive window.
mutt -s 'risk items' -a file.xlsx my_name#company_name.com
Also the second command is working outside shell. Any help to solve this would be appreciated.
The error message is:
my_name#company_name.com: unable to attach file.
Your syntax is incorrect. Look at the output of mutt -h, which should include something like this:
mutt [<options>] [-Ex] [-Hi <file>] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...]
Look at the -a argument, which shows -a <file> [...] --.
The -a argument expects multiple files. You use -- to terminate the list so that mutt knows you've stopped listing files and are now listing addresses:
mutt -s 'risk items' -a file.xlsx -- my_name#company_name.com

How to send mail with attachment using shell

I am trying to sending mail with attachment using shell script, but I am not getting mail.
Shell Script
#!/bin/bash
$ echo "Mail Sent" | mail -s "a subject" -a "Report_by_Customer_20190614_131246.xls" test#gmail.com
Use mutt. Add list of files that you want to attach
echo "body" | mutt -s "subject" -a attachment0 attachment1 [...] -- test#gmail.com
Also here you can find another options

unix, email attachment not being attached

I am trying to send an attachment via a script and the logic works if i run manually but not as part of the script. if I add echo, the email goes out and populates with uuencode jef20.txt jef20.txt but no attachment. any ideas?
#!/bin/bash
echo Your Username?
read user
echo FIX Session?
read session
echo what client?
read client
awk '!/35=0|35=A|35=5|35=2|35=1|closed/' /company/gate/app_phoenix/logs/fix/$session >> /home/dnash/$client.txt
uuencode $client.txt $client.txt | mailx -s "Cert Logs" "$user#company.com"
Depending on your version of mailx , you can use the -a ( "attach" ) command line switch:
mailx -a "$client.txt" -s "Cert Logs" "$user#company.com"
See Also
mailx manpage
how to send an email via mailx with enclosed file

mailx: "send-mail: illegal option -- a" while attempting to send file attachment

Our production server has been retired and now we are using a hosted system running Redhat GNU/Linux.
We had many scripts using mutt to send file attachments but they're failing now since mutt is not installed on our servers (The sysadmin policy is that mutt is not secure so it will not be installed)
I have tried using mailx but to no avail. When I do
echo "this is my email body"| mailx -s "this is my email subject" "email#xyz.com" -a "filename.csv"
I get
$ send-mail: illegal option -- a
"filename.csv" exist and it is local to the directory I run the command from. Of course, when I do
mailx -s "this is my email subject" "email#xyz.com" < "filename.csv"
It works but it embeds the file attachment in the email body. Users do not want that.
What am I doing wrong?
I figured it out. I simply moved the -a flag before the email address like so
echo "this is my email body"| mailx -s "this is my email subject" -a "filename.csv" "email#xyz.com"
It worked just fine.
uuencode filename.csv filename.csv | mailx -s "this is my email subject" "email#xyz.com"
Or, if you want to clump text and an attachment together, then
echo "this is my email body" | cat -<(echo uuencode filename.csv filename.csv) | mailx -s "this is my email subject" "email#xyz.com"
I came across the same problem, use -A instead of -a. It worked for me.

Is there a way to automatically e-mail after a long script is finished?

I am trying to run a long bash script overnight to get some data. I wanted to include a script that would automatically e-mail me the files after the scripts are completed. Is there a way to do this using mutt? I want something like below:
sh atoms.sh
sh angles.sh
mutt -a atoms.dat angles.dat -- [e-mail adress]
Any takers?
EDITS: If there's any other way to achieve this -- "sending multiple attachment to an e-mail address after scripting is finished" -- I'd be very appreciated.
sh atoms.sh
sh angles.sh
mutt -s "data set from atoms.sh" [email address] < ./atom.dat
mutt -s "data set from angles.sh" [email address] < ./angles.dat
will disable the terminal interaction and send e-mails after the jobs are finished
-a file [...]
Attach a file to your message using MIME. To attach multiple files, separating
filenames and recipient addresses with "--" is mandatory, e.g. mutt -a img.jpg
*.png -- addr1 addr2.
$ $( sh atoms.sh; sh angles.sh ) && mutt -s "man mutt" \
-a grab.sh raptor.mpd.ogg.m3u scripts/bussorakel \
-- emailAddress#example.com < /dev/null
alternatively, you have:
$(sh atoms.sh; sh angles.sh ) & FOR=$!
wait $FOR
mutt -s "last command done, sending email" (...)

Resources