2009-08-25 9 views
5

Ho un registro che viene creato da un gruppo di lavori cron. Il mio compito ora è di inviare registri specifici (ad es. Output di errori) come email. Qual è il modo migliore per ottenere contenuti da un file e inviarlo come e-mail?Come posso inviare il contenuto del file come e-mail in Perl?

Ho già capito come inviare e-mail in perl. Ho solo bisogno di capire come leggere il file e metterlo come testo dell'e-mail.

risposta

4

Si può solo trangugiare il backup del contenuto del file in questo modo e utilizzarlo come si farebbe con qualsiasi altra stringa:

open my $fh, '<', 'file.txt' or die "Ouch: $!\n"; 

my $text = do { 
    local $/; 
    <$fh> 
}; 

close $fh or die "Ugh: $!\n"; 
print $text,"\n"; 
+0

Utilizzare 'File :: Slurp :: read_file'. –

+0

Penso che questo sia quello che stavo cercando. Puoi spiegare "my $ text = do {...};"? Sono davvero nuovo di perl. – codingbear

+3

Il blocco che segue 'do' viene eseguito e viene restituita l'ultima riga (si veda 'perldoc -f do'). Il $ locale/non definisce il valore del separatore del record di input in modo che '<$fh> 'recuperi l'intero file. Questo è un idioma perl abbastanza comune chiamato slurping di file. Si potrebbe anche usare 'File :: Slurp :: read_file' come raccomandato da Sinan. – seth

4

Che cosa stai usando per inviare l'e-mail? Io uso MIME::Lite. e puoi usarlo per allegare il file.

Altrimenti basta aprire il registro, leggerlo in linea alla volta (o utilizzare File::Slurp) e scaricare il contenuto del file nell'e-mail.

11

Io uso MIME::Lite, questo è lo script cron che uso per il mio backup notturni:

$msg = MIME::Lite->new(
    From => '[email protected]', 
    To  => '[email protected]', 
    Bcc  => '[email protected]', 
    Subject => "DB.tgz Nightly MySQL backup!", 
    Type => "text/plain", 
    Data => "Your backup sir."); 

$msg->attach(Type=> "application/x-tar", 
      Path =>"/var/some/folder/DB_Dump/DB.tgz", 
      Filename =>"DB.tgz"); 

$msg->send; 
+1

Mi piace la tua strada. – JDrago

+0

Vedere http://perldoc.perl.org/perlfaq9.html # How-do-I-send-mail% 39 –

+0

Ottimo esempio, ma non volevo l'allegato del file. +1 – codingbear

0

È possibile aprire un file in Perl in diversi modi.

Quello che dovete sapere è descritto in perl -f open

Ecco un esempio:

my $file = 'filename.txt'; 
open my $ifh, '<', $file 
    or die "Cannot open '$file' for reading: $!"; 
local $/ = ''; 
my $contents = <$ifh>; 
close($ifh); 

Ora solo e-mail $contents via e-mail.

io non sono sicuro di come si sta inviando e-mail, ma il modo che uso spesso è il seguente:

# Install these modules from CPAN: 
use Mail::Sendmail; 
use MIME::Base64; 

sendmail(
    To       => '[email protected]', 
    From      => 'Friendly Name <[email protected]>', 
    'reply-to'     => '[email protected]', 
    Subject      => 'That file you wanted', 

    # If you are sending an HTML file, use 'text/html' instead of 'text/plain': 
    'content-type'    => 'text/plain', 
    'content-transfer-encoding' => 'base64', 
    Message      => encode_base64($contents), 
); 
0

Credo che gli allegati sono la strada da percorrere dato quello che hai descritto e altri hanno già contribuito su questo, ma se si dispone di un requisito o bisogno di leggere un file e analizzarlo in un contenuto di e-mail (senza allegati) tramite Perl qui è il modo per farlo:

#!/usr/bin/perl 
#  this program will read a file and parse it into an email 
use Net::SMTP; 
#you need to change the four below line 
my $smtp = Net::SMTP->new("your_mail_server_goes_here"); 
my $from_email = "your_from_email"; 
my $to_email = "yuor_to_email"; 
my $file="the_full_path_to_your_file_including_file_name"; 

my $header = "your_subject_here"; 
$smtp->mail($from_email); 
#Send the server the 'Mail To' address. 
$smtp->to($to_email); 
#Start the message. 
$smtp->data(); 
$smtp->datasend("From: $from_email\n"); 
$smtp->datasend("To: $to_email\n"); 
$smtp->datasend("Subject: $header \n"); 
$smtp->datasend("\n"); 
#make sure file exists 
if (-e $file) { 
     $smtp->datasend("testing \n\n"); 
     #read the file one line at a time 
     open(RFILE, "<$file")||print "could not open file"; 
     while (my $line = <RFILE>){ 
       $smtp->datasend("$line"); 
     } 
     close(RFILE) || print "could not close file"; 
} 
else { 
     print "did not find the report $file "; 
     exit 1; 
#End the message. 
$smtp->dataend(); 
#Close the connection to your server. 
$smtp->quit(); 
#Send the MAIL command to the server. 
$smtp->mail("$from_email"); 
0

possiamo usare mail::outlook invece di mime::lite troppo :

#open file from local machine 

open my $fh, '<', "C:\\SDB_Automation\\sdb_dump.txt" or die "Ouch: $!\n"; 
my $text1 = do { 
local $/; 
<$fh> 
}; 
close $fh or die "Ugh: $!\n"; 
print $text1,"\n"; 
#create the object 


use Mail::Outlook; 
    my $outlook = new Mail::Outlook(); 

    # start with a folder 
my $outlook = new Mail::Outlook('Inbox'); 

    # use the Win32::OLE::Const definitions 

    use Mail::Outlook; 
    use Win32::OLE::Const 'Microsoft Outlook'; 
     my $outlook = new Mail::Outlook(olInbox); 

    # get/set the current folder 

    my $folder = $outlook->folder(); 
    my $folder = $outlook->folder('Inbox'); 

    # get the first/last/next/previous message 

    my $message = $folder->first(); 
    $message = $folder->next(); 
    $message = $folder->last(); 
    $message = $folder->previous(); 
    # read the attributes of the current message 

    my $text = $message->From(); 
    $text = $message->To(); 
    $text = $message->Cc(); 
    $text = $message->Bcc(); 
    $text = $message->Subject(); 
    $text = $message->Body(); 
    my @list = $message->Attach(); 


    # use Outlook to display the current message 
$message->display; 

    # create a message for sending 

    my $message = $outlook->create(); 
    $message->To('[email protected]'); 
    $message->Subject('boom boom boom'); 
    $message->Body("$text1"); 
    $message->Attach('C:\SDB_Automation\sdb_dump.txt'); 
    $message->send; 
Problemi correlati