使用perl的Net::SMTP::SSL模块发送smtps带附件的邮件

use Net::SMTP::SSL;  
use MIME::Lite;
 
my  $mail_from = '[email protected]';  
my  $mail_to = '[email protected]';   
my  $mail_subject = "Perl smtps testing1\n";  
my $username = "user1";
my $password = "123";
 
sub SendAttachMail  
{  
    my $mail_content = shift;  
    my $mail_attach = shift;
 
    my $smtp = Net::SMTP::SSL->new(  
                        'mail.smtpstest.com',  
                        Hello=>'mail.smtpstest.com',  
                        Port=>465,  
                        LocalPort=>0,  
                        Debug=>1);  
 
    die("smtp undefined: $@") if !defined $smtp;  
 
    my $auth_return = $smtp->auth($username,$password);  
    die("auth error: $@") if !defined $auth_return;  
    
    my @tmp = split/\\/,$mail_attach;
    my $attach_name = pop(@tmp);
    my $msg=MIME::Lite->new(
                    From=>$mail_from,
                    To=>$mail_to,
                    Subject=>$mail_subject,
                    Type=>'TEXT',
                    Data=>$mail_content,);

    $msg->attach(
            Type=>'AUTO',
            Path=>$mail_attach,
            Filename=>$attach_name,);

    my $content_string=$msg->as_string() or die "$!";
    
 
    $smtp->mail($mail_from);  
    $smtp->to($mail_to);  
    #$smtp->cc($mail_cc);  
    #$smtp->bcc($mail_bcc);  
    $smtp->data();   
    $smtp->datasend($content_string);
    $smtp->dataend();  
    $smtp->quit;  
}  
SendAttachMail("附件是一个virus文件",'/test/test.txt');

你可能感兴趣的:(Perl,Linux,脚本)