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

Hello,

I have no problem sending to a single email address. However, I can't seem to make email::send::gmail send to multiple addresses.

If I use To=>'add@one.com', it will send the email fine. If I use To=>'add@one.com,add@two.com', it fails with

"Error sending email: Email::Send::Gmail: no valid recipients at /usr/ +lib/perl5/site_perl/5.8.8/Email/Send.pm line 252 at /usr/lib/perl5/si +te_perl/5.8.8/Email/Send.pm line 252"
It fails with the same error if I try setting a Bcc=> 'addr@two.com' in the header, as well. Here is a snippet of code I'm using (addresses changed to spare the innocent):
my $multiple = 'test@one.com,test@two.com'; my @headers = ( From => 'Sender <sender@sending.com>', To => $multiple, Subject => $subject ); my $email = Email::Simple->create( header => \@headers, body => $params{"body"}, ); my $sender = Email::Send->new( {mailer => 'Gmail', mailer_args => [ username => 'user@gmail.com', password => 'password', ] } ); eval { $sender->send($email) }; print qq(Error sending email: $@) if $@;
Again, this works fine if I change $multiple to read:
my $multiple = 'test@one.com';
Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Multiple Recipients in email
by Anonymous Monk on Jan 24, 2009 at 07:49 UTC
    The gmail mailserver is telling you "no valid recipients", meaning it won't let you do that.
      That's odd, because I can send email to multiple recipients (and CCs, and BCCs) through standard email clients connecting to gmail through smtp... So what are those clients doing that I am not?
        Something or other :) Copy this and turn on Debug. http://cpansearch.perl.org/src/LBROCARD/Email-Send-Gmail-0.33/lib/Email/Send/Gmail.pm
        mostly cribbed from Email::Send::SMTP sub send { my ( $class, $message, @args ) = @_; my %args = @args; my ( $username, $password ) = @args{qw[username password]}; my $smtp = Net::SMTP::SSL->new( 'smtp.gmail.com', Port => 465, Debug => 0, ) || croak( 'Email::Send::Gmail: error connecting to server smtp.gmail.com +'); $smtp->auth( $username, $password ) or croak("Email::Send::Gmail: error authenticating username $user +name"); my @bad; eval { my $from = $class->get_env_sender($message); $smtp->mail($from) || croak("Email::Send::Gmail: error sending 'from' $from") +; my @to = $class->get_env_recipients($message); my @ok = $smtp->to( @to, { SkipBad => 1 } ) || croak("Email::Send::Gmail: error sending 'to' @to"); if ( @to != @ok ) { my %to; @to{@to} = (1) x @to; delete @to{@ok}; @bad = keys %to; } croak("Email::Send::Gmail: no valid recipients") if @bad == @t +o; }; croak($@) if $@; croak("Email::Send::Gmail: error sending data") unless $smtp->data( $message->as_string ); $smtp->quit || croak("Email::Send::Gmail: error sending 'quit'"); return 1; }
        Net::SMTP
      The "no valid recipients" message is coming from a croak statement in the Email::Send::Gmail module.

      The problem as is turns out is an error in the module code that varifies the recipient addresses. The current version of the module finds an error with multiple addresses when there is none.

      The fix was simple. Just needed a pair of parentheses around line 57:

      <<< my @ok = $smtp->to( @to, { SkipBad => 1 } ) >>> (my @ok = $smtp->to( @to, { SkipBad => 1 } ))
      I have sent an email to the maintainer to this effect. Hopefully he will find time to make the change.
        I know this is old, but thanks!!! This was the problem. Thanks for posting this4 years ago!
        Yes, thanks for that suggested fix to Gmail.pm! It was line 57 in my copy. This fixed my problem!
Re: Multiple Recipients in email
by tmharish (Friar) on Aug 13, 2009 at 06:55 UTC

    A little while back I spent some time over this.

    Gmail seems to require some additional headers to ensure mails are not sent to the Junk Mail folder / Label.

    Here is the code I used:

    use warnings; use strict; use Carp; use MIME::Lite; use Net::SMTP::SSL; use Data::Dump qw( dump ); use base 'Exporter'; sub send_mail { my $to = shift; my $subject = shift; my $body = shift; my $from = shift; my $password = shift; my $from_mime = $from; my $count = @_; if( $count ) { my $from_name = shift; $from_mime = "\"$from_name\" <$from>"; } my $msg = MIME::Lite->new ( From => "$from_mime" , To => "$to" , Subject => "$subject" , Data => "$body" , ); my $smtp; if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com', Port => 465, Debug => 0)) { croak "Could not connect to server\n"; } $smtp->auth($from, $password) || croak "Authentication failed!\n"; $smtp->mail($from . "\n"); my @recepients = split(/,/, $to); foreach my $recp (@recepients) { $smtp->to($recp . "\n"); } $smtp->data(); $smtp->datasend( $msg->as_string() ); $smtp->dataend(); $smtp->quit; }

    The funny thing however is that one of the modules that are being called seem to have some problem in the latest version.

    Here is the debug output of what works and what does not:

    Works:
    Net::SMTP::SSL>>> Net::SMTP::SSL(1.01) Net::SMTP::SSL>>> IO::Socket::SSL(1.13) Net::SMTP::SSL>>> IO::Socket::INET(1.29) Net::SMTP::SSL>>> IO::Socket(1.29) Net::SMTP::SSL>>> IO::Handle(1.25) Net::SMTP::SSL>>> Exporter(5.58) Net::SMTP::SSL>>> Net::Cmd(2.29)
    Does not work:
    Net::SMTP::SSL>>> Net::SMTP::SSL(1.01) Net::SMTP::SSL>>> IO::Socket::SSL(1.24) Net::SMTP::SSL>>> IO::Socket::INET(1.31) Net::SMTP::SSL>>> IO::Socket(1.30_01) Net::SMTP::SSL>>> IO::Handle(1.27) Net::SMTP::SSL>>> Exporter(5.62) Net::SMTP::SSL>>> Net::Cmd(2.29)

    I solved the problem by using an older version of the modules. I am sure there is a better way though!!