Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Using Sendmail.pm problem

by tariqahsan (Beadle)
on Sep 22, 2006 at 14:17 UTC ( [id://574377]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

This might very likely be a server environment problem.
But I am lost here to find the solution for this problem.
I am using my downloaded version of the Sendmail.pm from cpan
installed into a directory where I have the permission do so.
I have created a directory 'Mail' and copied the Sendmail.pm file in that directory.
Running this script -

#!/usr/bin/perl use MIME::QuotedPrint; use MIME::Base64; # Get environment variable value $home = $ENV{"HOME"}; BEGIN { push @INC, "$home/scripts/Mail" }; use Mail::Sendmail; $receiver = 'myemail@abc.com'; $sender = 'myemail@abc.com'; $subject = 'Test mail'; $message = 'This is a test mail'; $file = 'test.txt'; #mail parameter file $mailfile = 'mailparam.dat'; open (MAILFILE, "$mailfile") || die "Can't open $mailfile for reading: + $!\n"; while ( defined ( $_ = <MAILFILE>) ) { chomp $_; @mailarray = split(/\|/); $receiver = $mailarray[0]; $sender = $mailarray[1]; $sender = $mailarray[1]; $subject = $mailarray[2]; send_mail($receiver, $sender, $subject, $message, $file); } sub send_mail { my ($receiver, $sender, $subject, $message, $file) = @_; %mail = ( from => "$sender", to => "$receiver", subject => "$subject", ); $boundary = "====" . time() . "===="; $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; $message = encode_qp( $message ); open (F, $file) or die "Cannot read $file: $!"; binmode F; undef $/; binmode F; undef $/; $mail{body} = encode_base64(<F>); close F; $boundary = '--'.$boundary; $mail{body} = <<END_OF_BODY; $boundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable $message $boundary Content-Type: application/octet-stream; name="$file" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="$file" $mail{body} $boundary-- END_OF_BODY sendmail(%mail) || print "Error: $Mail::Sendmail::error\n"; }
Getting this error message in one of the unix server I am running on -
Undefined subroutine &main::sendmail called at sendmail.pl ...
But running this same script I don't have any problem whatsoever
in another unix server.

Any help to solve this problem would be much appreciated.

Thanks

Replies are listed 'Best First'.
Re: Using Sendmail.pm problem
by davorg (Chancellor) on Sep 22, 2006 at 14:30 UTC
    $home = $ENV{"HOME"};
    BEGIN { push @INC, "$home/scripts/Mail" };
    use Mail::Sendmail;

    This all looks rather dodgy. The variable $home doesn't get given a value until runtime, but you try to access it in a BEGIN block which is executed at compile time (i.e. before $home has a value). I think you'd be better off using:

    use lib "$ENV{HOME}/scripts/";

    I've left off the "Mail" from that path as because it's part of the module name, you don't need it in the library path.

    I'm also a bit dubious about how you installed the module. Creating directories and copying files around is prone to failure. I'd really recommend that you did it again using the standard "perl Makefile.PL / make / make test / make install" commands. I realise that you don't have permission to install modules into the central library, but you can pass arguments to "perl Makefile.PL" to get round that.

    Having said all that, I'm surprised by the error message you get. I would expect you to see an error saying that the module couldn't be loaded successfully.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Surprisingly as I mentioned it works fine in a different server.
      I know I should try to get this perl module installed
      in the right way. I'll try to see if I can tweak something in the perl module itself to make it work for now.

      Any suggestions in this regard would be much appreciated.

      Thanks

        There's no point in trying to "tweak something in the perl module" as the Perl module is almost certainly not being loaded successfully. The problem is not with the module, it is with how you installed the module and how your are subsequently trying to load it.

        If it works on another server then perhaps the sysadmin on that other server has already installed it, so your broken attempt to change @INC is having no effect.

        Try running this from the command line of the server where it works. This will tell you where the file is being loaded from:

        perl -MMail::Sendmail -le 'print $INC{"Mail/Sendmail.pm"}'
        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Using Sendmail.pm problem
by zentara (Archbishop) on Sep 23, 2006 at 12:02 UTC
    It sounds like your ENV method isn't working on the problem server, probably some security thing about a script changing it's ENV.

    Instead of

    # Get environment variable value $home = $ENV{"HOME"}; BEGIN { push @INC, "$home/scripts/Mail" }; use Mail::Sendmail;
    try putting the module in the script's execution directory
    use lib '.'; use Mail::Sendmail;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://574377]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found