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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I send email without using UNIX sendmail?

Sending email

How do I send email without using UNIX sendmail?

by  MikeLacey  Posted    (Edited  )
Please browse through faq219-2884 and faq219-2889 first. Comments on this FAQ and the General FAQ's are very welcome.

This neat sub was posted by AaronGeorge.

Use the module, Net::SMTP available from CPAN (www.cpan.org). It will work on any system with or without sendmail, UNIX or windows.

Code:
sub send_mail {

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

use Net::SMTP;

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

[tab]$smtp->mail($from);
[tab]$smtp->to($to);

[tab]$smtp->data();
[tab]$smtp->datasend("To: $to\n");
[tab]$smtp->datasend("From: $from\n");
[tab]$smtp->datasend("Subject: $subject\n");
[tab]$smtp->datasend("\n");

[tab]foreach $body (@body) {
[tab][tab]$smtp->datasend("$body\n");
[tab]}

[tab]$smtp->dataend();
[tab]$smtp->quit();
}

And this one, which handles attachments, is from Paul Becket

It uses the module, MIME::Lite also available from CPAN.

Code:
use MIME::Lite;
#.....

sub Mail_File {
    my $fileName      = $_[0];   # To attach
    my $myMailAddress = $_[1];   # Your email address / address e-mail should appear from
    my $email_address = $_[2];   # Recipients mail address
    my $title         = $_[3];   # Email title
    my $body_message  = $_[4];   # Text in main part of e-mail
    my $fileType      = $_[5];   # Know whether attachment is 'BINARY' or 'TEXT'
    my $fileName      = $_[6];   # Name of file to attach (including path)
    my $outFileName   = $_[7];   # Name to give e-mail attachment
  
    # Create MIME::Lite mail object
    my $msg = MIME::Lite->new(
               From     => $myMailAddress,
               To       => $email_address,
               Subject  => $title,
               Type     => 'multipart/mixed',
               );

    # Main Body of message
    $msg->attach(
         Type     => 'TEXT',
                 Data     => $body_message
                 );

    # Attach file here
    $msg->attach(Type        => $fileType,
                 Path        => $fileName,
                 Filename    => $outFileName,
                 Disposition => 'attachment'
                 );
    # Send e-mail
    $msg->send();
} # end sub Mail_File
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top