How can I send email attachment without using an additional library in Perl?

后端 未结 6 792
感情败类
感情败类 2021-01-15 06:49

Hey, I was wondering if there is a way to attach files (specifically .csv files) to a mail message in Perl without using MIME::Lite or any other libraries.

Right now

相关标签:
6条回答
  • 2021-01-15 06:52

    General style tips to make your life easier:

    • use lexical file handles
    • use 3-arg-open
    • check return values

    Ie:

    open my $mail, '|-', '/usr/sbin/sendmail', '-t'  or Carp::croak("Cant start sendmail, $! $@");
    
    print $mail  "foo";
    
    close $mail or Carp::croak("SendMail might have died! :( , $! $@");
    

    perldoc -f open

    0 讨论(0)
  • 2021-01-15 06:56

    If part of your problem is that you're on shared hosting and cannot install extra libraries, they can usually be installed in (and used from) a local a library (e.g., ~/lib). There are instructions for that over here (under "I don't have permission to install a module on the system!").

    0 讨论(0)
  • 2021-01-15 07:02

    you can specify the mail-headers as :

    Content-Type ie: image/jpeg; name="file.jpg"
    Content-Disposition (ie ) attachment; filename="name.jpg"
    Content-Transfer-Encoding (ie) base64

    Look at an email sent with an attachment, that should help you out.

    the trick is multipart boundaries.
    http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

    0 讨论(0)
  • 2021-01-15 07:07

    Why do you want to write code that already exists? There's probably a much better way to solve your task than recreating bugs and maintaining more code yourself. Are you having a problem installing modules? There are ways that you can distribute third-party modules with your code, too.

    If you want to do it yourself, you just have to do the same things the module does for you. You can just look at the code to see what they did. You just do that. It is open source after all. :)

    0 讨论(0)
  • 2021-01-15 07:08

    Example - Email a zipped file as an attachment:

    base64 /path/to/my/file.zip | mail -s "Subject" recipient@mydomain.com -a 'Content-Type: application/zip; name="myfile.zip"' -a 'Content-Disposition: attachment' -a 'Content-Transfer-Encoding: base64'
    
    0 讨论(0)
  • 2021-01-15 07:15
    print "To: ";       my $to=<>;      chomp $to;
    print "From: ";     my $from=<>;    chomp $from;
    print "Attach: ";   my $attach=<>;  chomp $attach;
    print "Subject: ";  my $subject=<>; chomp $subject;
    print "Message: ";  my $message=<>; chomp $message;
    
    my $mail_fh = \*MAIL;
    open $mail_fh, "|uuencode $attach $attach |mailx -m -s \"$subject\" -r $from $to";
    print $mail_fh $message;
    close($mail_fh);
    
    0 讨论(0)
提交回复
热议问题