Perl 简明教程

Perl - Sending Email

Using sendmail Utility

Sending a Plain Message

If you are working on Linux/Unix machine then you can simply use sendmail utility inside your Perl program to send email. Here is a sample script that can send an email to a given email ID. Just make sure the given path for sendmail utility is correct. This may be different for your Linux/Unix machine.

#!/usr/bin/perl

$to = 'abcd@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

open(MAIL, "|/usr/sbin/sendmail -t");

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent Successfully\n";

Actually, the above script is a client email script, which will draft email and submit to the server running locally on your Linux/Unix machine. This script will not be responsible for sending email to actual destination. So you have to make sure email server is properly configured and running on your machine to send email to the given email ID.

Sending an HTML Message

If you want to send HTML formatted email using sendmail, then you simply need to add Content-type: text/html\n in the header part of the email as follows −

#!/usr/bin/perl

$to = 'abcd@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = '<h1>This is test email sent by Perl Script</h1>';

open(MAIL, "|/usr/sbin/sendmail -t");

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Content-type: text/html\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent Successfully\n";

Using MIME::Lite Module

If you are working on windows machine, then you will not have access on sendmail utility. But you have alternate to write your own email client using MIME:Lite perl module. You can download this module from MIME-Lite-3.01.tar.gz and install it on your either machine Windows or Linux/Unix. To install it follow the simple steps −

$tar xvfz MIME-Lite-3.01.tar.gz
$cd MIME-Lite-3.01
$perl Makefile.PL
$make
$make install

That’s it and you will have MIME::Lite module installed on your machine. Now you are ready to send your email with simple scripts explained below.

Sending a Plain Message

Now following is a script which will take care of sending email to the given email ID −

#!/usr/bin/perl
use MIME::Lite;

$to = 'abcd@gmail.com';
$cc = 'efgh@mail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );

$msg->send;
print "Email Sent Successfully\n";

Sending an HTML Message

If you want to send HTML formatted email using sendmail, then you simply need to add Content-type: text/html\n in the header part of the email. Following is the script, which will take care of sending HTML formatted email −

#!/usr/bin/perl
use MIME::Lite;

$to = 'abcd@gmail.com';
$cc = 'efgh@mail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = '<h1>This is test email sent by Perl Script</h1>';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );

$msg->attr("content-type" => "text/html");
$msg->send;
print "Email Sent Successfully\n";

Sending an Attachment

If you want to send an attachment, then following script serves the purpose −

#!/usr/bin/perl
use MIME::Lite;

$to = 'abcd@gmail.com';
$cc = 'efgh@mail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Type     => 'multipart/mixed'
                 );

# Add your text message.
$msg->attach(Type         => 'text',
             Data         => $message
             );

# Specify your file as attachement.
$msg->attach(Type         => 'image/gif',
             Path         => '/tmp/logo.gif',
             Filename     => 'logo.gif',
             Disposition  => 'attachment'
            );
$msg->send;
print "Email Sent Successfully\n";

You can attach as many files as you like in your email using attach() method.

Using SMTP Server

If your machine is not running an email server then you can use any other email server available at the remote location. But to use any other email server you will need to have an id, its password, URL, etc. Once you have all the required information, you simple need to provide that information in send() method as follows −

$msg->send('smtp', "smtp.myisp.net", AuthUser=>"id", AuthPass=>"password" );

You can contact your email server administrator to have the above used information and if a user id and password is not already available then your administrator can create it in minutes.