Sending email from perl is relatively an easy exercise. To send text email, you just need to use Net::SMTP like the following:
use Net::SMTP;
sub sendmail($$@)
{
my $subj = shift;
my $msg = shift;
my $smtp = Net::SMTP->new($o_smtpsvr);
$subj = "Subject: $subj\n" unless $subj =~ /^Subject: /;
$smtp->hello('MyDomain');
$smtp->reset();
$smtp->help('');
$smtp->mail('noreply@xyz.com');
$smtp->to(@_);
$smtp->data($subj . $msg);
$smtp->dataend();
$smtp->quit;
}
sendmail('hello, world', 'my message', ('abc@xyz.com'));
|
|
If you wish to send HTML email instead, it gets a bit more complicated. The reason is that the message must be formatted in MIME. To do so, it's easier to use Email::MIME module from CPAN. Note that Email:MIME is not included with the standard perl distribution while Net::SMTP is.
use Email:MIME;
use Net::SMTP;
my $email_body =<<EOM;
<html>
<body>
Hello, world!
</body>
</html>
EOM
my $email = Email::MIME->create(
header_str => [
To => $email_to,
Cc => $email_cc,
From => $email_from,
Subject => $email_subject
],
body_str => $email_body,
attributes => {
content_type => 'text/html',
charset => 'utf8',
encoding => 'quoted-printable'
}
);
my $smtp = Net::SMTP->new($SmtpHostName);
$smtp->hello('MyDomain');
$smtp->reset();
$smtp->help('');
$smtp->mail($email_from);
$smtp->to($email_to);
$smtp->data($email->as_string);
$smtp->dataend();
$smtp->quit;
|
|
No comments:
Post a Comment