mail: send email with attachment from commandline

I know how to send an email from command line (script)

echo "body" | mail -s "subject" <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80edf9c0e5ede1e9ecaee3efed">[email protected]</a>

Is it possible to send attachments from commandline (script) as well?

I am using heirloom-mailx on Debian Wheezy.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The simple way: to use uuencode (part of sharutils package). Any formatting or body text are unavailable. Just a email with attachement and custom subject.

uuencode /path/to/file file_name.ext | mail -s subject <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284551684d45494144064b4745">[email protected]</a>

The complex way: to use sendmail and html formatting:

v_mailpart="$(uuidgen)/$(hostname)"
echo "To: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f42566f4a424e4643014c4042">[email protected]</a>
Subject: subject
Content-Type: multipart/mixed; boundary="$v_mailpart"
MIME-Version: 1.0

This is a multi-part message in MIME format.
--$v_mailpart
Content-Type: text/html
Content-Disposition: inline

<html><body>Message text itself.</body></html>

--$v_mailpart
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name=file_name.ext
Content-Disposition: attachment; filename=file_name.ext

`base64 /path/to/file`
 --$v_mailpart--" | /usr/sbin/sendmail -t

in case with several attachments last part may be repeated.

Method 2

With mutt instead of mail you would simply call

echo "body" | mutt -s "subject" -a attachment0 attachment1 [...] -- <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f99480b99c94989095d79a9694">[email protected]</a>

Here, attachmentN are the list of files that you want to attach.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x