Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Sending email

How do I send raw MIME attachments? by tanderso
Posted: 18 Dec 02

# 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");

# Then print the headers.

print MAIL qq~To: "$title" <$email_addr>
From: "$return_name" <$return_addr>
Subject: $subject

# 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~
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~
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~

# 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.~;

# 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!

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close