in reply to Using Email::Stuff to send SMTP mail through GMail

brian_d_foy helped me understand that issues with the way all the modules Email::Stuff is using talk to one another make it necessary to pass through LocalHost and LocalPort variables in order to send mail via GMail:

"The problem is the long chain of modules with slightly different interfaces. I haven't narrowed it down, but somebody isn't setting LocalAddr and LocalPort, so when you try to configure things, IO::Socket::configure fails because IO::Socket::_sock_info fails since nothing set those hash entries:"

($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr}, $arg->{LocalPort}, $arg->{Proto}) or return _error($sock, $!, $@);

So my solution ended up looking like this:

#!/usr/bin/perl use warnings; use strict; use Email::Stuff; my $LocalHost = 'jasmines.mikebaas.org'; my $LocalPort = '2525'; my $RemoteHost = 'smtp.gmail.com'; my $RemotePort = '465'; my $objst = Email::Stuff ->to($ARGV[0]) ->from($ARGV[1]) ->subject($ARGV[2]) ->text_body($ARGV[3]) ; $objst->send( 'SMTP', ssl => 1, Host => $RemoteHost, Port => $RemotePort, Proto => 'tcp', username => $ARGV[4], password => $ARGV[5], Debug => 1, LocalAddr => $LocalHost, LocalPort => $LocalPort, );

However, I cannot send emails one after the other in serial since the localport 2525 binds to 465 and doesn't let go. As far as I can see, there is no way to send a QUIT via Email::Stuff. If there were, I could create one connection per each email I wish to send via Gmail.

I've also tried sleeping in between each message but because that always seemed to produce random results. So I'm closer but not out of the woods yet.

Replies are listed 'Best First'.
Re^2: Using Email::Stuff to send SMTP mail through GMail
by initself (Monk) on Oct 01, 2006 at 01:58 UTC

    Thanks to brian_d_foy, we've got the final solution: a patch to Net::SMTP. Hopefully this will be applied to CPAN in the next few days. Using this patch, you no longer have to declare a LocalPort or LocalAddr. Instead you set 'Reuse => 1'.

    $objst->send( 'SMTP', ssl => 1, Host => $RemoteHost, Port => $RemotePort, Proto => 'tcp', username => $username, password => $password, Debug => 1, Reuse => 1, );

    Here's the patch:

    # Patch to Net::SMTP BEGIN { use Net::SMTP; no warnings 'redefine'; *Net::SMTP::new = sub { print "In my Net::SMTP::new...\n"; package Net::SMTP; use Net::Config; use Net::Cmd; my $self = shift; my $type = ref($self) || $self; my ($host,%arg); if (@_ % 2) { $host = shift ; %arg = @_; } else { %arg = @_; $host=delete $arg{Host}; } my $hosts = defined $host ? $host : $NetConfig{smtp_hosts}; my $obj; my $h; foreach $h (@{ref($hosts) ? $hosts : [ $hosts ]}) { $obj = $type->SUPER::new(PeerAddr => ($host = $h), PeerPort => $arg{Port} || 'smtp(25)', $arg{LocalAddr} ? ( LocalAddr => $arg{LocalAddr} ) : ( +), $arg{LocalPort} ? ( LocalPort => $arg{LocalPort} ) : ( +), Proto => 'tcp', Timeout => defined $arg{Timeout} ? $arg{Timeout} : 120 ); last if $obj; } return undef unless defined $obj; $obj->autoflush(1); $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef); unless ($obj->response() == CMD_OK) { $obj->close(); return undef; } ${*$obj}{'net_smtp_exact_addr'} = $arg{ExactAddresses}; ${*$obj}{'ne +t_smtp_host'} = $host; (${*$obj}{'net_smtp_banner'}) = $obj->message; (${*$obj}{'net_smtp_domain'}) = $obj->message =~ /\A\s*(\S+)/; unless($obj->hello($arg{Hello} || "")) { $obj->close(); return undef; } $obj; } }