使用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');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章