Linux – How to send email with attachment by postfix from command-line

command lineemaillinuxpostfixUbuntu

I've just setup a new Ubuntu 10.04 LTS server on linode for myself. Followed an excellent instruction at: here to finish the installation of some basic stuff including postfix.

I am trying to figure out a way to send an email to my gmail address with an attachment, but cannot find how. Already confirmed that email can reach my gmail account.

In the end I have to use mutt to send the email with attachments, probably SendEmails will also do well, but I am wondering how to do the same thing in postfix from command-line?

Many thanks in advance for this dumb question.

Best Answer

Why does it have to be postfix directly? mailx -a, mutt, or mutt -a will also use the sendmail interface -- unless you configure them for SMTP.

echo "This is a test message" | mutt -s Test -a foo.zip -- $USER
echo "This is a test message" | mail -s Test -a foo.zip $USER

(Note, in bsd-mailx the option is -A instead.)


Anyway, here's a "postfix" example.

Replace $USER, content type and filename to match your environment. Example assumes you are sending the message to yourself and have a ZIP file foo.zip in the current directory.

(printf "%s\n" \
    "Subject: test" \
    "To: $USER" \
    "Content-Type: application/zip" \
    "Content-Disposition: attachment; filename=foo.zip" \
    "Content-Transfer-Encoding: base64" \
    "";
 base64 foo.zip) | sendmail "$USER"

(Creation of MIME multipart messages left as an exercise to the reader.)

Related Question