信封头部

        ### Create a new multipart message:
        $msg = MIME::Lite->new(
            From    => 'me@myhost.com',
            To      => 'you@yourhost.com',
            Cc      => 'some@other.com, some@more.com',
            Subject => 'A message with 2 parts...',
            Type    => 'multipart/mixed'
        );
  • 这里注意收件人可以多个,并用$string中间用逗号分开, 但我自己的GetPass.pm中,是这样获取smtp的信息的:
sub getSmtpAuth {
    shift;
    my @param = @_;
    return @smtpAuth{@param};
}

my %smtpAuth = (
    username => 'smtp的账号',
    password => 'smtp的密码',
    from => 'smtp的from, foo@bar.com',
    host => 'smtp的地址: smtp.host.com',
);

异常

    # 和GetPass.pm不兼容,别用
    my ($recipientAddress, $fromAddress) = $password->getInfoRec("address", "from");

如果使用 @getSmtpAuth(“param1”, “param2”, … ) 会出错,因为返回的是数组包含了 $smtpAuth(“param2”) , 这里应该这样用:

    my $recipientAddress = $password->getInfoRec("address");
    my $fromAddress = $password->getInfoRec("from");

多图

注意邮件中的内容,它是先在html写好, 然后在后面附上base64的图片码, 下面是邮件部分显示:

        <IMG SRC="cid:nginxPVHourly.png"><IMG SRC="cid:nginxPVToday.png"><IMG SRC="cid:nginxPVPerServerHourly.png"><IMG SRC="cid:nginxPVPerServerToday.png">


--_----------=_143701578251690
Content-Disposition: inline; filename="nginxPVHourly.png"
Content-Id: <nginxPVHourly.png>
Content-Transfer-Encoding: base64
Content-Type: image/png; name="nginxPVHourly.png"

iVBORw0KGgoAAAANSUhEUgAAA+gAAAH0CAMA ...

多图例子一

这个方法并不好,但容易理解,

    $msg->attach(Type => 'text/html',
        Data => qq{ <BODY BGCOLOR=#FFFFFF>
        <H2>Hi</H2>
        <P ALIGN="left">
        这里应该填写信息一
        </P>
        <P ALIGN="middle">
        <IMG SRC="cid:2uni1.jpg">
        </P>
        <P AsIGN="middle">
        <IMG SRC="cid:2uni2.jpg">
        </P>
        </BODY> });
    # Attach the image
    $msg->attach(Type => 'image/jpg',
        Id   => '2uni2.jpg',
        Path => '/home/kk/Downloads/a.jpg');
    $msg->attach(Type => 'image/jpg',
        Id   => '2uni1.jpg',
        Path => '/home/kk/Downloads/a.jpg');

稍微改进一下代码, 多图例子二

# 添加需要显示的图片列表
    # Send HTML document with inline images
    my @PicArray = (
        "nginxPVHourly.png",
        "nginxPVToday.png",
    );
    
    ...

# 写一个图片多图显示的html语句
# 结果可以为 <IMG SRC="cid:2uni1.jpg"><IMG SRC="cid:2uni2.jpg">

    # 
    my @htmlScriptArray = @PicArray;
    map { s/$_/<IMG SRC="cid:$_">/g; $_} @htmlScriptArray;
    my $htmlPicScript = join "", @htmlScriptArray;
    #
    $msg->attach(Type => 'text/html',
        Data => qq{ 
        <BODY>
        $htmlPicScript
        </BODY> });

    ...

# 附上base64码
    # Attach the image
    foreach my $picName ( @PicArray ) {
        $msg->attach(Type => 'image/png',
            Id   => "$picName",
            Path => "/tmp/$picName");
    }