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

Sending attachments in emails using SMTP

by biswanath_c (Beadle)
on Jul 08, 2008 at 19:08 UTC ( #696294=perlquestion: print w/ replies, xml ) Need Help??
biswanath_c has asked for the wisdom of the Perl Monks concerning the following question:


Hi

i have a code now that sends email to a bunch of guys all right. Now, i would like to send attachments (txt files) in the email.

A few things:

i used this snippet that i got from another node in perl monks:

open CSVFH, "< $attach_file_1";

while (<CSVFH>) { chomp; $smtp->datasend("$_\n"); }

close CSVFH;

$smtp->datasend("Content-Type: application/text; name= $attach_file_2");

$smtp->datasend("Content-Disposition: attachment; filename=\"$attach_file_2\" \n");

$smtp->datasend("\n");

but this sends the files that i want to attach as the body of the email and NOT as actual attachments

And, i cannot download the MIME:Lite module. I HAVE to use SMTP only!

can someone help me achieve this?

Thanks

Comment on Sending attachments in emails using SMTP
Re: Sending attachments in emails using SMTP
by Tanktalus (Canon) on Jul 08, 2008 at 19:15 UTC
      I am not allowed to download modules as a company policy !
        Are you allowed to copy and paste bits of code from the internet into your own code? Then copy some code (Ctl-A then Ctl-C on a windows box). Though I don't really see the difference between that and "downloading."

        You probably should appeal to your company in that case to make an exception, because there's not much use in anyone here reinventing the wheel for you.

        MIME::Lite is a pure perl module, and doesn't really need to be installed, worst case scenario you could unpack the whole thing into the same directory as your cgi/script.

        Does your company not allow any use of modules, even ones that you maintain?

Re: Sending attachments in emails using SMTP
by Zen (Deacon) on Jul 08, 2008 at 20:13 UTC
    Shell out.

    http://www.unix.com/aix/36448-command-line-script-send-e-mail-html-body-binary-attachment.html
      Thanks for the replies! But i am using Windows ! How do i send attachments using SMTP in Windows? (sorry! i should have mentioned that earlier! )
            How do i send attachments using SMTP in Windows?

        Regardless of what platform you are on, I'd recommend you read (and understand) the excellent How-To published by Corion A detailed How-To for locally installing modules. Install cpan;//MIME::Lite locally and use it. You'll be glad you did.

        If perchance you cannot for some misbegotten reason install the module locally I'd suggest you copy/paste the pertinate pieces of code from that module (as others have suggested) appropriately and use that.

        If that still doesn't work for you... give up.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Sending attachments in emails using SMTP
by zentara (Archbishop) on Jul 09, 2008 at 12:00 UTC
    Here is one that sends an image, you can alter it to send text attachments. For real attachments, you need to setup boundary layers, and a Content-type: multipart/mixed; boundary=\"=........

    Don't ask me to explain this..... I tested it once and it worked.... but I use MIME::Lite. :-)

    #!/usr/bin/perl use warnings; use strict; # works but.................. # needs some taint checking if used with CGI # You would be wise to consider what happens if you try: # http://www.you.com/cgi-bin/sendattach.pl?`myonewordcommandwithoutarg +s` # and consider if you really want to give the whole world access to # execute arbitrary commands on your server. # Put the name of your file here my $filename = shift || $0 ; # Location of sendmail program on your host my $mailprog = '/usr/sbin/sendmail'; # The email where you want the attachment to be sent to. my $to = 'zentara@zentara.zentara.net'; # Who the message is from my $from = 'zentara@zentara.zentara.net'; # Subject of the email. my $subject = "Attachment!"; # Type your message here. my $message = "This is an attachment."; # Confirmation message that the email has been sent. my $confirm_message = "The email and attachment has been sent!"; my $boundary = 'qwertry'.time; ###################################################### # Encode according to file extention. my $content_type = "text/plain; charset=\"us-ascii\";"; #default my $encoding; if ( $filename =~ /\.gif$/i ){ $content_type = "image/gif; name=\"$filename\"; $encoding = 'base64';"} if ( $filename =~ /\.jpg$/i ){ $content_type = "image/jpeg; name=\"$filename\"; $encoding = 'base64';"} if ( $filename =~ /\.zip$/i ){ $content_type = "application/zip; name=\"$filename\"; $encoding = 'base64';"} if ( $filename =~ /\.html$/i || $filename =~ /\.htm$/i ){ $content_type = "text/html; charset=\"us-ascii\";";} if ( $filename =~ /\.xls$/i ){ $content_type = "application/msexcel; charset=\"us-ascii +\";";} if ( $filename =~ /\.txt$/i || $filename =~ /\.dat$/i || $filename =~ /\.doc$/i || $filename =~ /\.pl$/i || $filename =~ /\.cgi$/i ){ $content_type = "text/plain; charset=\"us-ascii\";";} ############################################# # Get the attachment my $b64image; my @file; if($encoding){ open( IMAGE, "$filename"); binmode(IMAGE); undef $/; my $rawimage = <IMAGE>; $/ = "\n"; close IMAGE; $b64image = &encode_base64($rawimage); chomp $b64image; }else{ # Doesn't need encoding. (Plain text type of document) open( FILE, $filename ); @file = <FILE>; close(FILE); } ############################################# # Send Email open( MAIL, "| $mailprog $to" ) || die("$0: Fatal Error! Cannot open sendmail:: $!\n"); print MAIL "Reply-to: $from\n"; print MAIL "From: $from\n"; print MAIL "To: $to\n"; print MAIL "Subject: $subject\n"; print MAIL "Mime-Version: 1.0\n"; print MAIL "Content-type: multipart/mixed; boundary=\"======================_$bou +ndary==_\"\n"; print MAIL"--======================_$boundary==_ Content-Type: text/plain; charset=us-ascii"; print MAIL "\n\n"; print MAIL "$message\n\n"; print MAIL "--======================_$boundary==_ Content-Type: $content_type\n"; if($encoding){ print MAIL "Content-Transfer-Encoding: $encoding\n"; print MAIL "Content-Disposition: inline; filename=\"$filename\"\n\ +n"; print MAIL $b64image; }else{ print MAIL "Content-Disposition: inline; filename=\"$filename +\"\n\n"; foreach my $line (@file) { print MAIL "$line"; } } print MAIL "\n\n\n"; print MAIL "--======================_$boundary==_--"; close(MAIL); ####################################### # Confirm Mail has been sent print "Content-type: text/html\n\n"; print " $confirm_message\n\n"; ####################################### sub encode_base64 { my $s = shift ; my $r = ''; while( $s =~ /(.{1,45})/gs ){ chop( $r .= substr(pack("u",$1),1) ); } my $pad=(3-length($s)%3)%3; $r =~ tr|` -_|AA-Za-z0-9+/|; $r=~s/.{$pad}$/"="x$pad/e if $pad; $r=~s/(.{1,72})/$1\n/g; $r; }

    I'm not really a human, but I play one on earth CandyGram for Mongo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (16)
As of 2014-12-18 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (59 votes), past polls