1. Simple scripts to sent an email
#!C:/Perl/bin/perl.exe -w
# Load the related modules:
use Net::SMTP;
# Important ..
use warnings;
use strict;
use Data::Dumper;
use vars qw($mail_from $smtp $mail_to $my_host $smtp);
# The related parameters:
$mail_from = 'schrodinger@sympatico.ca';
$mail_to = 'schrodinger@sympatico.ca';
$my_host ='smtphm.sympatico.ca';
# The useful functions
$smtp=Net::SMTP->new($my_host, Debug => 10) || die "Error: $!";
$smtp->mail($mail_from);
$smtp->to($mail_to);
# The other functions:
$smtp->data();
#Start the data sending
$message = 'Best of luck ..';
#The headers for the e-mail
#(newline character for end of headers):
$smtp->datasend("From: $from\n");
$smtp->datasend("To: $to\n");
$smtp->datasend("Subject: $subj\n");
$smtp->datasend("\n");
$smtp->datasend("$message");
$smtp->dataend() || print "Sending data failed: $!";
$smtp->quit;
#Close the smtp connection
2. Using the prompt
#!C:/Perl/bin/perl.exe -w
use Net::SMTP;
use warnings;
use strict;
use Data::Dumper;
use warnings;
use strict;
use Data::Dumper;
use vars qw($mailhost $smtp $from $to $sub $subject $message );
$mailhost = 'smtp1.sympatico.ca';
$smtp = Net::SMTP->new($mailhost, Debug => 10) || die "Error: $!";
print "\nFrom:";
$from = ;
chomp $from;
print "\nTo:";
$to = ;
chomp $to;
print "\nSubject:\n";
$subject = ;
chomp $subject;
print "\nYour message:\n";
$message = ;
chomp $message;
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
#Start the data sending
# The headers for the e-mail.
$smtp->datasend("From: $from\n");
$smtp->datasend("To: $to\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
$smtp->datasend("$message");
$smtp->dataend() || print "Dataend failed: $!";
#End the data sending
$smtp->quit;
# We can debug :
$smtp->debug(1);
Let's recall the chomp() function removes, from the end of a string,
any character that matches the current value of $/ (input record
separator, which is the default to a newline.)
This function chomp() is useful when reading data from
a file or from a user. When reading user input from the
standard input stream (STDIN), we set a newline character with
each line of data.
3. Quick scripts to send email
# --- If not already installed, download the Mail::Sendmail module:---
#!c:/Perl/bin
use Mail::Sendmail;
%mail = ( smtp => 'smtphm.sympatico.ca',
To => 'schrodinger@sympatico.ca',
From => 'schrodinger@sympatico.ca',
Message => "Message test using Perl and Mail_SendMail package .."
);
if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $Mail::Sendmail::error \n" }
# --- If not already installed, download the Mail::Sender module:---
#!C:/Perl/bin/perl.exe -w
use Mail::Sender;
$sender = new Mail::Sender {smtp => 'smtphm.sympatico.ca',
from => 'schrodinger1@sympatico.ca'};
$sender->MailFile({to => 'schrodinger2@sympatico.ca',
subject => 'Contact with attached file ..',
msg => "Astronauts walked on the moon in 1969.",
file => 'moon.txt'});
4. email with attachment
# --- If not already installed, download the MIME::Lite module:---
#!C:/Perl/bin/perl.exe -w
use MIME::Lite;
$to = "schrodinger\@sympatico.ca";
$from = "schrodinger\@sympatico.ca";
$subject = "Contact";
$message = "Astronauts walked on the moon in 1969 ..";
$file = "schrodinger.txt";
# send email
send_email($to, $from, $subject, $message, $file);
# email function
sub send_email
{
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;
# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message
);
# add the attachment
$msg->attach(
Type => "text/plain",
Path => $file,
Filename => $file,
Disposition => "attachment"
);
# send the email
MIME::Lite->send('smtp', 'smtphm.sympatico.ca', Timeout => 10) || die "Error: $!";
$msg->send();
|