Smtp via Gmail from DreamHost

software development

#1

Can someone help me determine what is wrong w/this code?
Code runs w/o error, but mail is not being delivered.
bill

[code]#@(#) file: send_mail.pl
#@(#) email message
#@(#) author: William.Haynes@usa.net

sub send_mail {
use strict;
use lib qw( /home/coacho/lib );
use CORssl;
use Mail::Mailer;
use vars qw( $EVAL_ERROR $GMAILPW $NULL );

require “./adm/cvt_html2text.pl”;
require “./lib/log_email.pl”;

my($to,$from,$replyto,$subject,@msg) = @_;

my $line;
my $mailer = $NULL;
my $pw = $GMAILPW;
my $rc=$NULL;
my $server = “smtp.gmail.com”;
my $user = "mail@coacho.com";

$mailer = Mail::Mailer -> new(“smtp”, Server => “$server”, Port => 465, Auth => [ “$user”, “$pw” ] );

clean up to addresses

strip any whitespace form To and Reply-to fields

then make sure that comma separated values are followed

by a space

$to =~ s/\s+//g;
$to =~ s/,/, /g;

$from =~ s/\s+//g;
$from =~ s/,/, /g;

$replyto =~ s/\s+//g;
$replyto =~ s/,/, /g;

wrapper mailer so that we can report errors

eval {
if ($replyto) {
$mailer->open({ ‘From’ => $from,
‘Reply-To’ => $replyto,
‘To’ => $to,
‘Subject’ => $subject,
}) or die “Can not open: $!\n”;
} else {

no reply-to

     $mailer->open({ 'From' => $from,
                     'To'   => $to,
                     'Subject' => $subject,
     }) or die "Can not open: $!\n";
  }

  foreach $line (@msg) {
     $line = cvt_html2text($line);
     print $mailer ("$line\n");
  }

  $mailer->close()
     or die "could not send whole message: $!\n";

};

NOTE: $EVAL_ERROR is set by eval

check for errors

if ( $EVAL_ERROR ) {
$rc = $EVAL_ERROR;
}

log outgoing e-mails to provide foundation for statistics

log_email($to,$from,$replyto,$subject,$rc);

return($rc);
} # End send_mail.pl
1;[/code]


#2

What jumps out at me first is that your code is not using the English pragma, so $@ is not aliased to $EVAL_ERROR, and you will not be notified of any errors that occur within the eval block. Fix that (either by adding “use English;” or by using $@ instead of $EVAL_ERROR) and any other errors will become significantly easier to find.


#3

thanks
made this change
now process fails to close
bill


#4

found bug
fixed…now getting this exception reportd
Completion code=Died at /usr/share/perl5/Mail/Mailer.pm line 150.

for initial test made change to use smtp @ DreamHost
here is revised function

using smtp instead of smtps
#@(#) file: send_mail.pl
#@(#) email message
#@(#) author: William.Haynes@usa.net

sub send_mail {
use strict;
use lib qw( /home/coacho/lib );
use CORssl;
use Mail::Mailer;
use English;
use vars qw( $GMAILPW $NULL );

require “./adm/cvt_html2text.pl”;
require “./lib/log_email.pl”;

my($to,$from,$replyto,$subject,@msg) = @_;

my $line;
my $mailer = $NULL;
my $rc = 0;

smtp configuration

DreamHost setup for test

my $server = ‘mail.coachoregistration.com’;
my $user = ‘mail@coachoregistration.com’;
my $pw = ‘xxxxx’;
my $port = 587;

GMAIL

my $server = ‘smtp.gmail.com’;

my $user = ‘mail@coacho.com’;

my $pw = $GMAILPW;

my $port = 465;

$mailer = Mail::Mailer -> new(“smtp”, Server => “$server”, Port => $port, Auth => [ “$user”, “$pw” ] );

clean up to addresses

strip any whitespace form To and Reply-to fields

then make sure that comma separated values are followed

by a space

$to =~ s/\s+//g;
$to =~ s/,/, /g;

$from =~ s/\s+//g;
$from =~ s/,/, /g;

$replyto =~ s/\s+//g;
$replyto =~ s/,/, /g;

wrapper mailer so that we can report errors

eval {
if ($replyto) {
$mailer->open({ ‘From’ => $from,
‘Reply-To’ => $replyto,
‘To’ => $to,
‘Subject’ => $subject,
}) or die “Can not open: $!\n”;
} else {

no reply-to

     $mailer->open({ 'From' => $from,
                     'To'   => $to,
                     'Subject' => $subject,
     }) or die "Can not open: $!\n";
  }

  foreach $line (@msg) {
     $line = cvt_html2text($line);
     print $mailer ("$line\n");
  }

  $mailer->close()
     or die "could not send whole message: $!\n";

}; # end eval

NOTE: $EVAL_ERROR is set by eval

check for errors

if ( $EVAL_ERROR ) {
$rc = $EVAL_ERROR;
}

log outgoing e-mails to provide foundation for statistics

log_email($to,$from,$replyto,$subject,$rc);

return($rc);
} # End send_mail.pl
1;


#5

can anyone recommend a Perl module to use SMTP w/authenication. looking for good examples. Trying to move away form Sendmail.


#6

I asked one of our e-mail gurus for you, and it turns out the sky is full of options when it comes to Perl! Here are a couple places to start from:

http://www.revsys.com/writings/perl/sending-email-with-perl.html
http://search.cpan.org/~rjbs/Email-Sender-1.300004/lib/Email/Sender.pm


#7

I think I already mentioned Net::SMTP in another thread, but here, I’ll mention it again. :slight_smile:


#8

1st article is about Net::SMTP
have it working but it does not support ReplyTo that I need

Email::Sender is not installed

can you pull some other references to review?
bill


#9

Net::SMTP will send an email with whatever headers you give it. You can use MIME::Entity to build the message.


#10

has anyone used Mail::Mailer module w/smtp or smtps ?
looking for additional documentation

bill