---
title: "Sendmails via Sendmail With Attachments"
description: "After Send mail via python it was time to try real sendmail. [ From the FAQ pages of sendmail ] How do I create attachments."
url: "https://h3manth.com/scribe/sendmails-sendmail-attachments/"
canonical_url: "https://h3manth.com/scribe/sendmails-sendmail-attachments/"
markdown_url: "https://h3manth.com/scribe/blog/sendmails-sendmail-attachments.md"
publishDate: "2010-06-22T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python","web"]
word_count: 256
reading_time_minutes: 2
x_markdown_tokens: 600
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Sendmails via Sendmail With Attachments

> After Send mail via python it was time to try real sendmail. [ From the FAQ pages of sendmail ] How do I create attachments.

After [Send mail via python](http://www.h3manth.com/content/send-mail-gmail-programatically-python) it was time to try real `sendmail`. ` [ From the FAQ pages of sendmail ] How do I create attachments with sendmail? You don't. Sendmail is a mail transfer agent (MTA). Creating e-mail messages, including adding attachments or signatures, is the function of a mail user agent (MUA). Some popular MUAs include mutt, elm, exmh, Netscape, Eudora and Pine. Some specialized packages (metamail, some Perl modules, etc.) can also be used to create messages with attachments. `

**But yes there is always a way out!** _"sendmail" as an MTA, it just sees data.If, we want to make "/usr/sbin/sendmail", as a MUA create a mail with attachments, the answer is no - use a MUA that can, or pre-build the mail and submit via sendmail -t_

**Hot Dope for simple text and html files can be seen below, the same can be used for others also by switching the content type as "Content-Type: MIMETYPE"**
    
    
    cat > mail << EOF
    "From: $From
    To: $TO
    Subject: $SUBJECT
    Mime-Version: 1.0
    Content-Type: text/plain\n"
    EOF
    
    cat YOUR_FILE >> mail
    
    /usr/lib/sendmail -t -oi < mail
    

**A common perl hack, makes things more robust**
    
    
    #!/usr/local/bin/perl -w
    
    use MIME::Lite;
    
    $PLAINFILE="/tmp/text";
    $BINFILE="/tmp/binary";
    $SUBJECT="Attachmented";
    $MAILTO="First.last@example.com";
    
    #Meta Msg creation
    $msg = new MIME::Lite 
       From    => "$ENV{LOGNAME}",
       To      => "$MAILTO",
       Subject => "$SUBJECT",
       Type    => 'multipart/mixed';
            
    # Attach
    attach $msg 
       Type     => 'text/plain',   
       Path     => "$PLAINFILE";
    attach $msg 
       Type     => 'application/octet-stream',
       Encoding => 'base64',
       Path     => "/tmp/$BINFILE",
       Filename => "$BINFILE";
    
    # Send message to sendmail
    
    open (SENDMAIL, "| /usr/lib/sendmail -t -oi");
    $msg->print(\*SENDMAIL);
    close(SENDMAIL);
