perl发送邮件

方法一:

#!/usr/bin/perl -w

use Net::SMTP;
use MIME::Lite;
use MIME::Base64;
use Authen::SASL;

my $from = '[email protected]';
my $passwd = '*********;
my $to = '[email protected]';
my $messages = "Hello Rainbow!";

my $msg = MIME::Lite->new(
    From     =>  $from,
    To       =>  $to,
#   Cc       =>  '[email protected]',
    Subject  =>  'Hello Rainbow!',
    Type     =>  'multipart/mixed',
    Data     =>  $messages,
);

$msg->attach(
    Type     => 'AUTO',      # the attachment mime type
    Path     => $dir, # local address of the attachment
);

MIME::Lite->send('smtp','smtp.domain.cn',  # domain为域名 如:gmail
    Debug   =>'1',
    AuthUser=>$from,
    AuthPass=>$passwd,
);
$msg->send;

方法二:

#!usr/bin/perl

use Net::SMTP;

$server = 'smtp.domain.com';  # domain为域名 如:gmail
$fromaddress = '[email protected]';
$toaddress = '[email protected]';
$passwd = '********';

$subject = "Test Example \n\n";

$message = "This is a test \n\n";

$smtp = Net::SMTP->new($server);
$smtp->auth($fromaddress,$passwd) || print "Auth Error!\n";


$smtp->mail($fromaddress);
$smtp->to($toaddress);
$smtp->data();
$smtp->datasend("Subject: $subject");
$smtp->datasend($message);
$smtp->dataend();

$smtp->quit();

说明:经测试方法而可以向外网发送邮件,而法一不行。

其实更简单的方法是使用linux shell来发送和接收邮件,

详见 1 mutt邮件客户端

        2 用linux脚本自动发送和收取邮件

你可能感兴趣的:(perl)