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

如何将Email::Mime与sendmail一起使用

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

    我正在尝试使用脚本发送HTML电子邮件。我将不得不使用本机unix和 Email::Mime 因为这是我发现的唯一安装在盒子里的东西,所以我一直坚持使用。我正在创建一个 电子邮件::Mime 并将其发送到sendmail。

    但我不断地犯错误: 在标头中找不到收件人地址 我见过其他RUBY脚本使用 sendmail 这对这个盒子有效。

    有人能帮我解决我在下面的代码片段中可能犯的错误吗?

    sub send_mail(){
    
    use MIME::QuotedPrint;
    use HTML::Entities;
    use IO::All;
    use Email::MIME;
    
    # multipart message
    my @parts = (
        Email::MIME->create(
            attributes => {
                content_type => "text/html",
                disposition  => "attachment",
                encoding     => "quoted-printable",
                charset      => "US-ASCII",
            },
            body_str => "Hello there!",
        ),
    );
    
    my $email = Email::MIME->create(
        header_str => [
            To => 'abc@xxx.com',
            From => 'abc@xxx.com',
            Subject => "Test Email",
        ],
        parts      => [@parts],
    );
    
    # die $email->as_string;
    # die YAML::XS::Dump(\%mail);
    
    open(MAIL, "|/usr/sbin/sendmail -t");
    
    print MAIL $email;
    
    close (MAIL);
    
    }
    

    提前谢谢。

    2 回复  |  直到 7 年前
        1
  •  2
  •   ysth    7 年前
    print MAIL $email;
    

    print MAIL $email->as_string;
    
        2
  •  0
  •   Thomas Lang    7 年前

    $transport = EMail::Sender::Transport::SMTP::Persistent->new({
      # host, port, ssl, etc
    })
    

    此外,我认为您实际上不需要将内容创建为自己的MIME内容。 我在自己的作品中使用了类似的东西:

    $email = Email::MIME->Create(
      header_str => [ ... ],
      body_str => $message,
      attributes => {
        charset => 'UTF-8',
        encoding => 'base64',
        content_type => 'text/html',
      }
    )
    

    sendmail($email, { transport => $transport }) ,您需要通过关闭会话 $transport->disconnect .