5

I'm looking for a simple (OO?) approach to email creation and sending.

Something like

$e = Email->new(to => "test <test@test.com>", from => "from <from@from.com>");
$e->plain_text($plain_version);
$e->html($html_version);
$e->attach_file($some_file_object);

I've found Email::MIME::CreateHTML, which looks great in almost every way, except that it does not seem to support file attachments.

Also, I'm considering writing these emails to a database and having a cronjob send them at a later date. This means that I would need a $e->as_text() sub to return the entire email, including attachments, as raw text which I could stuff into the db. And so I would then need a way of sending the raw emails - what would be a good way of achieving this?

Many thanks

brian d foy
  • 129,424
  • 31
  • 207
  • 592
aidan
  • 9,310
  • 8
  • 68
  • 82

3 Answers3

4

You have to read the documentation more carefully, then two of your three questions would be moot.

From the synopsis of Email::MIME::CreateHTML:

my $email = Email::MIME->create_html(

You obviously get an Email::MIME object. See methods parts_set and parts_set for so called attachments.

Email::MIME is a subclass of Email::Simple. See method as_string for serialising the object to text.

See Email::Sender for sending mail.

daxim
  • 39,270
  • 4
  • 65
  • 132
2

You might check out perl MIME::Lite.

You can get the message as a string to save into a database:

### Get entire message as a string:
$str = $msg->as_string;
WhirlWind
  • 13,974
  • 3
  • 42
  • 42
1

Email::Stuff is a nice wrapper for Email::MIME. You don't need to care about the MIME structure of the mail, the module does it for you.

Email::Stuff->from     ('cpan@ali.as'                      )
            ->to       ('santa@northpole.org'              )
            ->bcc      ('bunbun@sluggy.com'                )
            ->text_body($body                              )
            ->attach   (io('dead_bunbun_faked.gif')->all,
                        filename => 'dead_bunbun_proof.gif')
            ->send;

It also has as_string.

Thomas Kappler
  • 3,795
  • 1
  • 22
  • 21