The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Email::Stuffer - A more casual approach to creating and sending Email:: emails


version 0.020


  # Prepare the message
  my $body = <<'AMBUSH_READY';
  Dear Santa

  I have killed Bun Bun.

  Yes, I know what you are thinking... but it was actually a total accident.

  I was in a crowded line at a BayWatch signing, and I tripped, and stood on
  his head.

  I know. Oops! :/

  So anyways, I am willing to sell you the body for $1 million dollars.

  Be near the pinhole to the Dimension of Pain at midnight.



  # Create and send the email in one shot
  Email::Stuffer->from     (''             )
                ->to       (''     )
                ->bcc      (''       )
                ->text_body($body                     )
                ->attach_file('dead_bunbun_faked.gif' )


The basics should all work, but this module is still subject to name and/or API changes

Email::Stuffer, as its name suggests, is a fairly casual module used to stuff things into an email and send them. It is a high-level module designed for ease of use when doing a very specific common task, but implemented on top of the light and tolerable Email:: modules.

Email::Stuffer is typically used to build emails and send them in a single statement, as seen in the synopsis. And it is certain only for use when creating and sending emails. As such, it contains no email parsing capability, and little to no modification support.

To re-iterate, this is very much a module for those "slap it together and fire it off" situations, but that still has enough grunt behind the scenes to do things properly.

Default Transport

Although it cannot be relied upon to work, the default behaviour is to use sendmail to send mail, if you don't provide the mail send channel with either the transport method, or as an argument to send.

(Actually, the choice of default is delegated to Email::Sender::Simple, which makes its own choices. But usually, it uses sendmail.)

Why use this?

Why not just use Email::Simple or Email::MIME? After all, this just adds another layer of stuff around those. Wouldn't using them directly be better?

Certainly, if you know EXACTLY what you are doing. The docs are clear enough, but you really do need to have an understanding of the structure of MIME emails. This structure is going to be different depending on whether you have text body, HTML, both, with or without an attachment etc.

Then there's brevity... compare the following roughly equivalent code.

First, the Email::Stuffer way.

  Email::Stuffer->to('Simon Cozens<>')
                ->text_body("You've been good this year. No coal for you.")

And now doing it directly with a knowledge of what your attachment is, and what the correct MIME structure is.

  use Email::MIME;
  use Email::Sender::Simple;
  use IO::All;

      header => [
          To => '',
          From => '',
      parts => [
            body => "You've been a good boy this year. No coal for you."
            body => io('choochoo.gif'),
            attributes => {
                filename => 'choochoo.gif',
                content_type => 'image/gif',

Again, if you know MIME well, and have the patience to manually code up the Email::MIME structure, go do that, if you really want to.

Email::Stuffer as the name suggests, solves one case and one case only: generate some stuff, and email it to somewhere, as conveniently as possible. DWIM, but do it as thinly as possible and use the solid Email:: modules underneath.


This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.


As you can see from the synopsis, all methods that modify the Email::Stuffer object returns the object, and thus most normal calls are chainable.

However, please note that send, and the group of methods that do not change the Email::Stuffer object do not return the object, and thus are not chainable.


Creates a new, empty, Email::Stuffer object.

You can pass a hashref of properties to set, including:

  • to

  • from

  • cc

  • bcc

  • reply_to

  • subject

  • text_body

  • html_body

  • transport

The to, cc, bcc, and reply_to headers properties may be provided as array references. The array's contents will be used as the list of arguments to the setter.


Returns, as a list, all of the headers currently set for the Email For backwards compatibility, this method can also be called as B[headers].


Returns, as a list, the Email::MIME parts for the Email

  $stuffer->header($header_name = $value)

This method sets a named header in the email. Multiple calls with the same $header_name will overwrite previous calls $value.



This method sets the To header in the email.



This method sets the From header in the email.



This method sets the Reply-To header in the email.



This method sets the Cc header in the email.



This method sets the Bcc header in the email.



This method sets the Subject header in the email.


  $stuffer->text_body($body, %attributes);

Sets the text body of the email. Appropriate headers are set for you. You may override MIME attributes as needed. See the attributes parameter to "create" in Email::MIME for the headers you can set.

If $body is undefined, this method will do nothing.

Prior to Email::Stuffer version 0.015 text body was marked as flowed, which broke all pre-formated body text. Empty space at the beggining of the line was dropped and every new line character could be changed to one space (and vice versa). Version 0.015 (and later) does not set flowed format automatically anymore and so text body is really plain text. If you want to use old behavior of "advanced" flowed formatting, set flowed format manually by: text_body($body, format => 'flowed').


  $stuffer->html_body($body, %attributes);

Sets the HTML body of the email. Appropriate headers are set for you. You may override MIME attributes as needed. See the attributes parameter to "create" in Email::MIME for the headers you can set.

If $body is undefined, this method will do nothing.


  $stuffer->attach($contents, %attributes)

Adds an attachment to the email. The first argument is the file contents followed by (as for text_body and html_body) the list of headers to use. Email::Stuffer will try to guess the headers correctly, but you may wish to provide them anyway to be sure. Encoding is Base64 by default. See the attributes parameter to "create" in Email::MIME for the headers you can set.


  $stuffer->attach_file($file, %attributes)

Attachs a file that already exists on the filesystem to the email. attach_file will attempt to auto-detect the MIME type, and use the file's current name when attaching. See the attributes parameter to "create" in Email::MIME for the headers you can set.

$file can be a filename or an IO::All::File object.


  $stuffer->transport( $moniker, @options )


  $stuffer->transport( $transport_obj )

The transport method specifies the Email::Sender transport that you want to use to send the email, and any options that need to be used to instantiate the transport. $moniker is used as the transport name; if it starts with an equals sign (=) then the text after the sign is used as the class. Otherwise, the text is prepended by Email::Sender::Transport::.

Alternatively, you can pass a complete transport object (which must be an Email::Sender::Transport object) and it will be used as is.


  my $email_mime = $stuffer->email;

This method creates and returns the full Email::MIME object for the email.


  my $email_document = $stuffer->as_string;

Returns the string form of the email. Identical to (and uses behind the scenes) Email::MIME->as_string.




  $stuffer->send({ to => [ $to_1, $to_2 ], from => $sender });

Sends the email via Email::Sender::Simple. Envelope information can be specified in a hash reference.

On failure, returns false.




  $stuffer->send_or_die({ to => [ $to_1, $to_2 ], from => $sender });

Sends the email via Email::Sender::Simple. Envelope information can be specified in a hash reference.

On failure, throws an exception.


Here is another example (maybe plural later) of how you can use Email::Stuffer's brevity to your advantage.

Custom Alerts

  package SMS::Alert;
  use base 'Email::Stuffer';

  sub new {
           # Of course, we could have pulled these from
           # $MyConfig->{support_tech} or something similar.
           ->transport('SMTP', { host => '' });

  package My::Code;

  unless ( $Server->restart ) {
          # Notify the admin on call that a server went down and failed
          # to restart.
          SMS::Alert->subject("Server $Server failed to restart cleanly")


  • Fix a number of bugs still likely to exist

  • Write more tests.

  • Add any additional small bit of automation that isn't too expensive


Email::MIME, Email::Sender,


  • Adam Kennedy <>

  • Ricardo SIGNES <>


  • Aaron W. Swenson <>

  • adam <adam@88f4d9cd-8a04-0410-9d60-8f63309c3137>

  • <>

  • <>

  • Alastair Douglas <>

  • Aristotle Pagaltzis <>

  • Chase Whitener <>

  • CosmicNet <>

  • Dan Book <>

  • fREW Schmidt <>

  • John Napiorkowski <>

  • Josh Stompro <>

  • Kevin Tew <>

  • Kieren Diment <kd@fenchurch.local>

  • Kris Matthews <>

  • Lee Johnson <>

  • Manni Heumann <>

  • Pali <>

  • Ricardo Signes <>

  • Ross Attrill <>

  • Russell Jenkins <>

  • Shawn Sorichetti <>

  • Steve Dondley <>

  • tokuhirom <>


This software is copyright (c) 2004 by Adam Kennedy and Ricardo SIGNES.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.