in reply to Multiple Recipients in email
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:Does not work: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)
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!!
|
|---|