代码之家  ›  专栏  ›  技术社区  ›  justrajdeep

如何使用Perl电子邮件::Mime内联图像?

  •  0
  • justrajdeep  · 技术社区  · 7 年前

    我正在尝试发送带有内联图像的HTML电子邮件。我将不得不使用本机unix和 Email::Mime 电子邮件::Mime sendmail . 我正在使用 cid 为了在图像中添加线条,但出于某种原因,我一直将图像作为附件。

    有人能帮我吗,下面是代码片段。

    sub send_mail(){
    
    use MIME::QuotedPrint;
    use HTML::Entities;
    use IO::All;
    use Email::MIME;
    
    $boundary = "====" . time() . "====";
    
    $text = "HTML mail demo\n\n"
          . "This is the message text\n"
          . "Voilà du texte qui sera encodé\n";
    
    $plain = encode_qp $text;
    
    $html = encode_entities($text);
    $html =~ s/\n\n/\n\n<p>/g;
    $html =~ s/\n/<br>\n/g;
    $html = "<p><strong>" . $html . "</strong></p>";
    $html .= '<p><img src="cid:123.png" class = "mail" alt="img-mail" /></p>';
    
    # multipart message
        my @parts = (
            Email::MIME->create(
                attributes => {
                    content_type => "text/html",
                    encoding     => "quoted-printable",
                    charset      => "US-ASCII",
                },
                body_str => "<html> $html </html>",
            ),
            Email::MIME->create(
                attributes => {
                    content_type => "image/png",
                    name => "pie.png",
                    disposition  => "Inline",
                    charset      => "US-ASCII",
                    encoding     => "base64",
                    filename => "pie.png",
                    "Content-ID" => "<123.png>",
                    path => "/local_vol1_nobackup/user/ramondal/gfxip_gfx10p2_main_tree03/src/verif/ge/tb",
                },
                body => io("pie.png")->binary->all,
            ),
        );
    
         my $email = Email::MIME->create(
             header_str => [
                 To => 'abc@xyz.com',
                 Subject => "Test Email",
             ],
             parts      => [@parts],
         );
    
    
        # die $email->as_string;
    
        open(MAIL, "|/usr/sbin/sendmail -t") or die $!;
    
        print MAIL $email->as_string;
    
        close (MAIL);
    
        }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Steffen Ullrich    7 年前

    您的代码有两个问题。

    首先,它应该是一个 Content-Id: <123.png> content-id=<123.png> 的参数 Content-Type 标题。要解决此问题,请不要添加 Content-Id attributes 而是作为 header_str :

    ...
    Email::MIME->create(
        header_str => [
            "Content-ID" => "123.png",
        ],
        attributes => {
            content_type => "image/png",
    ...
    

    multipart/mixed 邮件的内容类型。但是图像和HTML是相关的,所以它应该是一个 multipart/related

    ...
    my $email = Email::MIME->create(
        header_str => [
            To => 'abc@xyz.com',
            Subject => "Test Email",
        ],
        attributes => {
            content_type => 'multipart/related'
        },
        parts      => [@parts],
    );
    ...
    
    推荐文章