<p>I've created a script that runs every night on my Linux server that uses <code>mysqldump</code> to back up each of my MySQL databases to .sql files and packages them together as a compressed .tar file. The next step I want to accomplish is to send that tar file through email to a remote email server for safekeeping. I've been able to send the raw script in the body an email by piping the backup text file to <code>mailx</code> like so:</p>
<pre><code>$ cat mysqldbbackup.sql | mailx backup@email.example
</code></pre>
<p><code>cat</code> echoes the backup file's text which is piped into the <code>mailx</code> program with the recipient's email address passed as an argument.</p>
<p>While this accomplishes what I need, I think it could be one step better, <strong>Is there any way, using shell scripts or otherwise, to send the compressed .tar file to an outgoing email message as an <em>attachment</em>?</strong>  This would beat having to deal with very long email messages which contain header data and often have word-wrapping issues etc.</p>

[{'answer_id': 17365, 'author': 'Chris N', 'author_id': 1653, 'author_profile': 'https://Stackoverflow.com/users/1653', 'pm_score': 5, 'selected': False, 'text': '<p>From looking at <code>man mailx</code>, the mailx program does not have an option for attaching a file. You could use another program such as mutt.</p>\n<pre><code>echo &quot;This is the message body&quot; | mutt -a file.to.attach -s &quot;subject of message&quot; recipient@example.com\n</code></pre>\n<p>Command line options for mutt can be shown with <code>mutt -h</code>.</p>\n'}, {'answer_id': 17366, 'author': 'David Schlosnagle', 'author_id': 1750, 'author_profile': 'https://Stackoverflow.com/users/1750', 'pm_score': 4, 'selected': False, 'text': '<p>You can use <a href="http://www.mutt.org/doc/manual/" rel="nofollow noreferrer">mutt</a> to send the email with attachment</p>\n<pre><code>mutt -s &quot;Backup&quot; -a mysqldbbackup.sql backup@example.com &lt; message.txt\n</code></pre>\n'}, {'answer_id': 17381, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 6, 'selected': False, 'text': '<p>Depending on your version of Linux it may be called mail. To quote @David above:</p>\n<pre><code>mail -s &quot;Backup&quot; -a mysqldbbackup.sql backup@email.example &lt; message.txt\n</code></pre>\n<p>or also:</p>\n<pre><code>cat message.txt | mail -s &quot;Backup&quot; -a mysqldbbackup.sql backup@email.example\n</code></pre>\n'}, {'answer_id': 17422, 'author': 'Daniel Fone', 'author_id': 1848, 'author_profile': 'https://Stackoverflow.com/users/1848', 'pm_score': 6, 'selected': False, 'text': '<p>Or, failing mutt:</p>\n\n<pre><code>gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB" backup@email.com\n</code></pre>\n'}, {'answer_id': 84355, 'author': 'Gunstick', 'author_id': 15653, 'author_profile': 'https://Stackoverflow.com/users/15653', 'pm_score': 2, 'selected': False, 'text': '<p>metamail has the tool metasend</p>\n\n<pre><code>metasend -f mysqlbackup.sql.gz -t backup@email.com -s Backup -m application/x-gzip -b\n</code></pre>\n'}, {'answer_id': 1470149, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<p>I use mpack.</p>\n\n<pre><code>mpack -s subject file user@example.com\n</code></pre>\n\n<p>Unfortunately mpack does not recognize '-' as an alias for stdin. But the following work, and can easily be wrapped in an (shell) alias or a script:</p>\n\n<pre><code>mpack -s subject /dev/stdin loser@example.com &lt; file\n</code></pre>\n"}, {'answer_id': 4887607, 'author': 'glenn jackman', 'author_id': 7552, 'author_profile': 'https://Stackoverflow.com/users/7552', 'pm_score': 4, 'selected': False, 'text': '<p>I once wrote this function for ksh on Solaris (uses Perl for base64 encoding): </p>\n\n<pre><code># usage: email_attachment to cc subject body attachment_filename\nemail_attachment() {\n    to="$1"\n    cc="$2"\n    subject="$3"\n    body="$4"\n    filename="${5:-\'\'}"\n    boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"\n    {\n        print -- "To: $to"\n        print -- "Cc: $cc"\n        print -- "Subject: $subject"\n        print -- "Content-Type: multipart/mixed; boundary=\\"$boundary\\""\n        print -- "Mime-Version: 1.0"\n        print -- ""\n        print -- "This is a multi-part message in MIME format."\n        print -- ""\n        print -- "--$boundary"\n        print -- "Content-Type: text/plain; charset=ISO-8859-1"\n        print -- ""\n        print -- "$body"\n        print -- ""\n        if [[ -n "$filename" &amp;&amp; -f "$filename" &amp;&amp; -r "$filename" ]]; then\n            print -- "--$boundary"\n            print -- "Content-Transfer-Encoding: base64"\n            print -- "Content-Type: application/octet-stream; name=$filename"\n            print -- "Content-Disposition: attachment; filename=$filename"\n            print -- ""\n            print -- "$(perl -MMIME::Base64 -e \'open F, shift; @lines=&lt;F&gt;; close F; print MIME::Base64::encode(join(q{}, @lines))\' $filename)"\n            print -- ""\n        fi\n        print -- "--${boundary}--"\n    } | /usr/lib/sendmail -oi -t\n}\n</code></pre>\n'}, {'answer_id': 9524359, 'author': 'rynop', 'author_id': 563420, 'author_profile': 'https://Stackoverflow.com/users/563420', 'pm_score': 9, 'selected': True, 'text': '<p>None of the mutt ones worked for me. It was thinking the email address was part of the attachment. Had to do:</p>\n<pre><code>echo &quot;This is the message body&quot; | mutt -a &quot;/path/to/file.to.attach&quot; -s &quot;subject of message&quot; -- recipient@domain.example\n</code></pre>\n'}, {'answer_id': 12570802, 'author': 'Mike Graf', 'author_id': 1114274, 'author_profile': 'https://Stackoverflow.com/users/1114274', 'pm_score': 0, 'selected': False, 'text': '<p>Just to add my 2 cents, I\'d write my own PHP Script:</p>\n\n<p><a href="http://php.net/manual/en/function.mail.php" rel="nofollow">http://php.net/manual/en/function.mail.php</a></p>\n\n<p>There are lots of ways to do the attachment in the examples on that page.</p>\n'}, {'answer_id': 14213935, 'author': 'user1651561', 'author_id': 1651561, 'author_profile': 'https://Stackoverflow.com/users/1651561', 'pm_score': 4, 'selected': False, 'text': '<h2>Send a Plaintext body email with one plaintext attachment with mailx:</h2>\n<pre><code>(\n  /usr/bin/uuencode attachfile.txt myattachedfilename.txt; \n  /usr/bin/echo &quot;Body of text&quot;\n) | mailx -s \'Subject\' youremail@example.com\n</code></pre>\n<p>Below is the same command as above, without the newlines</p>\n<pre><code>( /usr/bin/uuencode /home/el/attachfile.txt myattachedfilename.txt; /usr/bin/echo &quot;Body of text&quot; ) | mailx -s \'Subject\' youremail@example.com\n</code></pre>\n<p>Make sure you have a file <code>/home/el/attachfile.txt</code> defined with this contents:</p>\n<pre><code>&lt;html&gt;&lt;body&gt;\nGovernment discriminates against programmers with cruel/unusual 35 year prison\nsentences for making the world\'s information free, while bankers that pilfer \ntrillions in citizens assets through systematic inflation get the nod and \nwalk free among us.\n&lt;/body&gt;&lt;/html&gt;\n</code></pre>\n<p>If you don\'t have uuencode read this: <a href="https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work">https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work</a></p>\n<h2>On Linux, Send HTML body email with a PDF attachment with sendmail:</h2>\n<p>Make sure you have ksh installed: <code>yum info ksh</code></p>\n<p>Make sure you have sendmail installed and configured.</p>\n<p>Make sure you have uuencode installed and available: <a href="https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work">https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work</a></p>\n<p>Make a new file called <code>test.sh</code> and put it in your home directory: <code>/home/el</code></p>\n<p>Put the following code in <code>test.sh</code>:</p>\n<pre><code>#!/usr/bin/ksh\nexport MAILFROM=&quot;el@defiant.com&quot;\nexport MAILTO=&quot;youremail@example.com&quot;\nexport SUBJECT=&quot;Test PDF for Email&quot;\nexport BODY=&quot;/home/el/email_body.htm&quot;\nexport ATTACH=&quot;/home/el/pdf-test.pdf&quot;\nexport MAILPART=`uuidgen` ## Generates Unique ID\nexport MAILPART_BODY=`uuidgen` ## Generates Unique ID\n\n(\n echo &quot;From: $MAILFROM&quot;\n echo &quot;To: $MAILTO&quot;\n echo &quot;Subject: $SUBJECT&quot;\n echo &quot;MIME-Version: 1.0&quot;\n echo &quot;Content-Type: multipart/mixed; boundary=\\&quot;$MAILPART\\&quot;&quot;\n echo &quot;&quot;\n echo &quot;--$MAILPART&quot;\n echo &quot;Content-Type: multipart/alternative; boundary=\\&quot;$MAILPART_BODY\\&quot;&quot;\n echo &quot;&quot;\n echo &quot;--$MAILPART_BODY&quot;\n echo &quot;Content-Type: text/plain; charset=ISO-8859-1&quot;\n echo &quot;You need to enable HTML option for email&quot;\n echo &quot;--$MAILPART_BODY&quot;\n echo &quot;Content-Type: text/html; charset=ISO-8859-1&quot;\n echo &quot;Content-Disposition: inline&quot;\n cat $BODY\n echo &quot;--$MAILPART_BODY--&quot;\n\n echo &quot;--$MAILPART&quot;\n echo \'Content-Type: application/pdf; name=&quot;\'$(basename $ATTACH)\'&quot;\'\n echo &quot;Content-Transfer-Encoding: uuencode&quot;\n echo \'Content-Disposition: attachment; filename=&quot;\'$(basename $ATTACH)\'&quot;\'\n echo &quot;&quot;\n uuencode $ATTACH $(basename $ATTACH)\n echo &quot;--$MAILPART--&quot;\n) | /usr/sbin/sendmail $MAILTO\n</code></pre>\n<p>Change the export variables on the top of <code>test.sh</code> to reflect your address and filenames.</p>\n<p>Download a test pdf document and put it in <code>/home/el</code> called pdf-test.pdf</p>\n<p>Make a file called /home/el/email_body.htm and put this line in it:</p>\n<pre><code>&lt;html&gt;&lt;body&gt;&lt;b&gt;this is some bold text&lt;/b&gt;&lt;/body&gt;&lt;/html&gt;\n</code></pre>\n<p>Make sure the pdf file has sufficient 755 permissions.</p>\n<p>Run the script <code>./test.sh</code></p>\n<p>Check your email inbox, the text should be in HTML format and the pdf file automatically interpreted as a binary file.  Take care not to use this function more than say 15 times in a day, even if you send the emails to yourself, spam filters in gmail can blacklist a domain spewing emails without giving you an option to let them through.  And you\'ll find this no longer works, or it only lets through the attachment, or the email doesn\'t come through at all.  If you have to do a lot of testing on this, spread them out over days or you\'ll be labelled a spammer and this function won\'t work any more.</p>\n'}, {'answer_id': 19705851, 'author': 'Fredrik Wendt', 'author_id': 153117, 'author_profile': 'https://Stackoverflow.com/users/153117', 'pm_score': 5, 'selected': False, 'text': '<p>I use SendEmail, which was created for this scenario. It\'s packaged for Ubuntu so I assume it\'s available </p>\n\n<blockquote>\n  <p><code>sendemail -f sender@some.where -t receiver@some.place -m "Here are your files!" -a file1.jpg file2.zip</code></p>\n</blockquote>\n\n<p><a href="http://caspian.dotconf.net/menu/Software/SendEmail/" rel="noreferrer">http://caspian.dotconf.net/menu/Software/SendEmail/</a></p>\n'}, {'answer_id': 20817976, 'author': 'Allan Pinto', 'author_id': 2428576, 'author_profile': 'https://Stackoverflow.com/users/2428576', 'pm_score': 0, 'selected': False, 'text': '<p><code>mailx</code> does have a <code>-a</code> option now for attachments.</p>\n'}, {'answer_id': 24479275, 'author': 'Yoav', 'author_id': 868331, 'author_profile': 'https://Stackoverflow.com/users/868331', 'pm_score': 0, 'selected': False, 'text': '<p>Not a method for sending email, but you can use an online Git server (e.g. Bitbucket or a similar service) for that.</p>\n\n<p>This way, you can use <code>git push</code> commands, and all versions will be stored in a compressed and organized way.</p>\n'}, {'answer_id': 25266816, 'author': 'Alejandro Santillan', 'author_id': 3933842, 'author_profile': 'https://Stackoverflow.com/users/3933842', 'pm_score': 1, 'selected': False, 'text': '<p>I usually only use the mail command on RHEL. I have tried mailx and it is pretty efficient.</p>\n\n<pre><code>mailx -s "Sending Files" -a First_LocalConfig.conf -a\nSecond_LocalConfig.conf Recipient@myemail.com\n\nThis is the content of my msg.\n\n.\n</code></pre>\n'}, {'answer_id': 29638575, 'author': 'Pipo', 'author_id': 2118777, 'author_profile': 'https://Stackoverflow.com/users/2118777', 'pm_score': 0, 'selected': False, 'text': '<p>the shortest way for me is</p>\n\n<pre><code>file=filename_or_filepath;uuencode $file $file|mail -s "optional subject" email_address\n</code></pre>\n\n<p>so for your example it\'ll be</p>\n\n<pre><code>file=your_sql.log;gzip -c $file;uuencode ${file}.gz ${file}|mail -s "file with magnets" ph.gachoud@gmail.com\n</code></pre>\n\n<p>the good part is that I can recall it with <a href="http://lifehacker.com/278888/ctrl%252Br-to-search-and-other-terminal-history-tricks" rel="nofollow">Ctrl+r</a> to send another file... </p>\n'}, {'answer_id': 30393142, 'author': 'Sourabh Potnis', 'author_id': 3322308, 'author_profile': 'https://Stackoverflow.com/users/3322308', 'pm_score': 5, 'selected': False, 'text': "<pre><code> echo -e 'Hi, \\n These are contents of my mail. \\n Thanks' | mailx -s 'This is my email subject' -a /path/to/attachment_file.log -b bcc.email@example.com -c cc.email@example.com -r from.email@example.com to.email1@example.com to.email2@example.com to.email3@example.com\n</code></pre>\n"}, {'answer_id': 31813090, 'author': 'poncho', 'author_id': 5190280, 'author_profile': 'https://Stackoverflow.com/users/5190280', 'pm_score': 1, 'selected': False, 'text': '<p>I used </p>\n\n<pre><code>echo "Start of Body" &amp;&amp; uuencode log.cfg readme.txt | mail -s "subject" "a@b.c" \n</code></pre>\n\n<p>and this worked well for me....</p>\n'}, {'answer_id': 32399399, 'author': 'dagorv', 'author_id': 5300945, 'author_profile': 'https://Stackoverflow.com/users/5300945', 'pm_score': 0, 'selected': False, 'text': "<p>This is how I am doing with one large log file in CentOS:</p>\n<pre><code>#!/bin/sh\nMAIL_CMD=&quot;$(which mail)&quot;\nWHOAMI=&quot;$(whoami)&quot;\nHOSTNAME=&quot;$(hostname)&quot;\nEMAIL&quot;your@email.address&quot;\nLOGDIR=&quot;/var/log/aide&quot;\nLOGNAME=&quot;$(basename &quot;$0&quot;)_$(date &quot;+%Y%m%d_%H%M&quot;)&quot;\n\nif cd ${LOGDIR}; then\n  /bin/tar -zcvf &quot;${LOGDIR}/${LOGNAME}&quot;.tgz &quot;${LOGDIR}/${LOGNAME}.log&quot; &gt; /dev/null 2&gt;&amp;1\n  if [ -n &quot;${MAIL_CMD}&quot; ]; then\n  # This works too. The message content will be taken from text file below\n  # echo 'Hello!' &gt;/root/scripts/audit_check.sh.txt\n  # echo &quot;Attachment&quot; | ${MAIL_CMD} -s &quot;${HOSTNAME} Aide report&quot; -q /root/scripts/audit_check.sh.txt -a ${LOGNAME}.tgz -S from=${WHOAMI}@${HOSTNAME} ${EMAIL}\n    echo &quot;Attachment&quot; | ${MAIL_CMD} -s &quot;${HOSTNAME} Aide report&quot; -a &quot;${LOGNAME}.tgz&quot; -S from=&quot;${WHOAMI}@${HOSTNAME}&quot; &quot;${EMAIL}&quot;\n    /bin/rm &quot;${LOGDIR}/${LOGNAME}.log&quot;\n  fi\nfi\n</code></pre>\n"}, {'answer_id': 41791423, 'author': 'Konchog', 'author_id': 5678653, 'author_profile': 'https://Stackoverflow.com/users/5678653', 'pm_score': 1, 'selected': False, 'text': '<p><em>From source machine</em></p>\n\n<pre><code>mysqldump --defaults-extra-file=sql.cnf database | gzip | base64 | mail me@myemail.com\n</code></pre>\n\n<p><em>On Destination machine. Save the received mail body as <strong>db.sql.gz.b64</strong>; then..</em></p>\n\n<pre><code>base64 -D -i db.sql.gz.b64 | gzip -d | mysql --defaults-extra-file=sql.cnf\n</code></pre>\n'}, {'answer_id': 42414622, 'author': 'nurp', 'author_id': 2232573, 'author_profile': 'https://Stackoverflow.com/users/2232573', 'pm_score': 0, 'selected': False, 'text': '<p>If the file is text, you can send it easiest in the body as:</p>\n\n<pre><code>sendmail recipient@example.com &lt; message.txt\n</code></pre>\n'}, {'answer_id': 43997990, 'author': 'Paras Singh', 'author_id': 3841982, 'author_profile': 'https://Stackoverflow.com/users/3841982', 'pm_score': -1, 'selected': False, 'text': '<p>If mutt is not working or not installed,try this-</p>\n\n<pre><code>*#!/bin/sh\n\nFilePath=$1\nFileName=$2\nMessage=$3\nMailList=$4\n\ncd $FilePath\n\nRec_count=$(wc -l &lt; $FileName)\nif [ $Rec_count -gt 0 ]\nthen\n(echo "The attachment contains $Message" ; uuencode $FileName $FileName.csv ) | mailx -s "$Message" $MailList\nfi*\n</code></pre>\n'}, {'answer_id': 46052152, 'author': 'Alexander Yancharuk', 'author_id': 2648942, 'author_profile': 'https://Stackoverflow.com/users/2648942', 'pm_score': 3, 'selected': False, 'text': '<p>Another alternative - <a href="http://www.jetmore.org/john/code/swaks/" rel="noreferrer">Swaks</a> (Swiss Army Knife for SMTP).</p>\n\n<pre><code>swaks -tls \\\n    --to ${MAIL_TO} \\\n    --from ${MAIL_FROM} \\\n    --server ${MAIL_SERVER} \\\n    --auth LOGIN \\\n    --auth-user ${MAIL_USER} \\\n    --auth-password ${MAIL_PASSWORD} \\\n    --header "Subject: $MAIL_SUBJECT" \\\n    --header "Content-Type: text/html; charset=UTF-8" \\\n    --body "$MESSAGE" \\\n    --attach mysqldbbackup.sql\n</code></pre>\n'}, {'answer_id': 46114228, 'author': 'Girdhar Singh Rathore', 'author_id': 5115670, 'author_profile': 'https://Stackoverflow.com/users/5115670', 'pm_score': 1, 'selected': False, 'text': '<p><strong>using mailx command</strong></p>\n<pre><code> echo &quot;Message Body Here&quot; | mailx -s &quot;Subject Here&quot; -a file_name user@example.com\n</code></pre>\n<p><strong>using sendmail</strong></p>\n<pre><code>#!/bin/ksh\n\nfileToAttach=data.txt\n\n`(echo &quot;To: user@company.example&quot;\n  echo &quot;Cc: user@company.example&quot;\n  echo &quot;From: Application&quot;\n  echo &quot;Subject: your subject&quot;\n  echo  your body\n  uuencode $fileToAttach $fileToAttach\n  )| eval /usr/sbin/sendmail -t `;\n</code></pre>\n'}, {'answer_id': 48588035, 'author': 'tripleee', 'author_id': 874188, 'author_profile': 'https://Stackoverflow.com/users/874188', 'pm_score': 4, 'selected': False, 'text': '<p>There are several answers here suggesting <code>mail</code> or <code>mailx</code> so this is more of a background to help you interpret these in context.</p>\n<h3>Historical Notes</h3>\n<p>The origins of Unix <a href="https://en.wikipedia.org/wiki/Mail_(Unix)" rel="nofollow noreferrer"><code>mail</code></a> go back into the mists of the early history of Bell Labs Unix™ (1969?), and we probably cannot hope to go into its full genealogy here. Suffice it to say that there are many programs which inherit code from or reimplement (or inherit code from a reimplementation of) <code>mail</code> and that there is no single code base which can be unambiguously identified as &quot;the&quot; <code>mail</code>.</p>\n<p>However, one of the contenders to that position is certainly &quot;Berkeley Mail&quot; which was originally called <code>Mail</code> with an uppercase M in 2BSD (1978); but in 3BSD (1979), it replaced the lowercase <code>mail</code> command as well, leading to some new confusion. SVR3 (1986) included a derivative which was called <code>mailx</code>.  The <code>x</code> was presumably added to make it unique and distinct; but this, too, has now been copied, reimplemented, and mutilated so that there is no single individual version which is definitive.</p>\n<p>Back in the day, the <em>de facto</em> standard for sending binaries across electronic mail was <a href="https://en.wikipedia.org/wiki/Uuencoding" rel="nofollow noreferrer"><code>uuencode</code></a>.  It still exists, but has numerous usability problems; if at all possible, you should send MIME attachments instead, unless you specifically strive to be able to communicate with the late 1980s.</p>\n<p><a href="https://en.wikipedia.org/wiki/MIME" rel="nofollow noreferrer">MIME</a> was introduced in the early 1990s to solve several problems with email, including support for various types of content other than plain text in a single character set which only really is suitable for a subset of English (and, we are told, Hawai\'ian).  This introduced support for multipart messages, internationalization, rich content types, etc, and quickly gained traction throughout the 1990s.</p>\n<p>(The <a href="http://heirloom.sourceforge.net/mailx_history.html" rel="nofollow noreferrer">Heirloom <code>mail</code>/<code>mailx</code> history notes</a> were most helpful when composing this, and are certainly worth a read if you\'re into that sort of thing.)</p>\n<h3>Current Offerings</h3>\n<p>As of 2018, Debian has three packages which include a <code>mail</code> or <code>mailx</code> command. (You can search for <code>Provides: mailx</code>.)</p>\n<pre><code>debian$ aptitude search ~Pmailx\ni   bsd-mailx                       - simple mail user agent\np   heirloom-mailx                  - feature-rich BSD mail(1)\np   mailutils                       - GNU mailutils utilities for handling mail\n</code></pre>\n<p>(I\'m not singling out Debian as a recommendation; it\'s what I use, so I am familiar with it; and it provides a means of distinguishing the various alternatives unambiguously by referring to their respective package names. It is obviously also the distro from which Ubuntu gets these packages.)</p>\n<ul>\n<li><code>bsd-mailx</code> is a relatively simple <code>mailx</code> which does <em>not</em> appear to support sending MIME attachments. See its <a href="http://manpages.ubuntu.com/manpages/zesty/man1/bsd-mailx.1.html" rel="nofollow noreferrer">manual page</a> and note that this is the one you would expect to find on a *BSD system, including MacOS, by default.</li>\n<li><code>heirloom-mailx</code> is now being called <code>s-nail</code> and <em>does</em> support sending MIME attachments with <code>-a</code>.  See its <a href="http://manpages.ubuntu.com/manpages/zesty/man1/heirloom-mailx.1.html" rel="nofollow noreferrer">manual page</a> and more generally the <a href="https://en.wikipedia.org/wiki/Heirloom_Project" rel="nofollow noreferrer">Heirloom project</a></li>\n<li><code>mailutils</code> aka <a href="https://www.gnu.org/software/mailutils/" rel="nofollow noreferrer">GNU Mailutils</a> includes a <a href="https://mailutils.org/manual/html_section/mail.html" rel="nofollow noreferrer"><code>mail</code>/<code>mailx</code> compatibility wrapper</a> which <em>does</em> support sending MIME attachments with <code>-A</code></li>\n</ul>\n<p>With these concerns, if you need your code to be portable and can depend on a somewhat complex package, the simple way to portably send MIME attachments is to use <code>mutt</code>.</p>\n<p>If you know what you are doing, you can assemble an arbitrary MIME structure with the help of <code>echo</code> and <code>base64</code> and e.g. <a href="https://manpages.ubuntu.com/manpages/impish/man1/qprint.1.html" rel="nofollow noreferrer"><code>qprint</code></a> (or homegrown replacements; both <code>base64</code> and <code>qprint</code> can easily be implemented as Perl one-liners) and pipe it to <code>sendmail</code>; but as several other answers on this page vividly illustrate, you probably don\'t.</p>\n<pre><code>( printf \'%s\\n\' \\\n    &quot;From: myself &lt;sender@example.org&gt;&quot; \\\n    &quot;To: backup address &lt;backup@email.example&gt;&quot; \\\n    &quot;Subject: Backup of $(date)&quot; \\\n    &quot;MIME-Version: 1.0&quot; \\\n    &quot;Content-type: application/octet-stream; filename=\\&quot;mysqldbbackup.sql\\&quot;&quot; \\\n    &quot;Content-transfer-encoding: base64&quot; \\\n    &quot;&quot;\n  base64 &lt; mysqldbbackup.sql ) |\nsendmail -oi -t\n</code></pre>\n'}, {'answer_id': 54726498, 'author': 'rumpel', 'author_id': 597401, 'author_profile': 'https://Stackoverflow.com/users/597401', 'pm_score': 2, 'selected': False, 'text': '<p>Mailutils makes this a piece of cake</p>\n\n<pre><code>echo "Body" | mail.mailutils -M -s "My Subject" -A attachment.pdf mail@example.org\n</code></pre>\n\n<ul>\n<li><code>-A file</code> attaches a file</li>\n<li><code>-M</code> enables MIME, so that you can have an attachment and plaintext body.</li>\n</ul>\n\n<p>If not yet installed, run</p>\n\n<pre><code>sudo apt install mailutils\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/17359', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1339/']