Monday, July 20, 2009

Send Email from perl script

Sometimes we do not have enough privileges to install perl modules on the host system and we have to work with ideal installation of perl. In that case, simpler way to send emails using perl is to use sendmail command. Following function can be used to send emails in perl.

sub sendEmail
{
($from, $to, $subject, $message) = @_;

my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}

above function can be used as below:

sendEmail("vishnu\@test.com", "vishnu\@mytest.com", "Test sendEmail.", "Testing sendEemail function.");

No comments: