天天看點

perl發郵件代碼(備份)

 儲存一下,在你的代碼中require它即可

=head  

need install module Net::SMTP_auth,  

you can type the following command at  

your shell prompt  

cpan -i Net::SMTP_auth  

=cut  

use Net::SMTP;  

use Fcntl qw/:DEFAULT :flock/;  

sub sendmail {  

    my $msg = shift;  

    my $smtp = Net::SMTP->new(  

                              Host    => 'smtp.163.com',  

                              Timeout => 120,  

                              Debug   => 1  

                             );  

    if (defined $smtp) {  

        $smtp->auth("xxx","xxx");  

        $smtp->mail("xxx\@163.com");  

        $smtp->to("xxx\@126.com");  

        $smtp->data();  

        $smtp->datasend("From: Server Monitor <xxx\@163.com>\n");  

        $smtp->datasend("To: SA team <sa\@abc.com>\n");  

        $smtp->datasend("Subject: Server Warnings\n");  

        $smtp->datasend("\n");  

        $smtp->datasend("$msg\n");  

        $smtp->datasend();  

        $smtp->quit;  

    }  

}  

sub printlog {  

    my $str = shift;  

    open LOG,'>>','log.txt' or die "$!\n";  

    flock LOG,LOCK_EX;  

    print LOG "$str\n";  

    flock LOG,LOCK_UN;  

    close LOG;  

sub daemon { 

my $pid = fork(); 

die "can't fork progress: $!\n" unless defined $pid; 

exit 0 if $pid; 

setsid() or die "can't create a new session: $!\n"; 

open STDIN,'<','/dev/null'; 

open STDOUT,'>','/dev/null'; 

open STDERR,'>&',STDOUT; 

chdir "/"; 

umask 022; 

$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin'; 

return $$; 

}

1;  

 本文轉自dongfang_09859 51CTO部落格,原文連結:http://blog.51cto.com/hellosa/669622,如需轉載請自行聯系原作者