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

My script is sending emails, but i have a privacy issue that each and every recipients sees all other recipients

How can i send emails to each recipient individually.

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 ]; my @to; for my $em ( @$TotalEmails ) { push @to, @$em; } my $tostring = join ',', @to; my $message_s = "<p>hi just test</p>"; my $smtpserver = 'smtp.xxxxxx.com'; my $smtpport = 587; my $smtpuser = 'xxxxx'; my $smtppassword = 'xxxxx'; 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 => $tostring, From => 'xxx@xxxx.com', Subject => "test hi", 'Content-Type' => 'text/html', ], body => $message_s, ); sendmail($email_s, { transport => $transport });

Replies are listed 'Best First'.
Re: Sending email
by marto (Cardinal) on Jul 21, 2023 at 11:57 UTC

    Either use BCC rather than To, or send one mail at a time within the for loop, rather than push each email to an array.

      when i try something like this

      my $email_s = Email::Simple->create( header => [ Bcc => $tostring, From => 'xxx@xxxx.com', Subject => "test hi", 'Content-Type' => 'text/html', ], body => $message_s,

      this is the response

      no recipients

        Bcc doesn't count as recipients - by design. You need at least one recipient on the To line. People usually put themslelves in that field.

Re: Sending email
by pryrt (Abbot) on Jul 21, 2023 at 19:16 UTC
    Did you follow Email::Sender's QuickStart instructions for Bcc?

    Based on that, and the envelope information section on the same page, I would think your sendmail command should be something like

    $email_s->header_set('bcc'); # remove Bcc from headers, otherwise eve +ry recipient can still see the whole BCC list sendmail($email_s, { transport => $transport, to => [$bcc1, $bcc2, ... +] });

      I have tried it but emails are not getting delivered

      my $email_s = Email::Simple->create( header => [ From => 'xxx@xxx.com', Subject => "test hi", 'Content-Type' => 'text/html', ], body => $message_s, ); $email_s->header_set('bcc'); sendmail($email_s, { transport => $transport, to => [$tostring] });

        You don't show it here, but in your OP you have:

        my $tostring = join ',', @to;

        There's not enough of your code in one place, but I'd suggest changing

        to => [$tostring]

        to something closer to

        to => [@to]

        The '[...]' construct creates an anonymous array reference. Between '[' and ']', you can put a single scalar or a list of scalars:

        [$scalar1] # OK [$scalar1, $scalar2] # OK ["$scalar1, $scalar2"] # NOT OK ['$scalar1, $scalar2'] # NOT OK

        — Ken

Re: Sending email
by afoken (Chancellor) on Jul 21, 2023 at 16:58 UTC

    Context: Re^11: Selecting DB

    Alexander

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