# In order to send MIME attachments directly to sendmail # or another mail program as raw source rather than using # an object-oriented module to do this for you, you would # need to use a multipart content-type and format the body # of your message.
# start by piping a stream to your mailer
open (MAIL, "| /usr/sbin/sendmail -t >& /dev/null");
# Print the MIME-version you're using and the type of # multipart message you're sending. There are several # you can use, but multipart/mixed will suffice. After # the multipart type, use a semicolon and then set the # boundary string you'd like to use to seperate the parts # of the email. It should be something that isn't likely # to appear in the body of the email.
print MAIL qq~MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="some_string_unlikely_to_occur_in_body"\n\n~;
# Next you can add a preamble if you like, but you can # also leave this out.
print MAIL qq~This is the preamble... nobody with an MIME email client can read this. It is a comment for non-MIME-capable clients.\n~;
# When you're ready to start your email body or any other # message part, go ahead and print out your boundary # string preceeded by two dashes (--). Following that, # print the header for that message part, which may include # a content-type and content-disposition.
print MAIL qq~ --some_string_unlikely_to_occur_in_body Content-Type: text/plain; charset=us-ascii\n\n~;
# Then you can print the content of this message part
print MAIL qq~This is the email body.~;
# When you're ready to move on to the next message part, # print out your boundary string again with two dashes in # front of it. For inline message parts, you can set # content-disposition to "inline" or else leave it blank. # For attachments, set content-disposition to "attachment". # You can also include a filename which the attachment # should download and be saved as.
print MAIL qq~ --some_string_unlikely_to_occur_in_body Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="attach.txt"\n\n~;
# Then you can print the content of this message part too
print MAIL qq~This is a text file attachment. Change my content-type (and content obviously) to make me an image or something else.~;
# When you're done with all message parts, print out the # boundary string again, this time also followed by two # dashes.
print MAIL qq~ --some_string_unlikely_to_occur_in_body-- ~;
# You can optionally include an epilogue at the end.
print MAIL qq~This is the epilogue. Nobody with an MIME email reader should see me. As with the preamble, I'm just a comment for non-MIME email clients.~;
print MAIL qq~\n.~; close(MAIL);
# That's all there is to it. You can of course include # multipart content-types hierarchically within emails so # that you can attach emails with attachments, etc.
# Also, you can change multipart/mixed to # multipart/alternative, and then the MIME email client # should choose the best message part to display. This # is most often used to provide both a plain text version # and an HTML version of the same content. Only one or # the other should be displayed, but not both, depending # on the viewers' email client settings.