How to send mail with attachment using shell - 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

Related

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.

Sending email with Ruby through mutt

I'm trying to send an email in this way:
path_to_file="/home/username/tmp/filename.html"
subject="My Subject"
to.each do |address|
cmd = "`echo \"#{body}\" | mutt -s \"#{subject}\" #{address} -a #{path_to_file}`"
system(cmd)
end
end
This is running on a unix machine, the file has an html extenxion and contains html code.
I receive the file correctly, with the same filename and extension, the problem is that is completely empty, this is strange because when i run the same command (mutt) from the terminal, applying the same path to file:
echo "body here" | mutt -s "some subject" mamail#mail.com -a ${path_to_file}
Then it works just fine.
Any idea?

Sending the mail using mutt command

I have a requirement where I need to send two files A and B . The file A's content should be displayed as in-line or body of the mail and file B as an attachment.
Is multiple attachment using mutt is possible?
The command
echo "Hello everyone " | mutt -s 'My mail ' abc#gmail.com -a myFile.txt
is writing Hello everyone as body of the mail and myFile.txt as an attachment(inline).
Both of my files A and B are dynamically generated, so I cannot have an echo statement.
It's actually very simple:
mutt -s 'My mail ' abc#gmail.com -a report1.txt < report2.txt
If you had two scripts to create the reports, you could use pipes (i.e. no files would be created on disk):
script2 | mutt -s 'My mail ' abc#gmail.com -a <(script1)
cat A | mutt -s 'My mail' abc#gmail.com -a B
If the shell script prints file A's content to standard output, like this:
script >A
then you can use tee to print both to the file A and into the pipe to mutt:
script | tee A | mutt -s 'My mail' abc#gmail.com -a B

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