Agent-TCLI-Package-Net

 view release on metacpan or  search on metacpan

lib/Agent/TCLI/Package/Net/SMTP.pm  view on Meta::CPAN


=head1 INTERFACE

This module must be loaded into a Agent::TCLI::Control by an
Agent::TCLI::Transport in order for a user to interface with it.

=cut

use warnings;
use strict;

use Object::InsideOut qw(Agent::TCLI::Package::Base);

use POE;
use POE::Component::Client::SMTP;
use Email::Simple::Creator;
use File::Slurp;
use Data::Dump qw(pp);
use Agent::TCLI::Command;
use Agent::TCLI::Parameter;
use Getopt::Lucid qw(:all);

our $VERSION = '0.030.'.sprintf "%04d", (qw($Id: SMTP.pm 74 2007-06-08 00:42:53Z hacker $))[2];

=head2 ATTRIBUTES

The following attributes are accessible through standard <attribute>
methods unless otherwise noted.

These attrbiutes are generally internal and are probably only useful to
someone trying to enhance the functionality of this Package module.

=over

=cut

=back

=head2 METHODS

Most of these methods are for internal use within the TCLI system and may
be of interest only to developers trying to enhance TCLI.

=over

=item new ( hash of attributes )

Usually the only attributes that are useful on creation are the
verbose and do_verbose attrbiutes that are inherited from Agent::TCLI::Base.

=cut

sub _preinit :PreInit {
	my ($self,$args) = @_;

	$args->{'name'} = 'tcli_smtp';

	$args->{'session'} = POE::Session->create(
      object_states => [
          $self => [qw(
          	_start
          	_stop
          	_shutdown
          	_default
          	_child

			establish_context
			send
			SendMailSuccess
			SendMailFailure
			settings
			show
			)],
      ],
	);

}

sub _init :Init {
	my $self = shift;

	$self->LoadYaml(<<'...');
---
Agent::TCLI::Parameter:
  name: from
  constraints:
    - EMAIL
  default: tcli_agent@example.com
  help: The sender's email address
  manual: >
    This holds the sender's email address.
  type: Param
---
Agent::TCLI::Parameter:
  name: to
  constraints:
    - EMAIL_LOOSE
  help: The recipiants email address
  manual: >
    This holds a list of recipients. Note that To/CC/BCC fields are separated
    in your email body. From the SMTP server's point of view
    (and from this component's too) there is no difference as
    who is To, who CC and who BCC.
    The bottom line is: be careful how you construct your email message.
  type: Param
---
Agent::TCLI::Parameter:
  name: body
  help: the body of the email
  manual: >
    The textual part of hte email message, or the MIME content if so inclined.
  type: Param
---
Agent::TCLI::Parameter:
  name: subject
  aliases: subj
  help: the subject line
  manual: >
    The subject line for the message.
  type: Param
---

lib/Agent/TCLI/Package/Net/SMTP.pm  view on Meta::CPAN

  help: send a mail message
  manual: >
    Send mail to a SMTP server when the body is from a text file on the local
    host system.
  parameters:
    to:
    from:
    msgfile:
    timeout:
    subject:
    server:
    port:
  required:
    to:
    msgfile:
  topic: net
  usage: smtp sendmsg to=joe@example.com msgfile="/tmp/hello.msg"
---
Agent::TCLI::Command:
  name: set
  call_style: session
  command: tcli_smtp
  contexts:
    smtp: set
  handler: settings
  help: set defaults for smtp messages
  parameters:
    to:
    from:
    body:
    timeout:
    subject:
    server:
    port:
  topic: network
  usage: smtp set server=mx.example.com
---
Agent::TCLI::Command:
  name: show
  call_style: session
  command: tcli_smtp
  contexts:
    smtp: show
  handler: show
  help: show current settings
  parameters:
    to:
    from:
    body:
    timeout:
    subject:
    server:
    port:
  topic: network
  usage: smtp show timeout
...

}


sub _start {
	my ($kernel,  $self,  $session) =
      @_[KERNEL, OBJECT,   SESSION];
	$self->Verbose("_start: tcli http starting");

	# are we up before OIO has finished initializing object?
	if (!defined( $self->name ))
	{
		$kernel->yield('_start');
		return;
	}

	# There is only one command object per TCLI
    $kernel->alias_set($self->name);


	$self->Verbose(" Dump ".$self->dump(1),3 );

}

sub _stop {
    my ($kernel,  $self,) =
      @_[KERNEL, OBJECT,];
	$self->Verbose("_stop: ".$self->name." stopping",2);
}

sub SendMailSuccess {
	my ($kernel,  $self,  $request) =
      @_[KERNEL, OBJECT,      ARG0];

	$request->Respond($kernel,"OK", 200);
	return
}

sub SendMailFailure {
	my ($kernel,  $self,  $request, $fail) =
      @_[KERNEL, OBJECT,      ARG0,  ARG1];

    $request->Respond($kernel, "Failed: ".pp($fail),400);
	$self->Verbose( "SendMailFailed: ".pp($fail));
}

sub send {
    my ($kernel,  $self, $session, $request, ) =
      @_[KERNEL, OBJECT,  SESSION,     ARG0, ];

	my $txt = '';
	my $param;
	my $command = $request->command->[0];
	my $cmd = $self->commands->{$command};

	return unless ( $param = $cmd->Validate($kernel, $request, $self) );

	$self->Verbose("send: to(".$param->{'to'}.") ",2);
	$self->Verbose("send: param dump ",,$param);

	# Build email message from parts.
	my ($email, $body);

	if ($command eq 'send')
	{
		$email = Email::Simple->create(
			header => [
				From    => $param->{'from'},
				To      => $param->{'to'},
				Subject => $param->{'subject'},
			],
			body => $param->{'body'},
		);



( run in 0.545 second using v1.01-cache-2.11-cpan-39bf76dae61 )