As Perl programmer we might want to shoot off a mail for some reason or the other, especially if we've used Perl for the web. Perl being such a powerful language, it's can do virtually anything you want to do with it. Sending a mail is a piece of cake, be it using sendmail or using SMTP. CPAN is a vast reposiroty of modules, modules for every task you might be able to think.
Sendmail [www.sendmail.org] is a mail transfer agent that is a well-known project of the open source, free software and Unix communities, which is distributed both as free software and proprietary software, you can find it on most Unix/Linux installations.
Here's an example of sending mails with sendmail.
That's it. Wasn't that easy!! Let's make it easier by using a Perl module Email::Send::Sendmail
Most of you must be knowing what SMTP is, is a protocol for sending mails, read more here.
For sending mails using SMTP we'll use a very popular Perl module Net::SMTP.
So, I hope it was helpful, some of you might want to checkout the module Net::SMTP_auth which helps you autheticate to the SMTP server before sending any mails, most SMTP servers today aren't open relays.
Sending Mails using Sendmail
Sendmail [www.sendmail.org] is a mail transfer agent that is a well-known project of the open source, free software and Unix communities, which is distributed both as free software and proprietary software, you can find it on most Unix/Linux installations.
Here's an example of sending mails with sendmail.
Code: Perl
#!/usr/bin/perl
$path_to_sendmail = '/usr/sbin/sendmail -t'; # this is the usual location of the sendmail program,
# see if your's is somewhere else
open MAIL,"|$path_to_sendmail"; # open a pipe to the program
print MAIL q(From: deepz@go4expert.com
To: HeyYou@ThatDomain.com
Subject: Whats up?
See, I mailed you through my program.
);
close MAIL;
That's it. Wasn't that easy!! Let's make it easier by using a Perl module Email::Send::Sendmail
Code: Perl
use Email::Send;
my $message = q(From: deepz@go4expert.com
To: HeyYou@ThatDomain.com
Subject: Whats up?
See, I mailed you through my program.
);
Email::Send->new({mailer => 'Sendmail'})->send($message);
Sending Mails Using SMTP
Most of you must be knowing what SMTP is, is a protocol for sending mails, read more here.
For sending mails using SMTP we'll use a very popular Perl module Net::SMTP.
Code: Perl
#!/usr/bin/perl
use Net::SMTP;
$smtp = Net::SMTP->new('email.go4expert.com');
$smtp->mail('deepz@go4exper.com');
$smtp->to('HeyYou@ThatDomain.com');
$smtp->data();
$smtp->datasend('To: HeyYou@ThatDomain.com');
$smtp->datasend('Subject: Whats up?');
$smtp->datasend("\n");
$smtp->datasend("See, I mailed you through my program.\n");
$smtp->dataend();
$smtp->quit;
So, I hope it was helpful, some of you might want to checkout the module Net::SMTP_auth which helps you autheticate to the SMTP server before sending any mails, most SMTP servers today aren't open relays.


