2012-05-17 1 views
2

나는 multipart 콘텐츠 이메일에 pdf 파일을 첨부하려하고있다. 예, 나는 mime lite 또는 10 억 개의 펄 모듈을 사용할 수 있지만, 이 상자에서 나오는대로 지금까지 내가 이메일이 올바르게 보내지 만 (분명히) PDF로는 그것이이있다 말한다 파일을 열려고 할 때거기에 Net과 함께 PDF 파일을 첨부 할 수있는 방법은 : 펄 :

#!/usr/bin/perl 
use Net::SMTP; 
use MIME::Base64 qw(encode_base64); 
use MIME::Base64 qw(decode_base64); 
use strict; 
use warnings; 

my $from = '[email protected]'; 
my $to = '[email protected]'; 
my $to2 = '[email protected]'; 
my $boundary = 'frontier'; 




open my $Initial_File, '<', "summary.pdf"; 
binmode $Initial_File; 
open my $Initial_OutFile, '>', "temp.pdf"; 
my $buf; 
while (read($Initial_File, $buf, 60 * 57)) { 
    print $Initial_OutFile encode_base64($buf); 
} 

close $Initial_OutFile; 
close $Initial_File; 


open INFILE, '<', "temp.pdf"; 
open my $final_output, '>',"summary2.pdf"; 
binmode $final_output; 
my $buffer; 
while ($buffer = <INFILE>) { 
    print $final_output decode_base64($buffer); 
} 
my @pdf = $final_output; 
close $final_output; 
close INFILE; 

my $smtp = Net::SMTP->new('xx.xxx.com'); 
$smtp->mail($from); 
$smtp->recipient($to,$to2, { SkipBad => 1 }); 
$smtp->data(); 
$smtp->datasend("Subject: Test Email \n"); 
$smtp->datasend("MIME-Version: 1.0\n"); 
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=".$boundary."\n"); 
$smtp->datasend("\n"); 
$smtp->datasend("--".$boundary."\n"); 
$smtp->datasend("Content-type: text/plain\n"); 
$smtp->datasend("Content-Disposition: quoted-printable\n"); 
$smtp->datasend("\nTest From You \n"); 
$smtp->datasend("--".$boundary."\n"); 
$smtp->datasend("Content-Disposition: attachment; filename=summary2.pdf \n"); 
$smtp->datasend("Content-Type: application/pdf; name=summary2.pdf "); 
$smtp->datasend("\n"); 
$smtp->datasend("@pdf\n"); 
$smtp->datasend("--".$boundary."--\n"); 
$smtp->dataend(); 
# $smtp->quit; 

exit; 

이, 펄 5.8.8를 사용하도록 제한하고있어 잘못된 인코딩을 사용하는 경우 PDF 파일을 첨부 파일에 버퍼링하는 방법이 있습니까?

+0

은 [MIME :: Lite는 순수 펄한다] (http://deps.cpantesters.org/?module=MIME%3A%3ALite&perl=5.8.8&pureperl=on), 그냥 코드를 복사합니다. – daxim

+0

"상자에서 나오는대로 펄 5.8.8을 사용하는 것으로 제한됩니다."- 아마도 문제를 먼저 처리해야합니다 :-) CPAN을 사용할 수 없다면 실제로 전체 Perl의 힘. –

+0

@DaveCross는 완전히 동의합니다. 이미 CPAN을 설치했다면 프로덕션 서버와 IT가 조금이라도 변경하지 않으려 고합니다. – isJustMe

답변

1

은 추가 MIME을 결국 : 라이트 수동으로 사용자 디렉토리 여기

use lib '/xx/sas/xx/perl'; 
use MIME::Lite; 
open(SMTP,'/xx/sas/xx/perl/MIME/srv.txt') || die("Could not open the file"); 
my $mail_host = <SMTP>; 
close(SMTP); 
open(DATA, $ARGV[3]) || die("Could not open the file"); 
my @csv = <DATA>; 
close(DATA); 

foreach (@csv){ 
    $textStr.= $_; 
} 

$msg = MIME::Lite->new (
    From => $ARGV[0], 
    To => $ARGV[1], 
    Subject => $ARGV[2], 
    Type =>'multipart/mixed' 
) or die "Error creating multipart container: $!\n"; 

$msg->attach (
    Type => 'TEXT', 
    Data => $textStr 
) or die "Error adding the text message part: $!\n"; 

foreach my $file (split(',', $ARGV[4])) { 
     $content_type='TEXT'; 
     if ($file =~ /\.gif$/i){ $content_type ='image/gif'} 
     if ($file =~ /\.jpg$/i){ $content_type ='image/jpeg'} 
     if ($file =~ /\.zip$/i){ $content_type ='application/zip'} 
     if ($file =~ /\.html$/i){ $content_type ='text/html'} 
     if ($file =~ /\.pdf$/i){ $content_type ='application/pdf'} 
     if ($file =~ /\.xls$/i){ $content_type ='application/vnd.ms-excel'} 
     if ($file =~ /\.log$/i){ $content_type ='application/octet-stream'} 
     $msg->attach (
      Type => $content_type, 
      Path => $file, 
      Filename => $file, 
      Disposition => 'attachment' 
     ) or die "Error adding $file: $!\n";  
} 

MIME::Lite->send('smtp', $mail_host, Timeout=>60); 
$msg->send; 
0

이 ... 잘못된 같습니다

open FILE, "summary.pdf" or die $!; 
my @txt = <FILE>; 
close FILE; 

... 
$smtp->datasend("@txt\n"); 

난 당신이 코드의 두 라인 내에서 거짓말을 추구 문제를 생각합니다. 문자열 판독기로 이진 형식을 읽은 다음이 이진법을 문자열로 보간합니다.

시도 :

... 
open FILE, "summary.pdf" or die $!; 
binmode(FILE); 
my $data = do { local $/; <FILE> }; 
close FILE; 
... 
$smtp->datasend($data); 

또는이 라인을 따라 뭔가.

+0

그게 효과가! – isJustMe

+0

버머! 나는 그것을 시도하지 않았고, 그것이 올바른 방향 일 것이라고 말했다. :) –

2

순 :: SMTP를 사용하여 멀티 메일 메시지를 전송하는 방법의 일 예는에.

일반 텍스트, 일반 텍스트 파일 및 이진 이미지 (jpg)를 보내기위한 코드가 제공됩니다.

희망이 도움이됩니다.

알레한드로

ps. 위에 제공된 코드 RVS에는 pdf를 비롯한 몇 가지 다른 내용 유형이 나열되어 있습니다. 일반 텍스트 파일이 아닌 파일은 바이너리 파일로 간주되어 base64로 인코딩되어 전송됩니다. 메일을 수신하는 이메일 클라이언트가이를 처리해야하므로 디코딩 할 필요가 없습니다.

#!/usr/bin/perl 

use Net::SMTP; 
use strict; 
use warnings; 

use MIME::Base64 qw(encode_base64); 
#use MIME::Base64 qw(decode_base64); 

my $from = '[email protected]'; 
my $to = '[email protected]'; 

my $attachBinaryFile= 'test.jpg'; 
my $attachTextFile = 'test.txt'; 

my $boundary = 'frontier'; 

open(DAT, $attachTextFile) || die("Could not open text file!"); 
my @textFile = <DAT>; 
close(DAT); 

my $smtp = Net::SMTP->new('your.smtp.com', Timeout => 60) || die("Could not create SMTP object."); 
print "Sending mail\n"; 
$smtp->mail($from); 
$smtp->recipient($to, { SkipBad => 1 }); 
$smtp->data(); 
$smtp->datasend("To: $to\n"); 
$smtp->datasend("From: $from\n"); 
$smtp->datasend("Subject: Multi part test\n"); 
$smtp->datasend("MIME-Version: 1.0\n"); 
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n"); 
$smtp->datasend("\n"); 
$smtp->datasend("--$boundary\n"); 
$smtp->datasend("Content-type: text/plain\n"); 
$smtp->datasend("Content-Disposition: quoted-printable\n"); 
$smtp->datasend("\nToday\'s files are attached:\n"); 
$smtp->datasend("\nHave a nice day! :)\n"); 
$smtp->datasend("--$boundary\n"); 
$smtp->datasend("Content-Type: application/text; name=\"$attachTextFile\"\n"); 
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachTextFile\"\n"); 
$smtp->datasend("\n"); 
$smtp->datasend("@textFile\n"); 
$smtp->datasend("--$boundary\n"); 
$smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\n"); 
$smtp->datasend("Content-Transfer-Encoding: base64\n"); 
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachBinaryFile\"\n"); 
$smtp->datasend("\n"); 
my $buf; 
open(DAT, "../uploads/$attachBinaryFile") || die("Could not open binary file!"); 
    binmode(DAT); 
    local $/=undef; 
    while (read(DAT, my $picture, 4096)) { 
     $buf = &encode_base64($picture); 
     $smtp->datasend($buf); 
    } 
close(DAT); 
$smtp->datasend("\n"); 
$smtp->datasend("--$boundary\n"); 
$smtp->dataend(); 
$smtp->quit; 
print "Mail sent\n"; 
exit; 
+0

안녕하세요,이 코드를 사용하고 작동하는 것 같지만 코드에 포함되지 않은 이메일에 첨부 된 추가 파일에 궁금합니다. 왜 또는 어떻게되는지 궁금합니다. 고맙습니다! :디 – Viin

관련 문제