2

This is how I try to send, email with sendmail command.

echo "Dear user,

please find the attached file.

Thanks,
Support team " | mailx -s "x12 extract for 837 transaction" -a $LogFilePath/file.csv $email_id 

but i am getting the error as "-a illegal operation" checked with man mailx command there is no flag like -a and there flag for attachment.

so I have tried with sendmail command

( 
echo $email_id
#From: $email_from_recipients
echo "MIME-Version: 1.0"
echo "Content-Type: text/html; charset=us-ascii"   
echo Subject: Report
$LogFilePath/file.csv

) | /usr/lib/sendmail -t

it was not sending the file as attachment.

the contents of the csv file is displayed in the body of the mail with comma separated one.

how to send the attachment with sendmail command in unix?

4
  • Have you try cat $LogFilePath/file.csv instead of just $LogFilePath/file.csv? Commented May 3, 2018 at 6:32
  • tried with that too getting contents of the csv file is didplayed in the body of the email with comma separated one
    – Kalishwari
    Commented May 3, 2018 at 7:11
  • 3
    To help clarify what mailx implementation you have, let us know what Unix you are using.
    – Kusalananda
    Commented May 3, 2018 at 7:41
  • If you're writing directly to sendmail you need a blank line after your headers. Commented May 5, 2018 at 20:59

6 Answers 6

1

try -a option before To For example

echo test | mailx -s "Subj" --attach=/home/someuser/test.txt "[email protected]" or echo test | mailx -s "Subj" -a /home/someuser/test.txt "[email protected]"

1
  • From the question: ... but I am getting the error as "-a illegal operation" checked with man mailx command there is no flag like -a ...
    – Jeff Schaller
    Commented Dec 29, 2020 at 16:05
0

The sendmail client doesn't know anything about mime. While you could continue down the road you are going by creating the mime structures in bash, you are in for a painful and buggy journey. Particularly if you have to roll your own base64 implementation in shell script. Some email clients will still handle uuencoded attachments....

(
  echo "here's the file" 
  uuencode $LogFilePath/file.csv
) | mailx -s "x12 extract for 837 transaction" $email_id

...support will be patchy at best.

Really you want a mail client which understands MIME properly. There are libraries for most of the major programming languages, but there are also standalone programs which are easy to integrate with scripts - although you may have to compile them yourself. You didn't tell us what flavour of Linux or if you are the admin of the system (if not, then the admin should have been your first port of call).

I've previously used a (presumably different version of) mailx for this and mutt. These are available on Sourceforge and GitHub respectively.

0

How do I create attachments with sendmail? You don't. Sendmail is a mail transfer agent (MTA). Creating e-mail messages, including adding attachments or signatures, is the function of a mail user agent (MUA). Some popular MUAs include mutt, elm, exmh, Netscape, Eudora and Pine. Some specialized packages (metamail, some Perl modules, etc.) can also be used to create messages with attachments.

0

Using a specific mail implementation: s-nail (feature rich mail command), you have the -a switch especially designed to send attachments:

s-nail -s subject -a /path/to/file [email protected]

If you check man mailx, you will see that the -a is for adding headers, it could be confusing.

If you want something simple and reliable to send attachments, then here we go.

Add -v to have verbose output, and the progression of base64 operation on that file.

$ LANG=C apt-cache show s-nail
[...]
Homepage: https://www.sdaoden.eu/code.html
Description-en: feature-rich BSD mail(1)
 S-nail is a mail processing system with a command syntax reminiscent
 of ed(1) with lines replaced by messages. It is intended to provide
 the functionality of the POSIX mailx(1) command and offers (mostly
 optional) extensions for line editing, IDNA, MIME, S/MIME, SMTP and
 POP3 (and IMAP). It is usable as a mail batch language. S-nail is a
 derivative of Heirloom mailx, formerly known as nail, which
 itself is based upon Berkeley Mail that has a history back to the 70s.
-1
--attach

For example echo test | mail -s "Subj" "[email protected]" --attach=/home/someuser/test.txt

3
  • 1
    As it is mentioned in the headline we talk about UNIX and there mail do not have --attach Commented May 3, 2018 at 6:31
  • 1
    it's not working
    – Kalishwari
    Commented May 3, 2018 at 7:17
  • 1
    it sending mail but the attachment is missing it shows /home/coe_users/dead.letter... Saved message in /home/coe_users/dead.letter
    – Kalishwari
    Commented May 3, 2018 at 7:19
-1

Try with the -A option; turns out that -a is the flag for appending headers whereas -A is the flag for sending attachments.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .