in reply to Getting Error "Unknown send method" when sending mail using MIME::Lite??

Why are you (trying to) send out an email before constructing it?
MIME::Lite->send('smtp','example.com',Debug=>1) || die $!;
That doesn't seem properly placed.
If you remove that line, and edit $msg->send(); to read
$msg->send('smtp','example.com',Debug=>1) || die $!;
things would go better.

Replies are listed 'Best First'.
Re^2: Getting Error "Unknown send method" when sending mail using MIME::Lite??
by almut (Canon) on Feb 24, 2010 at 09:33 UTC

    According to the docs:

    MIME::Lite->send()

    When used as a classmethod, this can be used to specify a different default mechanism for sending message.

    In other words, what the OP is trying to do is perfectly fine.

      In other words, what the OP is trying to do is perfectly fine.

      The issue is that a function is being called on an object that has not yet been instantiated. Once the ->new function has been called, the object is created, which then allows the send function to be called.
      /\ Sierpinski

        Not sure what you're trying to point out.  Class methods don't need an object instance to be called upon.

        If you look at the implementation of MIME::Lite::send(),

        sub send { my $self = shift; my $meth = shift; if ( ref($self) ) { ### instance method: my ( $method, @args ); if (@_) { ### args; use them just this once $method = 'send_by_' . $meth; @args = @_; } else { ### no args; use defaults $method = "send_by_$Sender"; @args = @{ $SenderArgs{$Sender} || [] }; } $self->verify_data if $AUTO_VERIFY; ### prevents missing pa +rts! Carp::croak "Unknown send method '$meth'" unless $self->can($m +ethod); return $self->$method(@args); } else { ### class method: if (@_) { my @old = ( $Sender, @{ $SenderArgs{$Sender} } ); $Sender = $meth; $SenderArgs{$Sender} = [@_]; ### remaining args return @old; } else { Carp::croak "class method send must have HOW... arguments\ +n"; } } }

        you'll see that in the else branch (### class method), the send method to be set as default is stored in some global variable $Sender together with the arguments in $SenderArgs{$Sender}.

        Those values are then being used when send() is called as an instance method. What makes the program croak is that $self->can($method) is failing, because - apparently - there is no 'send_by_smtp' method in the OP's case.  I can't tell why, however — my version of MIME::Lite does have such a method.