Mojar::Message::Smtp - Lightweight email sender.
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;
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.
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;
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.
connect
disconnect
Timeout
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;
A Mojo::Log compatible logger, eg Mojar::Log.
$email->log($logger); $email->log->debug('Making progress');
Whether to show debug information. Defaults to false.
$email->debug(1)->send;
Whether to use SSL. Defaults to false.
$email->ssl(1); say $email->ssl ? 'secure' : 'insecure';
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;
The domain the client is connecting from.
$email->domain('my.domain');
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 )");
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 )");
The username for authentication; only effective if connect is called explicitly.
$email->user('hax3r')->connect->send->disconnect;
The password for authentication; only effective if user is set.
user
$email->user('hax3r')->secret('s3crt')->connect->send->disconnect;
The sender address.
The recipient address(es).
$email->To('single@example.com'); $email->To(['first@example.com', 'second@example.com']);
The carbon copy recipient address(es); similar to To.
To
The blind carbon copy recipient address(es); similar to To.
The text body of the email.
$email->body('Some text'); $email->body('<h1>Hi</h1>')->Type('text/html');
$email = Mojar::Message::Smtp->new(domain => ..., ...);
Constructor for the email, accepting all attributes listed above.
$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.
$email->send;
Sends an SMTP message. Can be passed call-specific overriding parameters.
$email->send(host => q{mailserver2});
See the source code for other methods you can override when subclassing this.
See Mojar.
MIME::Entity, Mail::Internet.
To install Mojar::Message, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojar::Message
CPAN shell
perl -MCPAN -e shell install Mojar::Message
For more information on module installation, please visit the detailed CPAN module installation guide.