segunda-feira, 30 de dezembro de 2019

Perl com Sendmail

Fonte original - https://opensourceforu.com/2015/11/scripting-mail-with-shell-and-perl/

Código exemplo:

#!/bin/perl

my ($subject, $mach, $from, $to, $attachment, $fh, $content);
$subject = "Test Mail";
$from = 'usuario@dominio';
$to = 'usuario@dominio, usuario2@dominio';
$attachment="/path/to/teste_email.pl";

# Cria arquivo com a mensagem - corpo do texto do email
$fh="mailbody.txt";
open(FILE, ">", "$fh") or die "Cannot open $fh: $!";
print FILE "Este  uma mensagem de texto!\nWelcome to the world of mailing... Buhahaha!!";
close(FILE);
# Termino do arquivo com a mensagem - corpo do texto do email

# Abre o Handler do Email
open(MAIL, "|/usr/sbin/sendmail -t -f $from");

# Header do Email - Destinatarios e Assunto
print MAIL "TO: $to\n";
print MAIL "Subject: $subject\n";

# Inicio do Corpo do Email - incluida a partir do arquivo temporario criado
print MAIL "Content-Type: multipart/mixed; boundary=frontier\n";
print MAIL "--frontier\n";
print MAIL "Content-Type: text/plain; charset=us-ascii\n\n";
open(FILE, "<", "$fh") or die "Cannot open $fh: $!";
print MAIL <FILE>;
print MAIL "\n\n";
print MAIL "--frontier\n";
# Fim do Corpo do Email
# Inicio do Attach
chomp(my $basename=`basename $attachment`);
print MAIL "Content-Disposition: attachment; filename=$basename\n";
print MAIL "Content-Type: text/plain; name=$attachment\n\n";
open(FILE, "<", "$attachment") or die "Cannot open $attachment: $!";
print MAIL <FILE>;
print MAIL "\n";
close(FILE);
# Termino do Attach

close(MAIL);

Nenhum comentário:

Postar um comentário