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

Hi all, I'm looking to send two different emails to two different clients. Currently, I only have the option to send one each if it meets a condition. I want one of these conditions to send two separate e-mails with different bodies and to different end users. What do you recommend I add to the code or what should I be looking into further to help figure this out? I've tried copying this section twice and tweaking it to new conditions but it doesn't send any emails out at all when I test it. Also tried different if else conditions that haven't worked for me. Thanks in advance.
$msgHTML .= "</FONT></BODY></HTML>"; my %mail; if ($product_row->{'ProductName'} eq "Company1"){ $mail{'subject'} = sprintf("Second Order Confirmation - Order Numb +er %s", $ordernum); $mail{'from'} = "Second Automatic Order Processor <orders\@mysite. +com>"; } else { $mail{'subject'} = sprintf("First Order Confirmation - Order Numbe +r %s", $ordernum); $mail{'from'} = "First Order Processor <orders\@mysite.com>"; } if ($bTestMode) { $mail{'to'} = 'myemail@address.com'; } else { my $deliveryEmail = param('reseller'); # if this is not null, deli +very will redirect to this email address if ($deliveryEmail) { $mail{'to'} = $deliveryEmail; $mail{'cc'} = 'myemail@mysite.com'; } else { $mail{'to'} = 'myemail@mysite.com,myemail@mysite.com'; } } $mail{'smtp'} = 'mail.mysite.com'; my $mimeMail; $mimeMail = MIME::Lite->new(From => $mail{'from'}, To => $mail{'to'}, Subject => $mail{'subject'}, Type => 'multipart/related', AuthUser=> 'user', AuthPass=> 'password'); my $both_parts = MIME::Lite->new( Type => "multipart/alternative", Data => $msgtxt, ); my $text_part = MIME::Lite->new( Type => "text/plain", Data => $msgtxt, ); my $html_part = MIME::Lite->new( Type => 'text/html', Data => $msgHTML); $both_parts->attach($text_part); $both_parts->attach($html_part); if ($product_row->{'ProductName'} eq "Company1"){ $mimeMail ->attach($both_parts); $mimeMail->attach( Encoding => 'base64', Type => 'image/jpeg', Path => './logo.jpg', Id => 'logo.jpg', Disposition => 'inline' ); } if ($product_row->{'ProductName'} ne "Company2"){ $mimeMail ->attach($both_parts); $mimeMail->attach( Encoding => 'base64', Type => 'image/jpeg', Path => './logo.jpg', Id => 'logo.jpg', Disposition => 'inline' ); } $mimeMail->send; print end_html; $dbh1->disconnect; $dbh2->disconnect; $dbh3->disconnect; sub uncommify { if ($_[0] eq "") { return "0"; } my $newstr = $_[0]; $newstr =~ s/,//g; return $newstr; } sub commify { if ($_[0] eq "") { return "0"; } my $text = reverse uncommify($_[0]); $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } }

Replies are listed 'Best First'.
Re: Need to send two simultaneous mails
by Corion (Patriarch) on Sep 12, 2018 at 07:28 UTC

    Where exactly are you having problems? Is it that the logic for determining receivers is wrong or is it that the logic for sending mails is wrong?

    You can approach this by writing a dummy mail routine that just prints out its parameters if it was called. That way you will know if your mail sending routine is called twice or only once.

    If you have only one order in your hand, but need to send more than one mail, I would suggest that you accumulate all the mails you need to send in a list, and at the end work through that list to send all the mails. In your code, I only see that you are setting the sender of the mail, but you only ever construct one mail. Maybe that is your problem?