Thursday, September 06, 2012

Sample Perl Script Mail Send (SMTP) Example For Linux

6 comments
Here is an example perl script to send email from a Linux box.

VERY IMPORTAT: @ symbol in this file MUST be escaped as \@.
for e.g. if you want to hardcode an email id say test@mydomain.com, the email id must be specified as test\@mydomain.com

disclaimer: I have been using this on CentOS 5 systems with perl v5.8.8.

#!/usr/bin/perl
#######################################################
#######################################################
#### The parameters in this file are written by   #####
#### script Createmailscriptattach                #####
#### This program takes 4 arguments               #####
#### 1) Toaddress ,2)FromAddress,3) message       #####
#### 4)subject 5)cc                               #####
#######################################################
#######################################################
use MIME::QuotedPrint;
use MIME::Base64;
use Net::SMTP;

$authenabled = 1;
$mailserver = "mail.mymailserver.com";
$username = "useridforsmtpauthentication";
$password = "passwordforauthentication";
$localhostname = "myfqdn";
$port = 25;
$time = `date`;

$to = $ARGV[0];
$from  = $ARGV[1];


$message  = $ARGV[2];
$sub  = $ARGV[3];
$cc= $ARGV[4];



$smtp = Net::SMTP->new("$mailserver",
                    Hello => "$localhostname",
                    Timeout => 60,
                Port => "$port",
                        Debug => 1);

if ( $authenabled eq 1 )
{
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();

$smtp->datasend(encode_base64("$username") );
$smtp->response();

$smtp->datasend(encode_base64("$password") );
$smtp->response();
}

$smtp->mail("$from");
$smtp->to("$to");
if ($cc ne "") {
$smtp->cc("$cc");
}

$smtp->data();
$smtp->datasend("From: $from\n");
$smtp->datasend("To: $to\n");
if ($cc ne "") {
$smtp->datasend("CC: $cc\n");
}
$smtp->datasend("Subject: $sub\n");
$smtp->datasend("Date: $time\n");

$smtp->datasend("\n");
$smtp->datasend("$message");
$smtp->dataend();
$smtp->quit();

6 comments:

Anonymous said...

This page definitely has all of the information I wanted concerning this subject and didn't know who to ask.

Feel free to visit my web blog - password recovery

Anonymous said...

Just about every SelectTech 552 dumbbell adjusts to fifteen unique pounds options from 5 pounds to 52.


Feel free to surf to my web site: bowflex adjustable dumbbells

Anonymous said...

Healthful Food items - I Remarkably advise on the lookout into Shakeology to be a meal
replacement through the plan.

Feel free to surf to my homepage ... Full Statement

Anonymous said...

The built-in rowing device operates pretty easily, functions a 5-position foot
harness and is particularly primarily proficient for cardiovascular warm-up and
cool-down.

Also visit my blog post: bowflex selecttech 552

Anonymous said...

I drop a comment each time I appreciate a post on a blog
or if I have something to valuable to contribute to the conversation.
Usually it's caused by the fire communicated in the post I looked at. And after this post "Sample Perl Script Mail Send (SMTP) Example For Linux". I was moved enough to create a thought ;) I do have a few questions for you if it's okay.

Could it be simply me or do a few of these remarks look like left by brain dead people?

:-P And, if you are writing at additional online social sites, I would like to follow
you. Would you make a list all of all your public pages like your
twitter feed, Facebook page or linkedin profile?


Feel free to surf to my webpage XXX Video Fix

Anonymous said...

This design is wicked! You obviously know how to keep a reader entertained.

Between your wit and your videos, I was almost moved to start
my own blog (well, almost...HaHa!) Wonderful job. I really loved what you had to say, and more than
that, how you presented it. Too cool!

Feel free to visit my weblog; psn card generator