Quick Search for:  in language:    
GUI,simple,PerlTk,application,will,send,Email
   Code/Articles  |  Newest/Best  |  Community  |  Jobs  |  Other  |  Goto  | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 24. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Perl.
Click here to see a screenshot of this code!CGIScripter
By David Simpson on 11/24

(Screen Shot)

Calender
By Jeff Mills on 11/20


quikpoll
By Jeff Mills on 11/20


Encrypt Password
By Jeff Mills on 11/20


Rock, Paper, Scissors w/ GUI
By Kurt Rudolph on 11/19


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Sending Email with Perl/Tk

Print
Email
 
VB icon
Submitted on: 9/17/2003 6:35:45 PM
By: Randy McCleary 
Level: Intermediate
User Rating: By 8 Users
Compatibility:5.0 (all versions), Active Perl specific

Users have accessed this code 1631 times.
 
(About the author)
 
     This is a simple Perl/Tk application that will send Email using the Net::SMTP module. So if you ever wondered about Creating a GUI to send email, then check this out.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    =**************************************
    = Name: Sending Email with Perl/Tk
    = Description:This is a simple Perl/Tk a
    =     pplication that will send Email using th
    =     e Net::SMTP module. So if you ever wonde
    =     red about Creating a GUI to send email, 
    =     then check this out.
    = By: Randy McCleary
    =
    =This code is copyrighted and has    = limited warranties.Please see http://w
    =     ww.Planet-Source-Code.com/vb/scripts/Sho
    =     wCode.asp?txtCodeId=540&lngWId;=6    =for details.    =**************************************
    
    #!/usr/bin/perl -w
    #######################################################
    ## This is a simple Perl/Tk application that will send
    ## Email using a visual GUI Form and SMTP.
    #######################################################
    use strict;
    use Tk;
    use Tk::Dialog;
    use Net::SMTP;
    #############################################
    ## Here is where we set will the Constant 
    ## values, that are used to send the Email
    ## $MailHost - HostName or IP Address
    ## $FromEmail - Email address sending from
    ## $FromName - Name sending from, will show
    ## in Inbox as the from name.
    #############################################
    my $MailHost = 'mailhost';
    my $FromEmail = 'my_email_address@yahoo.com';
    my $FromName = 'Email Tester';
    my $frmMain = MainWindow->new;
    $frmMain->title("SendEmail");
    $frmMain->geometry("525x441+8+8");
    ######################################
    ## Create the Labels and the Text
    ## Entries.
    ######################################	
    my $lblTo = $frmMain->Label(
    				-anchor => "w", 
    				-text => "To:", 
    				-background => "#D4D0C8", 
    				-cursor => "", 
    				-font => "Tahoma 10 bold", 
    				-foreground => "#140F7B"
    				);
    my $lblSubject = $frmMain->Label(
    				-anchor => "w", 
    				-text => "Subject:", 
    				-background => "#D4D0C8", 
    				-cursor => "", 
    				-font => "Tahoma 10 bold",
    				-foreground => "#140F7B"
    				);
    my $txtTo = $frmMain->Entry(
    				-borderwidth => 1, 
    				-cursor => "", 
    				-font => "Tahoma 8 normal", 
    				-foreground => "#000000", 
    				-relief => "sunken"
    				);
    my $txtSubject = $frmMain->Entry(
    				-borderwidth => 1, 
    				-cursor => "", 
    				-font => "Tahoma 8 normal", 
    				-foreground => "#000000", 
    				-relief => "sunken"
    				);
    my $txtBody = $frmMain->Text(
    				-borderwidth => 1, 
    				-font => "Tahoma 8 normal", 
    				-relief => "solid"
    				);
    ######################################
    ## Place the Labels and Text Entries
    ## on the form.
    ######################################	
    $lblTo->place(
    		 -width => 96, 
    		 -height => 24, 
    		 -x => 40, 
    		 -y => 32
    		 );
    $lblSubject->place(
    		 -width => 96, 
    		 -height => 24, 
    		 -x => 40, 
    		 -y => 64
    		 );
    $txtTo->place(
    		 -width => 264, 
    		 -height => 24, 
    		 -x => 144, 
    		 -y => 32
    		 );
    $txtSubject->place(
    		 -width => 264, 
    		 -height => 24, 
    		 -x => 144, 
    		 -y => 64
    		 );
    $txtBody->place(
    		 -width => 432, 
    		 -height => 256, 
    		 -x => 40, 
    		 -y => 128
    		 );
    ###################################
    ## Create the command buttons
    ###################################
    my $cmdSend = $frmMain->Button(
    				-activebackground => "#FFFCBF", 
    				-activeforeground => "#E30229", 
    				-background => "#FFFFFF", 
    				-borderwidth => 1, 
    				-text => "Send",
    				-command => sub{SendMail()}, 
    				-cursor => "", 
    				-font => "Tahoma 8 bold", 
    				-foreground => "#140F7B", 
    				-relief => "solid"
    				);
    my $cmdClear = $frmMain->Button(
    				-activebackground => "#FFFCBF", 
    				-activeforeground => "#E30229", 
    				-background => "#FFFFFF", 
    				-borderwidth => 1, 
    				-text => "Clear", 
    				-command => sub{Clear()},
    				-cursor => "", 
    				-font => "Tahoma 8 bold", 
    				-foreground => "#140F7B", 
    				-relief => "solid"
    				);
    my $cmdExit = $frmMain->Button(
    				-activebackground => "#FFFCBF", 
    				-activeforeground => "#E30229", 
    				-background => "#FFFFFF", 
    				-borderwidth => 1, 
    				-text => "Exit", 
    				-cursor => "", 
    				-font => "Tahoma 8 bold", 
    				-command => [$frmMain => 'destroy'],
    				 -foreground => "#140F7B", 
    				-relief => "solid"
    				);
    ######################################
    ## Place the command buttons
    ######################################	
    $cmdSend->place(
    		 -width => 96, 
    		 -height => 32, 
    		 -x => 88, 
    		 -y => 400
    		 );
    $cmdClear->place(
    		 -width => 96, 
    		 -height => 32, 
    		 -x => 200, 
    		 -y => 400
    		 );
    $cmdExit->place(
    		 -width => 96, 
    		 -height => 32, 
    		 -x => 312, 
    		 -y => 400
    		 );
    MainLoop;
    ############################ SUBS ################################
    ##################################################################
    ## Sub: Calculate
    ## Description: This sub will clear all the Text Entries
    ##################################################################
    sub Clear {
    	$txtTo->delete(0, 'end');
    	$txtSubject->delete(0, 'end');
    	$txtBody->delete('1.0', 'end');
    }
    ##################################################################
    ## Sub: SendMail
    ## Description: This sub will get all the input data and Sends 
    ## out the email message. It also checks to make sure your
    ## sending to a valid e-mail address and shows dialogs.
    ##################################################################
    sub SendMail {
    	my $ToEmail = $txtTo->get;
    	my $Subject = $txtSubject->get;
    	my $Body = $txtBody->get('1.0', 'end');
    	my $smtp = Net::SMTP->new($MailHost);
    	my $dialog = '';
    	if (IsEmail($ToEmail) == 1) {
    		$smtp->mail($FromEmail);
    		$smtp->to($ToEmail);
    		$smtp->data();
    		$smtp->datasend("To: $ToEmail\n");
    		$smtp->datasend("Subject: $Subject\n");
    		$smtp->datasend("From: $FromName <$FromEmail> \n");
    		$smtp->datasend("$Body\n\n\n");
    		$smtp->datasend("Message Sent by Perl Application\n");
    		$smtp->dataend();
    		$smtp->quit();
    		$dialog = $frmMain->Dialog(
    					 -title => "SendEmail: Message Sent",
    					 -text => "The message was successfully sent to $ToEmail",
    					 -buttons => ["OK"]);
    		$dialog->Show();
    	}
    	else {
    		$dialog = $frmMain->Dialog(
    					 -title => "SendEmail: Message Not Sent",
    					 -text => "You must specify a Valid Email address to send to.\n",
    					 -buttons => ["OK"]);
    		$dialog->Show();
    	}
    }
    ##################################################################
    ## Sub Name: IsEmail.
    ## Description: This sub validates the input to check to see if
    ## the input is a valid format of an e-mail address.
    ## Example: webmaster@msn.com.
    ##################################################################
    sub IsEmail {
    	my $InputString = shift;
    	if ($InputString =~ /^[\w.]+@\w[\w.-]+\w\.[A-Za-z]{2,4}$/) {
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }	
    		


Other 28 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/24/2003 11:25:02 PM:
Wonderful! Thanks. Finally something 
that does what it says it will do.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.