Thursday, April 2, 2015

some perl scripts

1. get a unique list
my @list = qw/3 3 2 1 2 3 4 0/; my %seen = (); my @uniq = grep { ! $seen{$_} ++ } @list; print @uniq;


2. send an email
my $subject = "test";
my $text = "test";
&send_mail($subject, $text);

sub send_mail {
  my $subject = $_[0];
  my $text = $_[1];

  my $mailhost = "168.0.1.1"; # the smtp host
  my $mailfrom = 'alexu@aplusflash.com'; # your email address

  my @mailto = ('<test@example.com', '<test1@example.com>'); # the recipient list
  #my @mailcc = ('<test@example.com>', '<test1@example.com>'); # the recipient list
  #my @mailbcc = ('<test@example.com>', '<test1@example.com>'); # the recipient list

  #my @mailtoaddr=(@mailto, @mailcc, @mailbcc);
  my @mailtoaddr=(@mailto);

  my $mailtolist = join(",", @mailto);
  #my $mailcclist = join(",", @mailcc);
  #my $mailbcclist = join(",", @mailbcc);

  my $smtp = Net::SMTP_auth->new($mailhost, Hello => 'test', Timeout => 120, Debug => 1);

  # anth login, type your user name and password here
  $smtp->auth('LOGIN', 'name','password');

  # Send the From and Recipient for the mail servers that require it
  foreach my $mailaddr (@mailtoaddr) {
 
    $smtp->mail($mailfrom);
$smtp->to($mailaddr);

    # Start the mail
    $smtp->data();

    # Send the header
    $smtp->datasend("From: $mailfrom\n");
    $smtp->datasend("To: $mailtolist\n");
    #$smtp->datasend("Cc: $mailcclist\n");
    #$smtp->datasend("Bcc: $mailbcclist\n");
    $smtp->datasend("Subject: $subject\n");
$smtp->datasend("Mime-Version: 1.0\n");
$smtp->datasend("Content-Type: text/html; charset=gb2312\n");
$smtp->datasend("Content-Transfer-Encoding: 8bit\n");
    $smtp->datasend("\n");

    # Send the message
    $smtp->datasend("$text\n\n");

    # Send the termination string
    $smtp->dataend();
  }

  $smtp->quit;
}

No comments:

Post a Comment