Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Sending mail from perl script

by Anonymous Monk
on Apr 30, 2010 at 20:46 UTC ( [id://837849]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Need a suggestion. I wanted to have my script to be able to send mail to specified mailing address, I am in WIN32.Regarding this, I tried using NET::SMTP, but NET::SMTP couldn't help me much. I finally got it using MIME::LITE but here is few observation: Got this earlier node on the same subject http://www.perlmonks.org/?node_id=622121

Even using MIME::LITE it's not that straight forward.

1. it has a dependency, you have to have module "Email::Date::Format" for MIME::LITE to work.

2. If you are passing the mail through an external program like "sendmail" then OK ... otherwise if you are using a SMTP server,got to authenticate yourself to send the mail you need again 2 other modules "MIME::BASE64" and "AUTHEN::SASL".

For my case, I have tp pass through the SMTP server, authenticating it. Is there any other way, I can get around this without using MIME::LITE. please suggest.

Thanks.

Replies are listed 'Best First'.
Re: Sending mail from perl script
by Corion (Patriarch) on Apr 30, 2010 at 22:11 UTC
      Corion,

      Thanks for your reply. Yes, installing modules is not a big deal but I don't want to use those many modules to get it worked. Actually, script is goping to run on a server and I don't have privilege to install module there ... So, you can imagine, I have to run behind adimin guy to get those many module installed (need to answer many Q's). That's why trying to avoid. Let's see if I can find out some other way to go.

        You want to use local::lib along with cpanminus to install CPAN modules into your home directory. And then use local::lib in your script to access those modules.

Re: Sending mail from perl script
by camenix (Acolyte) on May 01, 2010 at 03:25 UTC

    If you want to send mail without external module,you'll need more efforts.Net::SMTP is over powerful.If you know more details about email format,it works for you.

    To send a mail,Net::SMTP is the only module you need.Open your outlook express(or any other mail clients),drag an email to the desktop,then open the eml file by notepad.

    Send the email from your script based on the format of the eml file's content.

    The following codes show how to send a Base64 encoding letter with html format.It works on XP and Vista.

    The MIME::Base64 is not necessary according to your email client's encoding. If you use basic authorization,AUTHEN::SASL is also unnecessary.

    # These codes are written in 2003 package Mnsend; use Net::SMTP; use MIME::Base64; use strict; ### Modify email address and password first ############# ### at line like: $Smtp->auth('vvvvvv@gggg.com','yyyyyyy'); # # SendAlert(HP<hp@163.com>,'Example','<html>hello,world!</html>','a@16 +3.com,b@163.com'); ########################################################## # Encoding head,if your mother language is English # replace "=?GB2312?B?".$name."?=" with $name sub headerconvert{ my $name=shift; $name = encode_base64($name); chomp($name); return $name = "=?GB2312?B?".$name."?="; } sub mailheaderconvert{ my $name=shift; my ($first,$second)=($name=~m/^(.+)<(.+)>/); $first=headerconvert($first); $name="\"$first\"<$second>"; } sub SendAlert { my( $name, $subname, $Message, $List ) = @_; # rewrite message here $name = mailheaderconvert($name); my $alert = headerconvert($subname); # The html body is same as text,it is just a example. # In real world,$htmlmsg may be '<html><b>hello,world</b></html>' # the $testmsg may be 'hello,world!' my $textmsg = encode_base64($Message); my $htmlmsg = encode_base64($Message); foreach my $Recipient ( @$List ) { my $Smtp; my( $Host ) = ( $Recipient =~m/\@(.*)/ ); #if your mail address suffix is not same as your mail server n +ame #uncomment code below #my $Host = "your mail server name"; #print "Sending message to $Recipient...at $Host..."; if( $Smtp = new Net::SMTP( $Host ) ) { # modify the email address and password at your will. $Smtp->auth('vvvvvv@gggg.com','yyyyyyy'); $Smtp->mail($ENV{USER}); #print $Smtp->domain,"\n"; #begin mail $Smtp->mail( "Perl Pager" ); $Smtp->to( $Recipient ); #only for address $Smtp->data(); #$Smtp->datasend( "Date: Tue, 15 Jul 2003 15:32:19 +0800\n +" ); my @body=@{&getbody($name,$Recipient,$alert,$textmsg,$html +msg)}; foreach my $bodyline (@body){ $Smtp->datasend($bodyline); } $Smtp->dataend(); $Smtp->quit(); } else { print "failed\n"; `net send localhost "Mnsend"`; return undef; } print "success\n"; } return 1; } sub getbody { my @body=undef; my ($name,$Recipient,$alert,$textmsg,$htmlmsg)=@_; $body[0]="From: $name\n"; $body[1]="To: $Recipient\n"; $body[2]="Subject: $alert\n"; $body[3]="MIME-Version: 1.0\n"; $body[4]="Content-Type: multipart/alternative;\n"; $body[5]=" boundary=\"----=_NextPart_000_0017_01C34 +AE6.494814A0\"\n"; $body[6]="X-Priority: 3\n"; $body[7]="X-MSMail-Priority: Normal\n"; $body[8]="X-Mailer: Microsoft Outlook Express 5.50.4522.12 +00\n"; $body[9]="X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4 +522.1200\n"; $body[10]="\n"; $body[11]="This is a multi-part message in MIME format.\n" +; $body[12]="\n"; $body[13]="------=_NextPart_000_0017_01C34AE6.494814A0\n"; $body[14]="Content-Type: text/plain;\n"; $body[15]=" charset=\"gb2312\"\n"; $body[16]="Content-Transfer-Encoding: base64\n"; $body[17]="\n"; $body[18]="$textmsg\n"; $body[19]="------=_NextPart_000_0017_01C34AE6.494814A0\n"; $body[20]="Content-Type: text/html;\n"; $body[21]=" charset=\"gb2312\"\n"; $body[22]="Content-Transfer-Encoding: base64\n"; $body[23]="\n"; $body[24]="$htmlmsg\n"; $body[25]="\n"; $body[26]="------=_NextPart_000_0017_01C34AE6.494814A0--\n +"; $body[27]="\n"; return \@body; } 1;
    Good luck to you.
Re: Sending mail from perl script
by sabari (Beadle) on May 01, 2010 at 06:28 UTC
    my suggestion is do some work around in Telnet modules ( my self needs to test this ,but i have tested in manual telnet command todo listing of mails etc .. )
    Best Regards, S.Sabarinathan,
      Please no. The op wants to avoid installing modules/talking with admin, which is about 100000 times easier than what you suggest, the op just isn't up to it.
      Normal Net::Telnet and below Commands can help you post a mail without any other modules . smtp protocol command : http://the-welters.com/professional/smtp.html Testing I did is , put the telnet to my mail server SMTP port Connected to mail.bksys.co.in Escape character is '^]'. 220 mail.bksys.co.in ESMTP Postfix (Debian/GNU) HELO localhost 250 mail.bksys.co.in MAIL FROM:sabarinathan@bksys.co.in 250 2.1.0 Ok RCPT To:sabarinathan@bksys.co.in 250 2.1.5 Ok DATA 354 End data with <CR><LF>.<CR><LF> This is test mail by Telnet via SMTP . 250 2.0.0 Ok: queued as EC1BD1DC002 VRFY sabarinathan@bksys.co.in 252 2.0.0 sabarinathan@bksys.co.in
      Best Regards, S.Sabarinathan,
        Normal Net::Telnet and below Commands can help you post a mail without any other modules .
        smtp protocol command :
        
        http://the-welters.com/professional/smtp.html
        
        Testing I did is ,
        
        put the telnet to my mail server SMTP port
        
        Connected to mail.bksys.co.in
        Escape character is '^]'.
        220 mail.bksys.co.in ESMTP Postfix (Debian/GNU)
        HELO localhost
        250 mail.bksys.co.in
        MAIL FROM:sabarinathan@bksys.co.in
        250 2.1.0 Ok
        RCPT To:sabarinathan@bksys.co.in
        250 2.1.5 Ok
        DATA
        354 End data with <CR><LF>.<CR><LF>
        This is test mail by Telnet via SMTP
        .
        250 2.0.0 Ok: queued as EC1BD1DC002
        VRFY sabarinathan@bksys.co.in
        252 2.0.0 sabarinathan@bksys.co.in
        
        
        
        Best Regards, S.Sabarinathan,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://837849]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-25 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found