Perl 的 mail modules

因為 Blogger 有提供 mail2blog (寄 mail 然後自動轉到 blog 上),所以我想把 Netflow 跑出來的 virus report 給貼上去,結果發現 Perl 的 mail modules 似乎怪怪的 -_____-

我寫了三個版本的 mail modules,不知道是我寫錯還是怎樣,就是寄不出來…

第一個版本是 Mail::Send

#!/usr/bin/perl

use Mail::Send;
use strict;

sub mail2blogger
{
  my ($subj, @body) = @_;

  my $msg = new Mail::Send;

  $msg->to('gslin@gslin.org');
  $msg->subj($subj);
  $msg->set('From', 'gslin@gslin.org');

  my $fh = $msg->open();
  print($fh join("\n", @body));
  $msg->close();
}

sub main
{
  &mail2blogger('test', '', 'test');
}

第二個版本是 Mail::Sendmail

#!/usr/bin/perl

use Encode;
use Mail::Sendmail;
use strict;

sub big2utf
{
  my $msg = shift;

  return (Encode::encode('utf8', Encode::decode('big5', $msg)));
}

sub mail2blogger
{
  my ($subj, @body) = @_;

  my %msg;

  $msg{'Server'} = '127.0.0.1';
  $msg{'From'} = 'gslin@gslin.org';
  $msg{'To'} = 'gslin@ccca.nctu.edu.tw';
  $msg{'Subject'} = $subj;
  $msg{'Message'} = join("\n", map { big2utf($_) } @body);

  sendmail(%msg) or die $Mail::Sendmail::error;
}

sub main
{
  &mail2blogger('test', 'test', '');
}

第三個版本是 Net::SMTP

#!/usr/bin/perl

use Net::SMTP;
use strict;

sub mail2blogger
{
  my @body = shift;

  my $smtp = Net::SMTP->new('127.0.0.1', Hello => 'localhost');

  $smtp->mail('gslin');
  $smtp->to('gslin@gslin.org');
  $smtp->data();

  foreach my $line (@body)
  {
    $smtp->datasend($line . "\n");
  }

  $smtp->quit();
}

sub main
{
  &mail2blogger("Subject: test", "", "test");
}

有誰看得出問題來嗎,不然我就要用 system() 了 :~

2 thoughts on “Perl 的 mail modules”

  1. 先試試第一個:

    #!/usr/bin/perl

    use Mail::Send;
    use strict;

    mail2blogger(‘test’, ”, ‘test’);

    sub mail2blogger
    {
    my ($subj, @body) = @_;

    my $msg = new Mail::Send;

    $msg->to(‘gslin@gslin.org’);
    $msg->subject($subj);
    $msg->set(‘From’, ‘gslin@gslin.org’);

    my $fh = $msg->open();
    print($fh join(“\n\”, @body)) or die $!;
    $fh->close;
    }

    __END__

  2. 第二題也許可以這麼寫:

    #!/usr/bin/perl

    use Encode;
    use Mail::Sendmail;
    use strict;

    mail2blogger(‘test’, ‘test’, ”);

    sub mail2blogger {
    my ($subj, @body) = @_;
    my %msg = (
    ‘Server’ => ‘127.0.0.1’,
    ‘From’ => ‘gslin@gslin.org’,
    ‘To’ => ‘gslin@gslin.org’,
    ‘Subject’ => $subj,
    ‘Message’ => join(“\n\”,
    map { Encode::encode(‘utf8’, Encode::decode(‘big5’, $_)) } @body)
    );
    sendmail(%msg) or die $Mail::Sendmail::error;
    print $Mail::Sendmail::log;
    }

Comments are closed.