Search code examples
perlmime-typessendmail

How to use Email::Mime with sendmail


I am trying to send HTML email using a script. I will have to use native unix things and Email::Mime since those are the only thing I found installed in the box i am stuck with. I am creating a Email::Mime message and sending it to sendmail.

But i keep getting Error: No recipient addresses found in header I have seen other RUBY scripts using sendmail so that works for this box.

Can someone help me with what I might be doing wrong in the below snippet?

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 => '[email protected]',
        From => '[email protected]',
        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);

}

Thanks in advance.


Solution

  • print MAIL $email;
    

    should be

    print MAIL $email->as_string;