How Do I Send a File as an Email Attachment Using Linux Command Line?
I've Created a Script That Runs Every Night on My Linux Server That Uses Mysqldump to Back up Each of My Mysql Databases To. Sql Files and Packages Them...
I've created a script that runs every night on my Linux server that uses mysqldump 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 mailx like so:
$ cat mysqldbbackup.sql | mailx
cat echoes the backup file's text which is piped into the mailx program with the recipient's email address passed as an argument.
While this accomplishes what I need, I think it could be one step better, Is there any way, using shell scripts or otherwise, to send the compressed .tar file to an outgoing email message as an attachment? This would beat having to deal with very long email messages which contain header data and often have word-wrapping issues etc.
25 Answers
None of the mutt ones worked for me. It was thinking the email address was part of the attachemnt. Had to do:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" --
Or, failing mutt:
gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz | mail -s "MySQL DB"
Depending on your version of linux it may be called mail. To quote @David above:
mail -s "Backup" -a mysqldbbackup.sql < message.txt
or also:
cat message.txt | mail -s "Backup" -a mysqldbbackup.sql
From looking at man mailx, the mailx program does not have an option for attaching a file. You could use another program such as mutt.
echo "This is the message body" | mutt -a file.to.attach -s "subject of message"
Command line options for mutt can be shown with mutt -h.
I use SendEmail, which was created for this scenario. It's packaged for Ubuntu so I assume it's available
sendemail -f -t -m "Here are your files!" -a file1.jpg file2.zip
I use mpack.
mpack -s subject file
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:
mpack -s subject /dev/stdin < file
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 -c -r
I once wrote this function for ksh on Solaris (uses Perl for base64 encoding):
# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
to="$1"
cc="$2"
subject="$3"
body="$4"
filename="${5:-''}"
boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
{
print -- "To: $to"
print -- "Cc: $cc"
print -- "Subject: $subject"
print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
print -- "Mime-Version: 1.0"
print -- ""
print -- "This is a multi-part message in MIME format."
print -- ""
print -- "--$boundary"
print -- "Content-Type: text/plain; charset=ISO-8859-1"
print -- ""
print -- "$body"
print -- ""
if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
print -- "--$boundary"
print -- "Content-Transfer-Encoding: base64"
print -- "Content-Type: application/octet-stream; name=$filename"
print -- "Content-Disposition: attachment; filename=$filename"
print -- ""
print -- "$(perl -MMIME::Base64 -e 'open F, shift; @lines=<F>; close F; print MIME::Base64::encode(join(q{}, @lines))' $filename)"
print -- ""
fi
print -- "--${boundary}--"
} | /usr/lib/sendmail -oi -t
}
You can use mutt to send the email with attachment
mutt -s "Backup" -a mysqldbbackup.sql < message.txt
Must Read
Send a Plaintext body email with one plaintext attachment with mailx:
(
/usr/bin/uuencode attachfile.txt myattachedfilename.txt;
/usr/bin/echo "Body of text"
) | mailx -s 'Subject'
Below is the same command as above, without the newlines
( /usr/bin/uuencode /home/el/attachfile.txt myattachedfilename.txt; /usr/bin/echo "Body of text" ) | mailx -s 'Subject'
Make sure you have a file /home/el/attachfile.txt defined with this contents:
<html><body>
Government discriminates against programmers with cruel/unusual 35 year prison
sentences for making the world's information free, while bankers that pilfer
trillions in citizens assets through systematic inflation get the nod and
walk free among us.
</body></html>
If you don't have uuencode read this: