首页 | 新闻 | 交流 | 问吧 | 文档 | 手册 | 下载 | 博客

用perl发送邮件

作者:  时间: 2010-10-13

#!/usr/bin/perl


use MIME::Lite;


push(@body, "Failed: Server = $IP Stratum = $stratum\n");

# Set up email
        $to = "jamesg\@sz.webex.com";
        $from = "ntpchk\@sz.webex.com";
        $subject = "NTP server problem";
        $message = join("\n",@body);


        # Send email
        email($to, $from, $subject, $message);

        # Email function
        sub email
        {
        # Get incoming parameters
         local ($to, $from, $subject, $message) = @_;

        # Create a new message
        $msg = MIME::Lite->new(
        From => $from,
        To => $to,
        Subject => $subject,
        Data => $message
        );

        # Send the email
        MIME::Lite->send('smtp', '10.224.106.10', Timeout => 60);
        $msg->send();
        }


MIME::Lite只能发送纯文本的邮件,如果要发送带有HTML格式的邮件,就必须用到MIME::Lite::HTML模块啦。

#!/usr/bin/perl


use MIME::Lite::HTML;

        $to = "jamesg\@sz.webex.com";
        $from = "ntpchk\@sz.webex.com";
        $subject = "NTP server problem";

        # Send email
        email($to, $from, $subject, $message);

        # Email function
        sub email
        {
        # Get incoming parameters
         local ($to, $from, $subject, $message) = @_;

        # Create a new message
        $msg = MIME::Lite::HTML->new(
        From => $from,
        To => $to,
        Subject => $subject,
        );

        my $MIMEmail = $msg->parse("file://tmp/test.html");
        # Send the email
        $MIMEmail->send('smtp', '10.224.106.10', Timeout => 60);
        }


其实用bash command更简单

cat /tmp/sysinfochk.log |/usr/bin/formail -I "MIMIE-Version:1.0" -I "Content-type:text/html" -I "Subject:sjimage:SDE_DB:SYSINFO check log"|/usr/sbin/sendmail -oi "jamesg@sz.webex.com"