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

Hi all
i have Net:SMTP working okay with one e-mail address, but when i try and get the program to cc or send to another person it does not get to the next address. e.
my $ServerName = SMAILSVR}; $smtp = Net::SMTP->new($ServerName) my $MailFrom = 'postmaster\@testme.com'; my $MailTo1 = testmeuser@home.com}; $smtp->mail( $MailFrom ); $smtp->to( $MailTo1 ); @to = ('testme@yahoo.co.uk'); $smtp->recipient(@to); $smtp->data(); $smtp->datasend(); $smtp->quit();
I have even tried
my $ServerName = SMAILSVR}; $smtp = Net::SMTP->new($ServerName) my $MailFrom = 'postmaster\@testme.com'; my $MailTo1 = testmeuser@home.com}; my $MailTo2 = testmeuser1@home.com}; $smtp->mail( $MailFrom ); $smtp->to( $MailTo1 ); $smtp->to( $MailTo2 ); $smtp->data(); $smtp->datasend(); $smtp->quit();
Am i missing something or does Net:SMTP not handle multiple e-mail addreses, i have checked the module, and it looks like it.

2003-05-01 edit ybiC: <code> tags

Replies are listed 'Best First'.
Re: Net:SMTP multiple sent to
by perlplexer (Hermit) on May 01, 2003 at 15:38 UTC
    From perldoc Net::SMTP

    to ( ADDRESS [, ADDRESS [...]] )

    So, I'm guessing you need to pass a list of Email IDs to to() or recipient()

    --perlplexer
      I have tried the option to put in
      $smtp->to('testme@hom.com, test2@home.com');
      that did not seem to work
        The Net::SMTP documentation says that won't work. Standard Perl lists (I think it can also take an array reference) are what is used for multiple addresses. to is a synonym for recipient so there isn't any need for using both or making multiple calls.
        $smtp->receipient($MailFrom1, $MailFrom2);
        Also, what is up with:
        my $MailFrom = 'postmaster\@testme.com'; my $MailTo1 = testmeuser@home.com};
        Did the code get mangled from posting because I don't think that will compile. In any case, the second string should be in single quotes. You don't need or wan't a backslash inside single quotes.
        my $MailFrom = 'postmaster@testme.com'; my $MailTo1 = 'testmeuser@home.com';

        Finally, should be checking the return codes of the calls. You don't know if the recipient call is failing because the mail server is rejecting the address or because the addresses are misformed.

        A list of emails. Not a single scalar with all emails separated by commas.
        my @emails = ('foo@var.com', 'bar@foo.com'); $smtp->to(@emails);
        --perlplexer
Re: Net:SMTP multiple sent to
by semio (Friar) on May 01, 2003 at 17:27 UTC
    hi minixman
    First, use strict with warnings if you are not doing so.
    Second, when you're having problems, turn on debugging.
    Third, You can specify multiple recipients as follows.
    #!perlenv -w use strict; use Net::SMTP; my $ServerName = 'MAILSERVER'; my $smtp = Net::SMTP->new($ServerName, Debug => 1,); my $MailFrom = 'test@test.net'; my $MailTo1 = 'test@test.net'; my $MailTo2 = 'test@test.net'; $smtp->mail( $MailFrom ); $smtp->to ( $MailTo1 ); $smtp->data(); $smtp->datasend("To: $MailTo1\n"); $smtp->datasend("cc: $MailTo2\n"); $smtp->datasend("test"); $smtp->quit;
    hope this helps,
    cheers, semio