Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to send mail from Perl/Tk Program?

by mikasue (Friar)
on Apr 10, 2007 at 12:50 UTC ( #609124=perlquestion: print w/ replies, xml ) Need Help??
mikasue has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

Is it possible to send mail from a standalone Perl/Tk program? I looked into sendmail and MIME::Lite but it seems these are for cgi scripts sending mail from a webserver. I want to send a test email from a Perl/Tk program when a button is pressed.

Suggestions?

THANKS!

Comment on How to send mail from Perl/Tk Program?
Re: How to send mail from Perl/Tk Program?
by Corion (Pope) on Apr 10, 2007 at 12:52 UTC

    What part of the MIME::Lite documentation makes you think it is for CGI scripts only? I use MIME::Lite in scripts that send mail quite often, and well outside any webbish environment.

      Oh I see. However when I run the following code:
      use strict; use warnings; use Tk; use MIME::Lite; my $MW = MainWindow->new(-background=>'white'); $MW->geometry('400x350+250+100'); my $msg = MIME::Lite->new( From =>'youremail@netzero.com', To =>'youremail@email.com', Subject =>'Helloooooo, USER!', Data =>"This is a test email from my Perl/Tk prog +ram. Enjoy!" ); $MW->Button(-text=>'Test Email to Parent', -background=>'green', -font +=>'Arial 14', -foreground=>'blue', -command=>sub{$msg->send;} )->pack +(-side=>'left', -anchor=>'w', -padx=>'50'); MainLoop;
      I get the following error message:

      'sendmail' is not recognized as an internal or external command, operable program or batch file.

      I thought 'sendmail' was a unix program. I am on a Windows system.

      Thanks

        Maybe you want to change how messages are sent?

        You will still need a working SMTP server that is willing to accept your email - most likely your ISP provides one for you.

        You'll need to change that, then :-) Further down in the doc, there's this section:
        Change how messages are sent ### Do something like this in your 'main': if ($I_DONT_HAVE_SENDMAIL) { MIME::Lite->send('smtp', "smtp.myisp.net", Timeout=>60); } ### Now this will do the right thing: $msg->send; ### will now use Net::SMTP as shown above
        Hope that points you in the right direction :-)

        Update: Wow. Two other answers as I was typing up this one :-D

        on windows there is no sendmail program(unless you're
        on cygwin but probably you're not)
        use Email::Send
        for sending email ,it should work ok.

        And what if I'm looking for a different approach?

        I want a link that do the same as sendto:mymail@server.com. In other words, that call the standard mail program of the user.

        thanks,

        memo.garciasir@gmail.com

Re: How to send mail from Perl/Tk Program?
by Bro. Doug (Monk) on Apr 10, 2007 at 13:15 UTC
    mikasue,

    I often use MIME::Entity (I don't even have MIME::Lite installed). If you want to read up on MIME::Entity, you should start with MIME::Tools.

    Along with MIME::Lite, you should be able to use this to send your email.

    You may also investigate Mail::Box for more email related tools.

    Peace monks,
    Bro. Doug :wq
      It sounds like I will need to include a User Email Setup screen in my program to ask the user for his/her smtp server information. Am I on track now?

      Thanks!

        Am I on track now?

        You sure are. And, as an unsolicited tip, when you're popping windows in perlTk, try to use the same windows over and over, hiding and restoring windows as needed, changing names and labels as needed. I've found that perlTk program footprints in memory will grow as you Create new windows, which becomes a problem if the program runs too long.

        Maybe one of the other monks knows a good way to address this problem. delete() and undef() haven't cut it for me.

        Enjoy!
        Bro. Doug :wq

        SMTP is fine, as long as you can safely assume your users will all have access to it. Even when it's available, it may not be the user's choice. I, for one (and I believe a lot of users feel the same way) would prefer to use my web-based email account. If you can add that as a configuration option, that would be ideal. You could probably leverage modules such as Mail::Client::Yahoo and WWW::Hotmail to handle the nasty bits for you.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: How to send mail from Perl/Tk Program?
by CaMelRyder (Pilgrim) on Apr 10, 2007 at 16:40 UTC
    I use Net::SMTP. The following block of code sends an email:
            $smtp = Net::SMTP->new($smtp_server_addr) or print $@;
            $smtp->mail($fromAddress);
            $smtp->to($toAddress);
            $smtp->data();
            $smtp->datasend("To: God\n");
            $smtp->datasend("Subject: Thanks for perl\n");
            $smtp->datasend("\n");
            #@body is list of strings(lines of message)
            $smtp->datasend(\@body);  
            $smtp->dataend();
            $smtp->quit
    
    
    
    
    ¥peace from CaMelRyder¥
      $smtp->datasend("To: God\n"); $smtp->datasend("Subject: Thanks for perl\n");

      I would say "To: $Larry\n". But $Larry himself would probably agree with you. After all, thanks to interpolation, it can be both, and much more!

      CaMelRyder, What if the outgoing server requires authentication? How do you authenticate with Net::SMTP? Thanks,
Re: How to send mail from Perl/Tk Program?
by apl (Monsignor) on Apr 11, 2007 at 16:46 UTC
    sendmail is not restricted to cgi scripts. I use it regularly in Unix daemons to let me know the state of various end of day processing.
Re: How to send mail from Perl/Tk Program?
by Anonymous Monk on Apr 12, 2007 at 04:21 UTC
    Have you considered Mail::Send and Mail::Mailer? They might meet your needs.
      use strict; use Tk; use Tk::Animation; use Tk::LabEntry; use Tk::Dialog; use Net::SMTP; # main window my $mw = MainWindow->new(-title=>'SendMail Easy'); $mw->maxsize(qw/450 530/); $mw->minsize(qw/450 530/); my $fmain = $mw->Frame( -foreground=>'white',-background => 'ivory2', +-relief=> 'groove', -borderwidth=>2 )->pack( -side => 'top'); my $lbtop = $fmain->Label(-text => "SendMail Easy\n", -foreground => 'black', -background => 'ivory2')->pack(-side=>'top'); # from to my ($from, $to, $subj); my $fr1 = $fmain->Frame( -background => 'gray', -relief=> 'groove', -b +orderwidth=>2 )->pack( -side => 'top'); $fr1->LabEntry(-label=>'From:',-textvariable=>\$from,-labelPack=>[-sid +e=>'left'],-width=>30, -foreground => 'blue',-background => 'white')->pack(); $fr1->LabEntry(-label=>' To :',-textvariable=>\$to,-labelPack=>[-side +=>'left'],-width=>30, -foreground => 'blue',-background => 'white')->pack(); my $txtsubj = $fr1->LabEntry(-label=>'Subj:',-textvariable=>\$subj,-la +belPack=>[-side=>'left'],-width=>30, -foreground => 'blue',-background => 'white')->pack(); # ,-textvariable=>'perlocean@gmail.com' # compose my $fr2 = $fmain->Frame( -background => "ivory2", -relief => +'groove', -borderwidth=>2 )->pack( -side => 'top'); my $text = $fr2->Scrolled('Text', -scrollbars => 'osoe', -wrap => 'none', -foreground => 'blue', -background => 'white', -relief=>'flat')->pack(); # send button my $send = $fmain->Button( -text => 'Send eMail', -command => \&sendmail, -relief => 'groove', -foreground => 'black', -background => 'ivory2', )->pack(-ipadx=>10,-ipady=>5); $send->bind('<ButtonRelease-1>' => sub { $mw->messageBox(qw/-icon info -type OK -message/ => "eMail Sent... + \@njoy!"); }); # copyleft my $lbbottom = $mw->Label(-text => "Copyleft to Larry Wall", -foreground => 'black', -background => 'ivory2')->pack(-side=>'bottom'); ## animation my $camelanim = $mw->Animation('-format' => 'gif', -file => Tk->findINC('anim.gif') ); $mw->Label(-image => $camelanim)->pack(-side=>'bottom'); $camelanim->start_animation(200); MainLoop; sub sendmail{ my @message = $text-> get('1.0', 'end'); my $subject = $txtsubj -> get('1.0', 'end'); eval { my $cc = 'ccmail@domain.com'; my $smtp = Net::SMTP->new('smtp.server', Debug => 1) or die "connect"; $smtp->mail($from) or die "mail"; $smtp->to($to) or die "to"; # $smtp->to($cc) or die "cc"; $smtp->to($cc) or die "cc"; $smtp->data() or die "data"; $smtp->datasend($subject) or die "subject"; foreach my $line (@message) { $smtp->datasend("$line\n") or die "datasend"; } $smtp->dataend() or die "dataend"; $smtp->quit() or die "quit"; }; if ($@) { die "Message not sent: $@ failed\n"; } }
        This did not work for me. I got the following error.
        Tk::Error: wrong # args: should be ".frame.frame.labentry2.entry get" +at F:/Perl /lib/Tk.pm line 252. Tk callback for .frame.frame.labentry2.entry Tk::__ANON__ at F:/Perl/lib/Tk.pm line 252 Tk::Derived::Delegate at F:/Perl/lib/Tk/Derived.pm line 469 Tk::Widget::__ANON__ at F:/Perl/lib/Tk/Widget.pm line 322 main::sendmail at f:\BPP4kids\sendmail.pl line 63 Tk callback for .frame.button Tk::__ANON__ at F:/Perl/lib/Tk.pm line 252 Tk::Button::butUp at F:/Perl/lib/Tk/Button.pm line 111 <ButtonRelease-1> (command bound to event)

        All is not lost from the post though. I did learned how to include an animated gif in my program. Thanks!

Re: How to send mail from Perl/Tk Program?
by mikasue (Friar) on Apr 25, 2007 at 13:49 UTC
    Can you guys speak on authentication on the outgoing server? I have looked in the documentation and it doesn't speak on how to authenticate with a username and password for Mime::Lite and Net::SMTP. Thanks!
      The only mention of authentication that I have found is in the man page for Net::SMTP.
      
             auth ( USERNAME, PASSWORD )
                 Attempt SASL authentication.
      
      I don't know if this is the type of authentication that you are looking for but you can start there.
      
      ¥peace from CaMelRyder¥

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://609124]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-10-26 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls