CombatSquirrel has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a tiny program to send my birthday invitations using Mail::Sender. The last half-hour I tried debugging the following code (with the help of the docs), but I always get the error message Can't call method "OpenMultipart" without a package or object reference at btag.pl line 40, <DATA> line 1.
Does anyone have an idea what this means (after all, I am using the $sender variable)?
#!perl use strict; use warnings; use Mail::Sender; use Term::ReadKey; my ($mail_server, $id, $pswd); print "Enter SMTP mail server > "; chomp($mail_server = <>); $mail_server |= 'mail.Squirrel.org'; print "Enter user name > "; chomp($id = <>); print "Enter password > "; ReadMode( "noecho" ); chomp($pswd = <>); ReadMode( "restore" ); system('cls'); my $sender = new Mail::Sender { 'smtp' => $mail_server, 'from' => $id, 'auth' => 'CRAM-MD5', 'authid' => $id, 'authpwd' => $pswd, 'subject' => 'Birthday invitation', 'boundary' => 'A boring Multipart bou +ndary', 'multipart' => 'mixed', 'encoding' => 'Quoted-printable', 'confirm' => 'reading', 'keepconnection' => 'true'}; $Mail::Sender::NO_X_MAILER = "true"; $pswd = 'Thought it, sucker!'; for (<DATA>) { my ($last, $first, $gender, $email) = split; $sender->OpenMultipart({ 'to' => "$first $last <$email>" }); ### li +ne 40 $sender->( { 'Part' => 'multipart/alternative' } ); my $msg = <<END_OF_TEXT; Plain text message with Umlauts(äöü) to [fname]. END_OF_TEXT $msg =~ s/[fname]/$first/g; $sender->Part( { ctype => 'text/plain', encoding => 'iso-8859-1', m +sg => $msg }); $msg = <<END_OF_TEXT; <html> <head><title>B'day invitation</title></head> <body> <h1>Invitation</h1> HTML message with Umlauts(&auml;&ouml;&uuml;) to [fname]. </body> </html> END_OF_TEXT $msg =~ s/[fname]/$first/g; $sender->Part({ ctype => 'text/html', msg => $msg }); $sender->EndPart('multipart/alternative'); $sender->Close(); } $sender->close('all'); __DATA__ Squirrel Combat m Combat@Squirrel.org
Thanks in advance, CombatSquirrel.

Replies are listed 'Best First'.
Re: Problems using Mail::Sender
by tcf22 (Priest) on Aug 22, 2003 at 17:03 UTC
    You need to check to make sure that the object is created and that the message opens successfully. Try this:
    my $sender; unless(ref($sender = new Mail::Sender { 'smtp' => $mail_server, 'from' => $id, 'auth' => 'CRAM-MD5', 'authid' => $id, 'authpwd' => $pswd, 'subject' => 'Birthday invitation', 'boundary' => 'A boring Multipart bou +ndary', 'multipart' => 'mixed', 'encoding' => 'Quoted-printable', 'confirm' => 'reading', 'keepconnection' => 'true'})) { die "Creating obejct failed: $Mail::Sender::Error\n"; }
    You also need to check the OpenMultipart call for errors.

    You should always write your code as if every part of it will break, because unless you live in a perfect world, it probably eventually will.

      :-)
      You can also instruct the newest versions of Mail::Sender to throw an exception (die()) in case of errors. I guess that's a bit safer and easier since then you do not have to worry about each and every method call and just catch the exception where it makes most sense:

      eval { my $sender = new Mail::Sender { 'on_errors' => 'die', 'smtp' => $mail_server, 'from' => $id, 'auth' => 'CRAM-MD5', 'authid' => $id, 'authpwd' => $pswd, 'subject' => 'Birthday invitation', 'boundary' => 'A boring Multipart boundary', 'multipart' => 'mixed', 'encoding' => 'Quoted-printable', 'confirm' => 'reading', 'keepconnection' => 'true'}); $sender->OpenMultipart(...); $sender->Part(...); ... }; if ($@) { print "Failed to sent the message: $@\n"; } ...

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature

      Thanks for your tip, I should've known it (I learned it that way, after all :-). Anyways, the main problem turned out to be that the $mail_server variable was somehow affected; I replaced it by a constant string and everything worked. I also spotted some beginner's mistakes in my code, such as not escaping the square brackets. Everything is fixed now and I should be able to do the rest on my own.
      Thanks, all.
Re: Problems using Mail::Sender
by dreadpiratepeter (Priest) on Aug 22, 2003 at 16:53 UTC
    my guess would be that the call to new errored out and returned an error code instead of an object.

    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      Actually, you are right, it returns error code -1, which is, according to the docs, "smtphost unknown". The only problem with that is that another program I wrote using Mail::Sender, which uses the same host (I double-checked) works just fine. I have the impression it's something about the multipart-thing.
      Thanks a lot, though.