Hello, I usually do not program in perl, so I apologize that I do not even know where to begin looking for solution.

Web app I am building in php, I am trying to be very careful with user e-mails to avoid data leaks and they are in a different database then the web app uses, the web app cues a message to a user id when it wants to e-mail user.

That is where perl comes in. I want perl script running from cron every 10 minutes to fetch messages from the cue, associate e-mail with user id, and send it.

sendmail (er, postfix...) is working on the server and sending mail via /usr/sbin/sendmail works. Host is CentOS 7 x86_64

The error I get from the script:

Name "main::id" used only once: possible typo at sendmessages.pl line +39. Use of uninitialized value in lc at /usr/share/perl5/vendor_perl/Email +/Simple/Header.pm line 181. Use of uninitialized value in lc at /usr/share/perl5/vendor_perl/Email +/Simple/Header.pm line 181. Use of uninitialized value in lc at /usr/share/perl5/vendor_perl/Email +/Simple/Header.pm line 181. no recipients Trace begun at /usr/share/perl5/vendor_perl/Email/Sender/Simple.pm lin +e 107 Email::Sender::Simple::send_email('Email::Sender::Simple', 'Email::Abs +tract=ARRAY(0x34f66e8)', 'HASH(0x35be030)') called at /usr/share/perl +5/vendor_perl/Email/Sender/Role/CommonSending.pm line 27 Email::Sender::Role::CommonSending::__ANON__ at /usr/share/perl5/vendo +r_perl/Try/Tiny.pm line 71 eval {...} at /usr/share/perl5/vendor_perl/Try/Tiny.pm line 67 Try::Tiny::try('CODE(0x35bdfd0)', 'Try::Tiny::Catch=REF(0x34ccf28)') c +alled at /usr/share/perl5/vendor_perl/Email/Sender/Role/CommonSending +.pm line 40 Email::Sender::Role::CommonSending::send('Email::Sender::Simple', 'ali +cewonder@###') called at /usr/share/perl5/vendor_perl/Sub/Exporter/Ut +il.pm line 18 Sub::Exporter::Util::__ANON__('alicewonder@###') called at sendmessage +s.pl line 64 (### is hiding the valid domain, I h8 spam)

(the main::id I understand, it will be used in code once I get send working)

The code:

#!/usr/bin/perl -w use DBI; $dbh = DBI->connect('dbi:mysql:alicenet','####','####', { mysql_enable +_utf8 => 1, }) or die "Connection Error: $DBI::errstr\n"; $adbh = DBI->connect('dbi:mysql:anetauth','####','####', { mysql_enabl +e_utf8 => 1, }) or die "Connection Error: $DBI::errstr\n"; #count the messages $sql = "SELECT COUNT(id) AS count FROM mailcue WHERE failed < 3 AND la +sttry < (NOW() - INTERVAL 90 MINUTE)"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI:: errstr\n"; @row = $sth->fetchrow; $count = $row[0]; #max number of cued messages to fetch $n = ($count < 180)? 30 : int($count/6); #seconds to sleep between action - we want to finish in 8 minutes $sleep = int(480/$n); $sql = "SELECT id,userid,subject,message FROM mailcue WHERE failed < 3 + AND lasttry < (NOW() - INTERVAL 90 MINUTE) ORDER BY id LIMIT $n"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; while (@row = $sth->fetchrow_array) { $id = $row[0]; $userid = $row[1]; $subject = $row[2]; $body = $row[3]; $asql = "SELECT email FROM userauth WHERE id=$userid"; $asth = $adbh->prepare($asql); $asth->execute; @arow = $asth->fetchrow; $email = $arow[0]; # okay we can send this futher mocker use Email::MIME; my $message = Email::MIME->create( header_str => [ From => 'anetbot@domblogger.net', To => $email, Subject => $subject, ], attributes => { encoding => '8bit', charset => 'utf-8', }, body_str => $body, ); # send it use Email::Sender::Simple qw(sendmail); my $response = sendmail($email); # on success, delete from mailcue. On fail, increment failed field in + mailcue and insert error message into error field of mailcue. sleep $sleep; }

Thank you for any assistance in figuring out how to fix that error.


In reply to Use of uninitialized value in lc by Alice Wonder

Title:
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, details, 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, summary, 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.