Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

MIME Type Mailer Question

by Kage (Scribe)
on Dec 10, 2001 at 21:47 UTC ( [id://130719]=perlquestion: print w/replies, xml ) Need Help??

Kage has asked for the wisdom of the Perl Monks concerning the following question:

I found this script that allows for mailing attatchments, pariticularly ZIP files to people, but I need some help with manipulating it.. This is the file itself:
#!/usr/bin/perl ### User variables - you may change things in this section as indicate +d ### # Change this to the correct path to Sendmail for your system # $mailprog="/usr/sbin/sendmail"; # Change this to the LITERAL PATH to the files you wish to mail. Do no +t forget the ending / # $filelocation="/path/to/your/cgi-bin/attachit/";
# Change this to the email address you want mails to be sent FROM and +leave \ behind the @ sign# $adminmail="You\@YourDomain.com"; # Change this to the name you want mails to be sent FROM # $adminname="Webmaster"; # Change this to reflect the subject line you require for your mails # $subjectline="Your Download"; # Change this to show the message you want to include (i.e. the body o +f the email). # # A new line is entered by using \n e.g. Hi there\n\nThank-you for you +r interest in blah blah # # Note that "Dear Name\n\n" has been inserted for you automatically, N +ame being the name submitted # $message="Thank-you for your interest, we have pleasure in attaching s +omething small and hopefully relevant.\n\nRegards,\nEtc."; # Change this to the URL of the screen you want to display after the u +ser has submitted the form # #(i.e. a confirmation screen)# $returnscreen="http://www.YourDomain.com/confirm.htm"; # DO NOT alter anything from here on # read (STDIN,$temp,$ENV{'CONTENT_LENGTH'}); @pairs=split(/&/,$temp); foreach $item(@pairs) { ($key,$content)=split (/=/,$item); $content=~tr /+/ /; $content=~s /%(..)/pack("c",hex($1))/ge; $fields{$key}=$content; } require ("mimetypes.pl"); &sendproduct; sub sendproduct { $file=$filelocation.$fields{'attachment'}; ($ext) = $file =~ m,\.([^\.]*)$,; $ext =~ tr,a-z,A-Z,; $fext=&mimetype($ext); my @boundaryv = (0..9, 'A'..'F'); srand(time ^ $$); for (my $i = 0; $i++ < 24;) { $boundary .= $boundaryv[rand(@boundaryv)]; } open MAIL, "| $mailprog -t"; print MAIL "To: $fields{'email'}($fields{'name'})\n"; print MAIL "From: $adminmail ($adminname)\n"; print MAIL "MIME-Version: 1.0\n"; print MAIL "Subject: $subjectline\n"; print MAIL "Content-Type: multipart/mixed; boundary=\"------------$bou +ndary\"\n"; print MAIL "\n"; print MAIL "This is a multi-part message in MIME format.\n"; print MAIL "--------------$boundary\n"; print MAIL "Content-Type: text/plain; charset=us-ascii\n"; print MAIL "Content-Transfer-Encoding: 7bit\n\n"; print MAIL "Dear $fields{'name'},\n\n"; print MAIL $message; print MAIL "\n"; print MAIL "--------------$boundary\n"; print MAIL "Content-Type: $fext; name=\"$fields{'attachment'}\"\n"; print MAIL "Content-Transfer-Encoding: base64\n"; print MAIL "Content-Disposition: attachment; filename=\"$fields{'attac +hment'}\"\n\n"; my $buf; $/=0; open INPUT, "$file"; binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32'); while(read(INPUT, $buf, 60*57)) { print MAIL &encode_base64($buf); } close INPUT; print MAIL "\n--------------$boundary--\n"; print MAIL "\n"; close MAIL; print "Location: $returnscreen\n\n"; exit(); } sub encode_base64 #($) { my ($res, $eol, $padding) = ("", "\n", undef); while (($_[0] =~ /(.{1,45})/gs)) { $res .= substr(pack('u', $1), 1); chop $res; } $res =~ tr#` -_#AA-Za-z0-9+/#; # ` help emacs $padding = (3 - length($_[0]) % 3) % 3; # fix padding at the + end $res =~ s#.{$padding}$#'=' x $padding#e if $padding; # pad eoedv da +ta with ='s $res =~ s#(.{1,76})#$1$eol#g if (length $eol); # lines of at +least 76 characters return $res; }
And the MIME Type include looks like this:
sub mimetype { my %mime = ( #-------------------------------------<TEXT>----- 'HTML', "text/html", 'HTM', "text/html", ..... 'WRL', "x-world/x-vrml", 'VRML', "x-world/x-vrml", 'VRJ', "x-world/x-vrt", 'VRJT', "x-world/x-vrt", ); local ($y) = @_; $fext = $mime{$y}; return ($y); } return 1;

"I am loved by few, hated by many, and wanted by plenty." -Kage (Alex)
SkarySkriptz

Replies are listed 'Best First'.
Re: MIME Type Mailer Question
by davorg (Chancellor) on Dec 10, 2001 at 22:09 UTC

    It's a horrible script.

    • No use strict or -wT
    • No use CGI to handle CGI parameter parsing
    • And there are module on CPAN that will handle the creation and sending of MIME emails MIME::Lite for example

    Don't try to manipulate this file. Throw it away and start again.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: MIME Type Mailer Question
by grinder (Bishop) on Dec 10, 2001 at 22:24 UTC
    That looks awful. I would tend to throw it away and use something else. Here is a code fragment of a script I am working on at the moment. You should be able to adapt it to your use. The key to sanity is Mime::Entity which is part of the Mimetools bundle (or some damned named that I can never remember correctly).

    use strict; use MIME::Entity; my $mime = MIME::Entity->build( To => 'foo@example.com', Cc => 'bar@example.com', From => 'me@example.org', 'Reply-To' => 'postmaster@example.org', Subject => "Make Money Fast", 'X-Version' => $VERSION, Type => 'multipart/mixed', ); $mime->attach( Data => <<EMAIL_HEADER ); Hi, here are a number of ways you can MMF EMAIL_HEADER for my $file( @files ) { $mime->attach( Path => $file, Type => "application/octet-stream" . ";name=$file", Encoding => 'base64', ); } $mime->attach( Data => 'insert witty sig here' ); $mime->smtpsend;

    oh my god! I don't really send spam, honest.

    update: erm, the perils of cut'n'paste coding. Code rearranged slightly to make more sense :)

    --
    g r i n d e r
    just another bofh

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
Re: MIME Type Mailer Question
by demerphq (Chancellor) on Dec 10, 2001 at 22:20 UTC
    No question here. Use Mime::LITE. Its really simple and very powerful.

    And it will handle any number of attachments from any source (included from in memory). :-)

    Yves / DeMerphq
    --
    This space for rent.

Re: MIME Type Mailer Question
by AidanLee (Chaplain) on Dec 10, 2001 at 22:22 UTC
    You might also try Mail::Sender. I've had good experiences with it.
Re: MIME Type Mailer Question
by Kage (Scribe) on Dec 10, 2001 at 22:11 UTC
    Sorry I forgot to mention that I want it to be able to send MULTIPLE attatchments instread of one at a time
    "I am loved by few, hated by many, and wanted by plenty." -Kage (Alex)
    SkarySkriptz

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://130719]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found