Use the Net::SMTP module to send an email through an ms exchange smtp server

上一篇 / 下一篇  2008-11-12 14:02:02 / 个人分类:Perl

use Net::SMTP;

my $smtpSer = 'mailhost';
my $from ='from@xxx.com';
my $to ='to@xxx.com';
my $subject = 'Test';
my $body = "A simple test message";

$smtp = Net::SMTP->new($smtpSer);

$smtp->mail($ENV{USER});
$smtp->to($to);

$smtp->data();
$smtp->datasend("From: $from\n");
$smtp->datasend("To: $to\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("Body: $body\n");
$smtp->dataend();

$smtp->quit;

##############################################################################

perldoc Net::SMTP for more details

If you need authentication (ie. server does not relay) then substitute Net::SMTP for Net::SMTP_auth. The trick to make this two work together is to create a email message with MIME::Lite (you can add attachments, use html or text) then convert the MIME::Lite object into a string (there is a method to do it within the module) then using the datasend(string) method from Net::SMTP your email will be ready to go.


TAG: Perl Net::SMTP send email

 

评分:0

我来说两句

日历

« 2024-05-02  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 47916
  • 日志数: 80
  • 建立时间: 2008-10-27
  • 更新时间: 2009-07-17

RSS订阅

Open Toolbar