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

Hi Monks,

I have a script that I use to send e-mail when I have added them to my ftp server, however, these individuals change daily and weekly. That said, previously I manually created an array and added the users and it worked, but now that I push users onto the array from the script, the script works but doesn't send the e-mail? I have come to a hard stop on this. Does anyone see a reason why this would not work? I have placed a snippet (not the whole script)for your thoughts. I thought--pushing users onto the array would work. As always, thank you.

my @addresses; print "Enter address(s) and press [Enter]: "; chomp($usermail= <STDIN>); print "\nE-mail Address(s) are: $usermail\n"; ## Push each user onto the array ## push(@addresses,$usermail); ### E-mail Routine ### if($usermail ne "") { $smtp = Net::SMTP->new( 'SMTPSERVERNAME' ); $smtp->mail('sender@mydomain.com'); $smtp->recipient(@addresses); $smtp->data(); #Send the header. $smtp->datasend( "To: @addresses\n" ); $smtp->datasend( "From: sender\@mydomain.com\n" ); $smtp->datasend( "Subject: FTP Access \n" ); $smtp->datasend( "\n" ); #Send the body $smtp->datasend("You have been invited to access the my projec +t FTP Site by using the following 'project name' and 'password.' \n"); my $msg = <"\nproject name is: $username\n\nPassword is: $user +passwd\n\n$MOTD">; $smtp->datasend( $msg ); $smtp->dataend(); $smtp->quit; };

Replies are listed 'Best First'.
Re: Net::SMTP and Arrays
by kyle (Abbot) on Feb 25, 2008 at 18:26 UTC

    Is this what you meant?

    if($usermail ne "") {

    This is going to send email when $usermail is not blank. From the stuff inside the condition, it appears that you're sending to every address entered so far. So, for example:

    1. You enter u1@example.com, mail is sent to:
      • u1@example.com
    2. Then you enter u2@example.com, mail is sent to:
      • u1@example.com
      • u2@example.com
    3. Then you enter u3@example.com, mail is sent to:
      • u1@example.com
      • u2@example.com
      • u3@example.com

    Then again, you say that the mail is not sent at all, so perhaps I'm way off in the weeds here.

    I'd suggest that you use Data::Dumper to show the contents of @addresses before the attempt at delivery.

    use Data::Dumper; print Dumper \@addresses;

    It's a good idea to show $usermail before you push it into the list too.

    warn "\$usermail = '$usermail'\n";

    Those two should give you a better idea of what's going on.

      It doesn't send e-mail to anyone. When I print the $usermail out to the console, it shows all the e-mail addresses. However, it doesn't send the mail. I don't know if it is the recipient and I should change it to 'to' or, if that it doesn't accept something about the array.

Re: Net::SMTP and Arrays
by hesco (Deacon) on Feb 25, 2008 at 22:11 UTC
    Also, if I understand the RFC for email, you need two line feeds between the header and the body of the email message. So it looks like this instead:

    $smtp->datasend( "Subject: FTP Access \n" ); $smtp->datasend( "\n\n" ); #Send the body
    I tend to use MIME::Lite to construct the message, then its ->body_as_string() and ->header_as_string() methods, to feed into the $smtp->datasend() methods.

    Also, Net::SMTP has a debug flag you can set in its constructor, which will give you very verbose feedback, every step of the way. Did you check the return value of your constructor? If the smtp server can not be reached, the constructor should fail. After reviewing perdoc for this module to make sure I got this right, try something like this:

    my $smtp = Net::SMTP->new($smtp_server, Hello => $my_mail_servers_id, Debug => 1 ) or return 0;
    When you've got this worked out, turn debug off, by setting it to 0, in the constructor.

    -- Hugh

    if( $lal && $lol ) { $life++; }
Re: Net::SMTP and Arrays
by chrism01 (Friar) on Feb 26, 2008 at 06:15 UTC
    If you type in all the addresses on one line, even as a csv list, what you'll get is all the input addresses as the first element of the @addresses array.
    This is not going to work. You need to

    (@addresses) = split(/,/ $usermail);

    and enter them as a csv list on the cmd line.

    Otherwise, loop around the push(), entering each addr on a new line, until you've done them all, then proceed.

      Thank you. After you pointed that out..it dawned on me that yes..all of the addresses were one (1) element in the array ;-) I split everything thing up and it worked. Thanks again.