Perl en Español

  1. Home
  2. Tutoriales
  3. Foro
  4. Artículos
  5. Donativos
  6. Publicidad
 
Índice general » Mundo Perl » Intermedio » Envio adjunto gzip con SMTP Gmail  RESUELTO Responder al tema
Nuevo tema


Página 1 de 1  [ 5 mensajes ] 
 
Nota 2012-01-12 06:57 @331

Perlero Nuevo
Registrado: 2012-01-12 06:44 @322
Mensajes: 3
Envio adjunto gzip con SMTP Gmail
Hola a todos

Escribo para ver si me pueden ayudar con un problema que tengo. Quiero mediante un script en Perl enviar un correo mediante cuenta de Gmail con archivo adjunto de tipo zip. He probado con archivo de texto plano y no hay problema pero no me deja con un tar.gz.

Este es el script.

Syntax: [ Download ] [ Hide ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl -w
  2.  
  3. #use Net::SMTP::SSL;
  4. use Net::SMTP::TLS;
  5. use MIME::Base64;
  6. use MIME::Decoder::Gzip64;
  7. use File::Spec;
  8. use LWP::MediaTypes;
  9.  
  10. sub send_mail_with_attachments {
  11.  my $to = shift(@_);
  12.  my $subject = shift(@_);
  13.  my $body = shift(@_);
  14.  my @attachments = @_;
  15.  
  16.  my $from = 'remitentearrobagmail.com';
  17.  my $password = 'mipassword';
  18.  
  19.  my $smtp;
  20.  if (not $smtp = new  Net::SMTP::TLS('smtp.gmail.com',Hello=>'smtp.gmail.com',Port=>587,User=>'remitentearrobagmail.com',Password=>'mipass')) {
  21.      die "Could not connect to server\n";
  22.  }
  23.  
  24.  
  25.  # Create arbitrary boundary text used to seperate
  26.  # different parts of the message
  27.  my ($bi, $bn, @bchrs);
  28.  my $boundry = "";
  29.  foreach $bn (48..57,65..90,97..122) {
  30.      $bchrs[$bi++] = chr($bn);
  31.  }
  32.  foreach $bn (0..20) {
  33.      $boundry .= $bchrs[rand($bi)];
  34.  }
  35.  
  36.  # Send the header
  37.  $smtp->mail($from . "\n");
  38.  my @recepients = split(/,/, $to);
  39.  foreach my $recp (@recepients) {
  40.      $smtp->to($recp . "\n");
  41.  }
  42.  $smtp->data();
  43.  $smtp->datasend("From: " . $from . "\n");
  44.  $smtp->datasend("To: " . $to . "\n");
  45.  $smtp->datasend("Subject: " . $subject . "\n");
  46.  $smtp->datasend("MIME-Version: 1.0\n");
  47.  $smtp->datasend("Content-Type: multipart/mixed; BOUNDARY=\"$boundry\"\n");
  48.  
  49.  # Send the body
  50.  $smtp->datasend("\n--$boundry\n");
  51.  $smtp->datasend("Content-Type: application/zip\n");
  52.  $smtp->datasend($body . "\n\n");
  53.  
  54.  # Send attachments
  55.  foreach my $file (@attachments) {
  56.      unless (-f $file) {
  57.          die "Unable to find attachment file $file\n";
  58.          next;
  59.      }
  60.      my($bytesread, $buffer, $data, $total);
  61.      open(FH, "$file") || die "Failed to open $file\n";
  62.      binmode(FH);
  63.      while (($bytesread = sysread(FH, $buffer, 1024)) == 1024) {
  64.          $total += $bytesread;
  65.          $data .= $buffer;
  66.      }
  67.      if ($bytesread) {
  68.          $data .= $buffer;
  69.          $total += $bytesread;
  70.      }
  71.      close FH;
  72.  
  73.      # Get the file name without its directory
  74.      my ($volume, $dir, $fileName) = File::Spec->splitpath($file);
  75.  
  76.      # Try and guess the MIME type from the file extension so
  77.      # that the email client doesn't have to
  78.      my $contentType = guess_media_type($file);
  79.  
  80.      if ($data) {
  81.          $smtp->datasend("--$boundry\n");
  82.          $smtp->datasend("Content-Type: $contentType; name=\"$fileName\"\n");
  83.          $smtp->datasend("Content-Transfer-Encoding: base64\n");
  84.          $smtp->datasend("Content-Disposition: attachment; =filename=\"$fileName\"\n\n");
  85.          $smtp->datasend(encode_base64($data));
  86.          $smtp->datasend("--$boundry\n");
  87.      }
  88.  }
  89.  
  90.  # Quit
  91.  $smtp->datasend("\n--$boundry--\n"); # send boundary end message
  92.  $smtp->datasend("\n");
  93.  $smtp->dataend();
  94.  $smtp->quit;
  95. }
  96.  
  97. # Send away!
  98.  
  99. &send_mail_with_attachments('destinatarioarrobagmail.com', 'Envio de backup BD diaria', ' ', 'archivo1.tgz', 'archivo2.tgz');




Y me lanza el siguiente error:

Syntax: [ Download ] [ Hide ]
  1. Couldn't send mail: 552 5.7.0 Our system detected an illegal attachment on your message. 


Sé que tendrá algo que ver con el tipo de archivo pero le he cambiado el "application/"

Gracias por la ayuda, un saludo.


Nota 2012-01-12 07:30 @354
Avatar de Usuario
Administrador
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España
Mensajes: 10250
Re: Envio adjunto gzip con SMTP Gmail  RESUELTO
Bienvenido a los foros de Perl en español, alberpilot.

Haz la prueba siguiente: coge un fichero de texto, sencillo. Y lo comprimes de la misma manera que el adjunto que quieres mandar. Y luego ejecuta el programa para que envíe ese texto comprimido.

Si sale el mensaje de error, es que a Gmail no le gustan los adjuntos comprimidos de esa manera.

El mensaje de error dice que el adjunto es ilegal. Habría que ver qué significa ilegal para Gmail. ¿La extensión, el tipo de adjunto, lo que contiene el adjunto comprimido?

_________________
JF^D Perl programming


Nota 2012-01-12 08:39 @402

Perlero Nuevo
Registrado: 2012-01-12 06:44 @322
Mensajes: 3
Re: Envio adjunto gzip con SMTP Gmail
Efectivamente. He probado a comprimir un texto plano sencillo y me lo ha enviado bien. Pero necesito enviar un comprimido que contiene varios .sql ¿Cómo podría hacerlo?

¡Muchas gracias por la respuesta!


Nota 2012-01-12 14:00 @625
Avatar de Usuario
Administrador
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España
Mensajes: 10250
Re: Envio adjunto gzip con SMTP Gmail
Pues no sé... ¿cambiando la extensión de .sql a .txt?

¿O comprimiendo en un .rar y luego en .zip?

¿No usar Gmail?

_________________
JF^D Perl programming


Nota 2012-01-13 04:05 @212

Perlero Nuevo
Registrado: 2012-01-12 06:44 @322
Mensajes: 3
Re: Envio adjunto gzip con SMTP Gmail
Vale. Quería asegurarme de que mi código hacía lo que pretendía. Muchísimas gracias por la ayuda, ¡un saludo!


Responder al tema  [ 5 mensajes ] 

Reglas del Foro
No puedes abrir nuevos temas en este Foro
No puedes responder a temas en este Foro
No puedes editar tus mensajes en este Foro
No puedes borrar tus mensajes en este Foro
No puedes enviar adjuntos en este Foro

Publicidad

Socializa

Síguenos por Twitter

Suscríbete GRATUITAMENTE al Boletín de Perl en Español

Saltar a:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Traducción al español por Huan Manwë para phpbb-es.com
phpBB SEO