Can sendmail include an attachment?
Solution 1
Posting the solution that worked for me in case it can help anyone else, sorry it's so late.
The most reliable way I found for doing this was to include the attachment as base64 in the eml file itself, bellow is an example of the eml contents.
Note 01 : the base64 for the file comes from running the base64 command on linux using the attachment as an argument (should work with any base64 tool)
Note 02 : the string used for the boundary is just nonsense using the date and random upper case letters
Filename : emlfile.eml
From: Sender <[email protected]>
To: [email protected]
CC: [email protected]
Disposition-Notification-To: [email protected]
Subject: Generic Subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="19032019ABCDE"
--19032019ABCDE
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Generic Body Copy
--19032019ABCDE
Content-Type: application;
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="MyPdfAttachment.pdf"
*base64 string goes here (no asterix)*
--19032019ABCDE--
Then the filename.eml file can be sent using the command and it will include the attachment
# /usr/sbin/sendmail -t < filename.eml
Solution 2
With mutt
you can simply use:
echo "This is the message body" | mutt -a "/path/to/file_to_attach" -s "subject of message" -- [email protected]
Using mail
command:
mail -a /opt/emailfile.eml -s "Email File" [email protected] < /dev/null
-a
is used for attachments.
You can use SendEmail
:
sendemail -t [email protected] -m "Here is the file." -a attachmentFile
Related videos on Youtube
![waldemar_enns](https://i.stack.imgur.com/2Oq0N.jpg?s=256&g=1)
waldemar_enns
Primarily Informix-4GL Programmer, Linux Bash Programmer and Linux Administrator. Dabbler in Java, Python, HTML, PHP and JavaScript. Expert at breaking everything.
Updated on September 18, 2022Comments
-
waldemar_enns almost 2 years
Is it possible to include an attachment with sendmail? I am generating the following
emailfile.eml
files with the following layoutFrom: Company Name <[email protected]> To: [email protected] CC: [email protected] Subject: Generated Output Mime-Version: 1.0 This will be the body copy even though it's terrible
I am sending these emails using
# /usr/sbin/sendmail -t < emailfile.eml
This part is working file but I would like to include an attachment to this email.
-
serenesat almost 9 yearsUse
uuencode
ormutt
. -
waldemar_enns almost 9 yearsI was hoping to use something more like Content-Disposition: attachment but I can't get it to work
-
AnFi almost 9 yearsDo you want some text AND attachment OR attachment only? ["attachment only" case is trivial ]
-
waldemar_enns almost 9 yearsI'd like to send body copy and the attachment, preferably using sendmail and the eml file with Content-Disposition
-
AnFi almost 9 yearsIn such case the most simple is to use email client e.g. mutt
-
Kusalananda about 8 yearsYou may have a
mailx
implementation that supports attachments with-A
... -
JdeBP over 5 yearsAnswers go in answers, not in questions.
-
waldemar_enns over 5 yearsJdeBP is right one sec, I will correct it
-
waldemar_enns over 5 yearsThere all fixed
-
-
Alexej Magura almost 7 yearsYeah, I don't have
sendemail
on my workplace's CentOS, we just havesendmail
. -
jdex about 5 yearson my ubuntu system, the
mail
option was-A
not-a
-
mttpgn over 4 yearsGreat answer and completely correct. Also, for multiple attachments you can repeat the section from the second-to-last line upward to the second-to-last delimiter (inclusive) as many times as needed within the .eml file.
-
waldemar_enns over 4 yearsThank you very much! <3
-
Simon Rose over 3 yearsThis is exactly what I needed, thanks!
-
waldemar_enns over 3 yearsYou're very welcome!
-
Nikkoli about 3 yearsThank you! This is exactly what I was looking for to send a report of daily server activities 🙏
-
k4ppa about 2 yearsAn example of the bash code would be useful. I tried and all I have is an email with a base64 encoded string inside the body.