Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Your problems are manyfold:
  • You call the new() sub without passing it any parameters, and inside the sub you attempt to read from $self, which is an empty hashref.
  • You pass the subject and message params to send as a hashref, yet they end up in the $user variable, which itself is never used
You seem to be missing the fact that OO is not quite magic, methods are just ordinary subs, so you still need to read their parameters from what gets passed in. Is the subject supposed to be set on new() (ie object creation), or when you send the email?

Also, you have a global %mail hash, which will stay the same for every PL::Mail object you create, which probably isnt what you want.. To make it unique per object, you need to store that data in $self.

Also you need to be very careful about case sensitivity, $self->{SUBJECT} and $self->{subject} are not the same thing.

Here is an attempt at repair:

package PL::Mail; use strict; use Mail::Sendmail; ## Only global is your server since it doesnt change (?) my $server = ''; ## The new constructor just sets up the server and the From address si +nce these are static (could put the server name here directly and not + have a global variable) sub new { my $class = shift; my $self = {}; $self->{mail}{Smtp} = $server; $self->{mail}{From} = 'Postmaster <>'; bless $self, $class; return $self; } ## The send method is passed a hashref, containing subject, message an +d optionally a to key. ## eg $obj->send({subject => 'testing', message => 'just trying to sen +d mail', to => 'me'}); ## The $self hashref, which represents the object, is used to store al +l the parts of the %mail hash, which is then passed to sendmail sub send { my ($self, $data) = @_; $self->{mail}{To} = $data->{to} ? $self->{to} : 'Devin Austi +n <>'; $self->{mail}{Subject} = $data->{subject}; $self->{mail}{Message} = $data->{message}; my $date = Mail::Sendmail::time_to_date(); $self->{mail}{Message} .= <<"" Message sent on $date From $ENV{REMOTE_ADDR} All information contained in this message do not reflect the ideas of Devin Austin or MorningStarWeb and it's affiliates. ; if (sendmail %{$self->{mail}) { print STDERR $Mail::Sendmail::error; } else { print STDERR "Error sending mail ($date): $!"; } }


In reply to Re: problematic OO by castaway
in thread problematic OO by stonecolddevin

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (5)
    As of 2018-04-22 13:40 GMT
    Find Nodes?
      Voting Booth?