×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Sending email

How do I send email without using UNIX sendmail? by MikeLacey
Posted: 10 Feb 02 (Edited 15 Nov 05)

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: $!";

    $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();
}

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

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close