in reply to Re^7: Selecting DB
in thread Selecting DB

i have tried all options, which all monks given me. but am still facing some problem

this is the script, still cant send to all emails, it only sends to last email. but first ones are not being sent to

my $AID = "XD4555"; my $query = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID WHERE (m.USERID_IM = ?)"); $query->execute($AID); my $user1 = $query->fetchall_arrayref(); my $query_other = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID_IM WHERE (m.USERID = ?)"); $query_other->execute($AID); my $user2 = $query_other->fetchall_arrayref(); my $TotalEmails = [ @$user1, @$user2 ]; for my $em ( @$TotalEmails ){} my $message_s = "<p>hi test</p>"; my $smtpserver = 'mail.xxxxxx.com'; my $smtpport = 587; my $smtpuser = 'xxxx.com'; my $smtppassword = 'xxxxxxxx'; my $transport = Email::Sender::Transport::SMTP->new({ host => $smtpserver, ssl => 'starttls', port => $smtpport, sasl_username => $smtpuser, sasl_password => $smtppassword, }); my $email_s = Email::Simple->create( header => [ To => join(", ", @$em), From => 'xxxx@xxx.com', Subject => "test em", 'Content-Type' => 'text/html', ], body => $message_s, ); sendmail($email_s, { transport => $transport });

but now the error is that. 'no recipients' after doing some changes myself

Replies are listed 'Best First'.
Re^9: Selecting DB
by hippo (Archbishop) on Jul 10, 2023 at 14:22 UTC

    You have set the addresses you will be sending to like this:

    To => join(", ", @$em),

    but nowhere have you set $em to anything, so the array is empty, so you have no recipients. A quick SSCCE to show how to pull these AoAs together:

    #!/usr/bin/env perl use strict; use warnings; my $user1 = [ [ 'alpha@foo.com', 'beta@foo.dom' ], [ 'gamma@foo.com' ] + ]; my $user2 = [ [ 'delta@foo.com' ], [ 'epsilon@foo.dom', 'zeta@foo.com' + ] ]; my $TotalEmails = [ @$user1, @$user2 ]; my @to; for my $em ( @$TotalEmails ) { push @to, @$em; } my $tostring = join ',', @to; print "Full list of addrs: $tostring\n";

    See how that works? You don't even really need the intermediate $TotalEmails but I've left it in for clarity. You can now use $tostring when sending your emails and they should all go out.


    🦛

      my $tostring = join ', ', map @$_, @$TotalEmails;

      Too golfy ?

        For a novice, probably so. But it is a nice, concise solution (++). OTOH I might just reach for List::Flatten (or List::Flatten::Recursive if $TotalEmails were to be any deeper).

        use List::Flatten; my $tostring = join ',', flat @$TotalEmails;

        🦛

      Thanks its working. delivering now to all emails

      Thanks much

        its working. delivering now to all emails

        Yes, but you may have created a privacy issue. Currently, each and every receipient sees all other receipient. If all mails go to receipients in your company, no problem. If they go to "random" users on the internet, you better send to each receipient individually.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)