Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl-Send e-mail 1

Status
Not open for further replies.

amy100

Programmer
Nov 18, 2008
6
US
hi
Im new to Perl.
Im trying to write a program to send an e-mail via my cgi script. I tried various options - MIME::Lite, Sendmail, Mail::Sender, and none seem to work.
I get no error message in the Server Log file, but I dont receive an e-mail either. I suspect that it cant establish a connection to the SMTP server. Is there a way of knowing what communication happens with the SMTP server. The code I use is as below:


Thanks for your help.
Amy
Code:
#!C:\Perl\bin\perl.exe
use CGI::Carp qw(fatalsToBrowser);

 use Mail::Sender;
 
 use Net::SMTP;

 print "Content-type: text/html\n\n";
 
 my $relay = "xxx.xxx.edu";
 my $smtp = Net::SMTP->new($relay)
    || die "Can't open mail connection: $!";
    my $to='xxx\@cs.xxx.edu';
    my $from='xxx\@cs.xxx.edu';
    
    $smtp->mail($from);
        $smtp->to($to);
    
        $smtp->data();
        $smtp->datasend("To: $to\n");
        $smtp->datasend("From: $from\n");
        $smtp->datasend("Subject: $subject\n");
        $smtp->datasend("\n");
    
        foreach $body (@body) {
            $smtp->datasend("$body\n");
        }
    
        $smtp->dataend();
    $smtp->quit();
 
 


 open my $DEBUG, ">> debugfile.txt"
                 or die "Can't open the debug file: $!\n";

 $sender = new Mail::Sender
  {smtp => 'mail.cs.xxx.edu', from => 'xxx@cs.xxx.edu'};
 $sender->MailFile({to => 'xxx@cs.xxx.edu',
  subject => 'Here is the file',
  msg => "I'm sending you the list you wanted.",
  debug => $DEBUG,
  debug_level=>3,
  authid=>'xxx',
  authpwd=>'pwd'
 });
   $sender->SendEnc;
  
  $sender->Close;
 
Hi,

For Net::SMTP possibly try attempting an SASL authentication:

Code:
my $smtp_username = 'your email/username';
my $smtp_password = 'your password';

my $smtp = Net::SMTP->new($relay) || die "Can't open mail connection: $!";

$smtp->auth($smtp_username, $smtp_password) || die "Can't authenticate SASL: $!";

Heres a quick example of using MIME::Lite:

Code:
my $attachment = 0;
my ($email);
use MIME::Lite;
$email = MIME::Lite->new(From    => $from,
To      => $to,
Subject => $subject,
Type    => 'multipart/mixed');
$email->attach(Type    => 'TEXT',
Data    => $message);
if ($attachment == 1) {
$email->attach(Type    => '',
Path	 => $attachment_fullpath,
Filename => $attachment_filename);
}

$email->send();

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top