The ISP/cable provider Comcast blocks port 25 on its SMTP service for customers, requires password authentication and also, it turns out, doesn't like people using server names in the MAIL FROM field of the SMTP handshake. This last presents a problem as Mail::Mailer::smtp is hard wired to use the results of Mail::Util's mailaddress in the 'From' field. Mail::Mailer transmission modules, however, can be usefully subclassed, so the following works to send mail to Comcast:
mkdir -p lib/Mail/Mailer/smtp
And then create in this directory the file From.pm
package Mail::Mailer::smtp::From; use base Mail::Mailer::smtp; sub exec { my ($self, $exe, $args, $to) = @_; my %opt = @$args; my $host = $opt{Server} || undef; $opt{Debug} ||= 0; my $smtp = Net::SMTP->new($host, %opt) or return undef; if($opt{Auth}) { $smtp->auth(@{$opt{Auth}}) or return undef; } ${*$self}{sock} = $smtp; # # Adding a patch to set the From Address.. # $smtp->mail($self->mailaddress); $smtp->mail( $opt{From} ) if $opt{From}; # # End patch. # $smtp->to($_) for @$to; $smtp->data; untie *$self if tied *$self; tie *$self, 'Mail::Mailer::smtp::pipe', $self; $self; } 1;
And then you can send files to Comcast using code equivalent to this:
#! /usr/bin/perl use warnings; use strict; use lib './lib'; use Mail::Mailer::smtp::From; use Mail::Send; my $msg= Mail::Send->new; $msg->to('An@Address'); $msg->subject('Test Code'); my $fh = $msg->open('smtp::From', Server => 'smtp.comcast.net', Auth => [ 'user','pass' ], Port => 587, From => 'me@comcast.net', Debug => 1 ); print $fh "This is a test message.\n"; $fh->close or die("Could not send message.\n");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Getting Mail::Send to work with Comcast
by Anonymous Monk on Sep 24, 2010 at 22:45 UTC | |
by dwm042 (Priest) on Sep 25, 2010 at 01:58 UTC | |
by Anonymous Monk on Sep 25, 2010 at 04:16 UTC | |
Re: Getting Mail::Send to work with Comcast
by Mr. Muskrat (Canon) on Sep 27, 2010 at 21:22 UTC |
Back to
Cool Uses for Perl