Important alert: (current site time 7/15/2013 8:38:42 PM EDT)
 

VB icon

Sending Email with Perl/Tk

Email
Submitted on: 9/17/2003 6:35:45 PM
By: Randy McCleary 
Level: Intermediate
User Rating: By 12 Users
Compatibility: 5.0 (all versions), Active Perl specific
Views: 20080
(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 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.
= By: Randy McCleary
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.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 tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

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.
(If this comment was disrespectful, please report it.)

 
2/28/2005 11:27:14 AMAhmed Wageeh El-Hamouly

Great Work, it works perfect. i just have a suggestion. when you press on send, it usually takes a while, and so the button stays down. maybe you can put it in a thread so the button wont like be stuck.
(If this comment was disrespectful, please report it.)

 
4/10/2006 4:35:01 AMhari

the code is great!!! but i just met with an issue. this same code when executed from a client machine displays the compose window on the server, not at the client.

plz help....
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.