Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl send mail - windows TK

Status
Not open for further replies.

hessch

Technical User
Feb 8, 2010
5
0
0
CH
hello,

I have a problem with sends e-mail with “Lite” in an graphical interface with “TK”.

My program lists the file, in a file, all the files which are in the file as well as under files. This file in continuation is sent by smtp to the recipient who one wishes via the graphical interface.

code:

#!/usr/local/bin/perl

use Tk; #Utilisation de Tk
use MIME::Lite;
#use Net::SMTP;

#Création de la fenêtre principale
$fen = MainWindow->new();

#Définition de la taille min de la fenêtre
$fen->minsize('500','400');

#Définition du title de la fenêtre
$fen->title("Interface Tk avec Perl");

#Création de la barre de menu
$barre_menu = $fen->Frame(-relief => 'groove' , -borderwidth => 2);

#Création de la barre du menu général
$menu_gen = $barre_menu->Menubutton(
-text => 'Application' ,
-font => '{Garamond} 10',
-tearoff => 0 ,
-menuitems => [
[ 'command' ]
]
);

#Affichage du menu général à gauche
$menu_gen->pack(-side => 'left');

#Affichage de la barre de menu en haut de la fenêtre
$barre_menu->pack(-side => 'top', -anchor => 'n', -fill => 'x');

#Création de notre barre de statut à l'aide d'un Label
$barre_statut = $fen->Label(-relief => 'groove', -text => "Hello World", -font => '{Garamond} 10');

# nom
$n01 = 'Christian';
$n02 = 'Marc';
$n03 = 'Cyril';

#Création d'un label qui affichera le résultat de la sélection
$fen->Label(
-relief => 'groove',
-textvariable => $selectValue, #Variable contenant l'option dans la liste de sélection
-font => '{Garamond} 10'
)->pack(
-fill => 'both',
-padx => '5',
-pady => '5'
);
#Création de la liste de sélection
$fen->Optionmenu(
-options => [ $n01, $n02, $n03, ],
-command => sub {
#print "choix : ", @_, "n"
nom(@_);
}, #procédure de nom
-textvariable => $selectValue #Définition de la variable qui récupère la sélection
)->pack();

#Ajout de notre bouton à la fenêtre
$fen->Button(
-text => 'Send', #Texte du bouton
-font => '{Garamond} 10', #Nom et taille de la police utilisée
-command => &clic #Action qui modifie notre variable
)->pack(
-pady => '5', #Marge externe du bouton
-padx => '5',
-ipady => '5', #Marge interne du bouton
-ipadx => '5',
);

# Create three Radiobuttons in Frame widget $f1
# Link them using $favcolor
foreach (qw/vtx bluewin free/) {
$fen->Radiobutton(-text => $_, -variable => $choixsmtp,
-value => $_)->pack(-fill => 'x', -expand=>1, -anchor => 'w');
}

#On l'ajoute en bas de la fenêtre afin de lui donner l'effet barre de statut
$barre_statut->pack( -side => 'bottom', -fill => 'both');

MainLoop;

#Définition de notre fonction de fermeture de l'application
sub Quitter
{
exit(0);
}

#Définition de notre fonction de fermeture de l'application
sub nom
{
#print "choix : ", @_, "n";
$nom = @_[0];
#print "choix : $nom n";
#$barre_statut -> configure ( -text => "nom") ;
#$barre_statut -> configure ( -text => $nom) ;

if( $nom == $n01 )
{
$barre_statut -> configure ( -text => $nom)
}
elsif( $nom == $n02 )
{
$barre_statut -> configure ( -text => $nom)
}
elsif( $nom == $n03 )
{
$barre_statut -> configure ( -text => $nom)
}
else
{
print "choix : de condition inconnuen";
}
print "choixsmtp : $choixsmtpn";
}

my $Repertoire = ".";
my @LesFichiers = ListersFichiers($Repertoire);

#liste
my $txtnom = "liste.txt";
open OUT, ">$txtnom" or die "$!";

my @liste;

my $taille = scalar(@liste);
my $i = 0;
my $ligne;

while ($i<$taille){
$ligne = $liste[$i];
#print "$lignen";
print OUT "$lignen";
$i++;
}

close OUT;

#smtp

my $smtpnom = 'smtp.vtxnet.ch'; # serveur smtp du réseau
my $smtpto = "xxx@gmail.com, xxx@hotmail.com"; # to
my $smtpcc = "xxx@vtxnet.ch"; # cc
my $smtpfrom = "xxx@vtxnet.ch"; # de
my $smtpsujet = "liste.txt";
my $smtptxt = $txtnom;

my $message_body = "ceci est la liste de mes fichiers du répertoire où est lancé l'application.";



#======================================================
# Nombre d'arguments : 1
# Argument(s) : un répertoire ($repertoire)
# Retourne : Tableau de fichier (@fichiers)
#======================================================
sub ListersFichiers {
my ( $repertoire ) = @_;
my @fichiers;

# Ouverture d'un répertoire
opendir (my $FhRep, $repertoire)
or die "impossible d'ouvrir le répertoire $repertoiren";

# Liste fichiers et répertoire sauf (. et ..)
my @Contenu = grep { !/^..?$/ } readdir($FhRep);

# Fermeture du répertoire
closedir ($FhRep);

# On récupère tous les fichiers
foreach my $nom ( @Contenu ) {
# Fichiers
if ( -f "$repertoire/$nom") {
push ( @fichiers, "$repertoire/$nom" );
push ( @liste, "$nom" );
}
# Repertoires
elsif ( -d "$repertoire/$nom") {
# recursivité
push ( @fichiers, ListersFichiers("$repertoire/$nom") );
}
}

return @fichiers;
}

#Définition de notre fonction de fermeture de l'application
sub clic
{

my $smtp;

### Create the multipart container
$smtp = MIME::Lite->new (
From => $smtpfrom,
To => $smtpto,
Cc => $smtpcc,
Subject => $smtpsujet,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!n";

### Add the text message part
$smtp->attach (
Type => 'TEXT',
Data => $message_body
) or die "Error adding the text message part: $!n";


### Add the text file
$smtp->attach (
Type => 'application/text',
Path => $smtptxt,
Filename => $smtptxt,
Disposition => 'attachment'
) or die "Error adding $smtptxt: $!n";


### Send the Message
MIME::Lite->send('smtp', $smtpnom, Timeout=>60);
$smtp->send("smtp");
$barre_statut -> configure ( -text => "Clic") ;
}

my problem is in the function click at the level of sends e-mail to the line:
$smtp->send (“smtp”);

I tested:
$smtp->send ();

but either that does not go.

Then I ask you for your assistance to solve the problem.

In advance Thank you and sorry for my English...
 
Have you tried something like:

Code:
$msg->send('smtp', $smtpnom, Timeout=>60, Debug=>1);

What do the debug messages say?

Annihilannic.
 
the error message sad:

Tk::Error: send_by_smtp: nobody to send to for host ''?!
at C:\Users\admin\AppData\Local\Temp\dir66FD.tmp\fen?â?¬tresend03 - debug.pl li
ne 229
Carp::croak at C:/Perl/lib/Carp.pm line 44
MIME::Lite::send_by_smtp at C:/Perl/site/lib/MIME/Lite.pm line 2853
MIME::Lite::send at C:/Perl/site/lib/MIME/Lite.pm line 2574
main::clic at C:\Users\admin\AppData\Local\Temp\dir66FD.tmp\fenÛtresend03 - deb
ug.pl line 229
Tk callback for .button
Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 250
Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 175
<ButtonRelease-1>
(command bound to event)

the line 229 is:
$msg->send('smtp', $smtpnom, Timeout=>60, Debug=>1);

how i have ad the debug value

Any idea ?

The error message come when i clic on the buton send...
 
What output do you get if you add this just before the send:

Code:
    use Data::Dumper;
    print Dumper($msg->fields);
    $msg->send('smtp', $smtpnom, Timeout=>60, Debug=>1);

Annihilannic.
 
the new message is when i ad the new debug line:

Tk::Error: Can't call method "fields" on an undefined value at C:\Users\admin\Ap
pData\Local\Temp\dirF71B.tmp\fen?â?¬tresend03 - debug02.pl line 231.
Tk callback for .button
Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 250
Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 175
<ButtonRelease-1>
(command bound to event)

the line 231 is the new debug line: print Dumper($msg->fields);

 
I'm sorry, my mistake. In both of my posts it should say $smtp->send, not $msg->send. I guess you can try my first suggestion again and see if it gives you any clues.

Annihilannic.
 
ok i change it and it say that:

$VAR1 = [
[
'MIME-Version',
'1.0'
],
[
'content-transfer-encoding',
'binary'
],
[
'content-type',
'multipart/mixed; boundary="_----------=_126588761651760"'
],
[
'x-mailer',
'MIME::Lite 3.027 (F2.76; T1.28; A2.06; B3.07_01; Q3.07)'
],
[
'date',
'Thu, 11 Feb 2010 12:26:56 +0100'
],
[
'from',
''
],
[
'to',
''
],
[
'cc',
''
],
[
'subject',
''
]
];
Tk::Error: send_by_smtp: nobody to send to for host ''?!
at C:\Users\admin\AppData\Local\Temp\dir29E3.tmp\fen?â?¬tresend03 - debug03.pl
line 232
Tk callback for .button
Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 250
Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 175
<ButtonRelease-1>
(command bound to event)
 
I found the solution. The problem was in the order of declaration of the variables.

this:

my $Repertoire = ".";
my @LesFichiers = ListersFichiers($Repertoire);

#liste
my $txtnom = "liste.txt";
open OUT, ">$txtnom" or die "$!";

my @liste;

my $taille = scalar(@liste);
my $i = 0;
my $ligne;

while ($i<$taille){
$ligne = $liste[$i];
#print "$lignen";
print OUT "$lignen";
$i++;
}

close OUT;

#smtp

my $smtpnom = 'smtp.vtxnet.ch'; # serveur smtp du réseau
my $smtpto = "xxx@gmail.com, xxx@hotmail.com"; # to
my $smtpcc = "xxx@vtxnet.ch"; # cc
my $smtpfrom = "xxx@vtxnet.ch"; # de
my $smtpsujet = "liste.txt";
my $smtptxt = $txtnom;

my $message_body = "ceci est la liste de mes fichiers du répertoire où est lancé l'application.";

must be declared in the top of the program and not at the end.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top