×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

最简单的是uuencode,要是想搞点复杂的,可以写点小程序。

本文发表在 rolia.net 枫下论坛这个是我以前写的Perl script,很简陋,参考下罢了
缺点是要装MIME:Lite之类Perl CPAN Lib.
如果你机子上有Java, 用JavaMail 一样好,还不用装任何东西

#!/usr/local/bin/perl -w
#
#Scripts wrapper to send out Metrics XLS everyday
use strict;
use MIME::Lite;
use Net::SMTP;
use Getopt::Std;

sub Usage($) {
my $program = shift;
print <<EOF;
This script will send a file by attach it as a MIME attachemnt
Usage: $program [-f from_address] [-s subject] -t to_address the_file_name_you_want_to_send

EOF

exit (1);
}

my $program = $0;
my $hostname = `/usr/bin/hostname`; chomp $hostname;
my $from_address = 'mimeSender@'.$hostname.'.telus.net';

my $to_address ;
my $subject;
my $mime_type = 'TEXT';
my $message = "This is a message sent from $hostname by mime message\
sender,\npls check attachment.\n";

my @timebit = localtime();
$subject = ($timebit[5]+1900) . "-" . ($timebit[4]+1) . "-" . $timebit[3];
$subject = "File sent on $subject";

my %opts;
getopts('f:s:t:', \%opts) || Usage($program);

if (!defined $opts{t}) {Usage($program); exit(1);}

$to_address = $opts{t};
$subject = $opts{s} if defined $opts{s};

# Create the initial text of the message
my $mime_msg = MIME::Lite->new(
From => $from_address,
To => $to_address,
Subject => $subject,
Type => $mime_type,
Data => $message
)
or die "Error creating MIME body: $!\n";

#Get the program's running path
my $PathToProgram;
$0 =~ m!^(.*/)!;
$PathToProgram = $1;

#my $filename = shift || ($PathToProgram . "report.txt.gz");
my $filename = shift;
if (!defined $filename) { Usage($program); exit(1); }
my $recommended_filename = `/usr/bin/basename $filename`;
chomp $recommended_filename;

# Attach the test file
$mime_msg->attach(
Type => 'application/zip',
Path => $filename,
Filename => $recommended_filename
)
or die "Error attaching test file: $!\n";


my $message_body = $mime_msg->body_as_string();

$mime_msg->send();

## END更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 怎么在UNIX中把一个文件以attachment的方式发送出去
    mailx不能带attachment, sendmail又不能带subject. 有什么方法 在sendmail里放subject.
    • i use postie, an open source program u can compile it if it is doable. or depending what OS u use. get a mail client that can do attachment.
      if u r fan of command line script.
      use this:

      uuencode file_to_be_sent | mail vetra@rolia.net
      • 我用了mail , attachment变成了body 出现,只有sendmail可以实现,唯一的不足是,我没有找到写Subject这个参数
        • r u saying u use sendmail as a client interface for composing the mail message directly?
          • uuuencode file.csv file.csv | sendmail -t -F "xxx" email_address 但是email_address只能是公司内部的,好像不能发出去
            • check ur sendmail.rc file. u guys don't have sys admin?
              • 我们没有Full time的admin,基本上就是我在顶,我们用default mailhost,然后就在host文件里指向内部的mail server
      • 我不希望用open source,毕竟我不管这些,将来移植会是麻烦。 要不然,ELM也是可以的
        • 有个恐龙命令叫uuencode
          • 是啊,我是用 uuencode的,不过,收到email的时候没有Subject
            • some "mail" command has -s option, some u just need to add one line echo/print "Subject:TTTTTTT\r\n" in ur mail message
              • sendmail的命令如果用导向符送入相应的To, CC, Subject后,attachment就掉了,本来我也只是精益求精
              • 我晕死,一个下午。。。 所有没有收到的信,还以为自己写错命令呢,结果全在Junk里面。
                • LOL
    • 最简单的是uuencode,要是想搞点复杂的,可以写点小程序。
      本文发表在 rolia.net 枫下论坛这个是我以前写的Perl script,很简陋,参考下罢了
      缺点是要装MIME:Lite之类Perl CPAN Lib.
      如果你机子上有Java, 用JavaMail 一样好,还不用装任何东西

      #!/usr/local/bin/perl -w
      #
      #Scripts wrapper to send out Metrics XLS everyday
      use strict;
      use MIME::Lite;
      use Net::SMTP;
      use Getopt::Std;

      sub Usage($) {
      my $program = shift;
      print <<EOF;
      This script will send a file by attach it as a MIME attachemnt
      Usage: $program [-f from_address] [-s subject] -t to_address the_file_name_you_want_to_send

      EOF

      exit (1);
      }

      my $program = $0;
      my $hostname = `/usr/bin/hostname`; chomp $hostname;
      my $from_address = 'mimeSender@'.$hostname.'.telus.net';

      my $to_address ;
      my $subject;
      my $mime_type = 'TEXT';
      my $message = "This is a message sent from $hostname by mime message\
      sender,\npls check attachment.\n";

      my @timebit = localtime();
      $subject = ($timebit[5]+1900) . "-" . ($timebit[4]+1) . "-" . $timebit[3];
      $subject = "File sent on $subject";

      my %opts;
      getopts('f:s:t:', \%opts) || Usage($program);

      if (!defined $opts{t}) {Usage($program); exit(1);}

      $to_address = $opts{t};
      $subject = $opts{s} if defined $opts{s};

      # Create the initial text of the message
      my $mime_msg = MIME::Lite->new(
      From => $from_address,
      To => $to_address,
      Subject => $subject,
      Type => $mime_type,
      Data => $message
      )
      or die "Error creating MIME body: $!\n";

      #Get the program's running path
      my $PathToProgram;
      $0 =~ m!^(.*/)!;
      $PathToProgram = $1;

      #my $filename = shift || ($PathToProgram . "report.txt.gz");
      my $filename = shift;
      if (!defined $filename) { Usage($program); exit(1); }
      my $recommended_filename = `/usr/bin/basename $filename`;
      chomp $recommended_filename;

      # Attach the test file
      $mime_msg->attach(
      Type => 'application/zip',
      Path => $filename,
      Filename => $recommended_filename
      )
      or die "Error attaching test file: $!\n";


      my $message_body = $mime_msg->body_as_string();

      $mime_msg->send();

      ## END更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • thanks, uuencode is working fine for me, they were in my junk folder, i did not check them out.