The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojar::Message::Smtp - Lightweight email sender.

SYNOPSIS

  use Mojar::Message::Smtp;
  my $email = Mojar::Message::Smtp->new(
    domain => 'example.com',
    log => $app_log
  );

  $email->To('myteam@example.com')
      ->From('manager@example.com')
      ->Subject(q{Team, is your inbox full?})
      ->body(q{Otherwise, consider this JPG your reward.})
      ->attach({Path => '/tmp/random.jpg',
          Encoding => 'base64',
          Type => 'image/jpeg'})
      ->send;
  $email->To('otherteam@example.com')->send;

DESCRIPTION

USAGE

Sends an email, possibly with attachments, via an SMTP mailserver.

There are two distinct ways of using this module. The common simple way is to just let connections be handled implicitly. The second way is to connect and disconnect explicitly.

Implicit Connections

  Mojar::Message::Smtp->new(domain => 'example.com', log => ...)
      ->From('me@example.com')
      ->To(['someone@example.com', 'shadow@example.com'])
      ->Subject('Using an open mailserver without SSL')
      ->body('This is a common situation and the easiest to navigate.')
      ->send;

Explicit Connections

If you want to use SSL you will need to have Net::SMTP::SSL installed and you then call connect and disconnect explicitly. Explicit connections also suit people who want to use the Timeout attribute or want to hold a connection open across multiple emails.

  Mojar::Message::Smtp->new(ssl => 1, domain => 'example.com', log => ...)
      ->host('secure.mail.server')
      ->From('me@example.com')
      ->To('someone@example.com')
      ->Subject('Using a secure mailserver over SSL')
      ->body('This is growing in popularity and is not difficult at all.')
      ->timeout(300)
      ->connect
      ->send
      ->To('someone.else@example.com')
      ->Subject('Balancing bananas on your head')
      ->send
      ->disconnect;

ATTRIBUTES

log

A Mojo::Log compatible logger, eg Mojar::Log.

  $email->log($logger);
  $email->log->debug('Making progress');
debug

Whether to show debug information. Defaults to false.

  $email->debug(1)->send;
ssl

Whether to use SSL. Defaults to false.

  $email->ssl(1);
  say $email->ssl ? 'secure' : 'insecure';
timeout

The SMTP connection timeout in seconds. This attribute only takes effect if you call connect explicitly. Defaults to 120 seconds.

  $email->timeout(120)->connect;
  $current_timeout = $email->timeout;
domain

The domain the client is connecting from.

  $email->domain('my.domain');
host

The SMTP host address to connect to. Defaults to '127.0.0.1' (localhost).

  $email->host('my.mail.server');
  $email->host('192.168.0.2');
  $email->log->debug("Using host $(\ $email->host )");
port

The SMTP port to connect to on the host. Defaults to 25 for standard and 465 for SSL.

  $email->port(3025);
  $email->log->debug("Using port $(\ $email->port )");
user

The username for authentication; only effective if connect is called explicitly.

  $email->user('hax3r')->connect->send->disconnect;
secret

The password for authentication; only effective if user is set.

  $email->user('hax3r')->secret('s3crt')->connect->send->disconnect;
From

The sender address.

To

The recipient address(es).

  $email->To('single@example.com');
  $email->To(['first@example.com', 'second@example.com']);
Cc

The carbon copy recipient address(es); similar to To.

Bcc

The blind carbon copy recipient address(es); similar to To.

body

The text body of the email.

  $email->body('Some text');
  $email->body('<h1>Hi</h1>')->Type('text/html');

METHODS

new
  $email = Mojar::Message::Smtp->new(domain => ..., ...);

Constructor for the email, accepting all attributes listed above.

attach
  $email->attach({
    Path => 'vi/NpBT78YQOms/maxresdefault.jpg',
    Type => 'image/jpeg',
    Encoding => 'base64'
  });

Configures an attachment. This can be chained:

  $email->attach({Path => 'a.jpg'})->attach({Path => 'b.jpg'});

See MIME::Entity for notes on the available parameters.

Note that attaching happens at send-time, so if the file might change before then, you should consider copying the file to a temporary static location and attaching from there.

send
  $email->send;

Sends an SMTP message. Can be passed call-specific overriding parameters.

  $email->send(host => q{mailserver2});
other methods

See the source code for other methods you can override when subclassing this.

SUPPORT

See Mojar.

SEE ALSO

MIME::Entity, Mail::Internet.