Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I send raw MIME attachments?

Sending email

How do I send raw MIME attachments?

by  tanderso  Posted    (Edited  )
# 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
[color #000088]
open (MAIL, "| /usr/sbin/sendmail -t >& /dev/null");
[/color]
# Then print the headers.
[color #000088]
print MAIL qq~To: "$title" <$email_addr>
From: "$return_name" <$return_addr>
Subject: $subject
~;
[/color]
# 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.
[color #000088]
print MAIL qq~MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="some_string_unlikely_to_occur_in_body"\n\n~;
[/color]
# Next you can add a preamble if you like, but you can
# also leave this out.
[color #000088]
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~;
[/color]
# 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.
[color #000088]
print MAIL qq~
--some_string_unlikely_to_occur_in_body
Content-Type: text/plain; charset=us-ascii\n\n~;
[/color]
# Then you can print the content of this message part
[color #000088]
print MAIL qq~This is the email body.~;
[/color]
# 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.
[color #000088]
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~;
[/color]
# Then you can print the content of this message part too
[color #000088]
print MAIL qq~This is a text file attachment. Change my content-type (and content obviously) to make me an image or something else.~;
[/color]
# When you're done with all message parts, print out the
# boundary string again, this time also followed by two
# dashes.
[color #000088]
print MAIL qq~
--some_string_unlikely_to_occur_in_body--
~;
[/color]
# You can optionally include an epilogue at the end.
[color #000088]
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);
[/color]
# 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.

# I hope this is helpful!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top