2010-12-04 12 views
12

FPDFを使用して作成された添付ファイルとしてPDFを電子メールで送信します。私のコードはこのように見えますが、添付ファイルは決して通過しません。電子メールFPDFを使用したPHPによる添付ファイル

<?php 
require('lib/fpdf/fpdf.php'); 

$pdf = new FPDF('P', 'pt', array(500,233)); 
$pdf->AddFont('Georgiai','','georgiai.php'); 
$pdf->AddPage(); 
$pdf->Image('lib/fpdf/giftcertificate.jpg',0,0,500); 
$pdf->SetFont('georgiai','',16); 
$pdf->Cell(40,10,'Hello World!'); 
$doc = $pdf->Output('test.pdf', 'S'); 

//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string, 
//encode it with MIME base64, 
//and split it into smaller chunks 
$attachment = chunk_split(base64_encode(file_get_contents($doc))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

?> 

誰でもこれに慣れていますか?私はPHPのmail()関数を使用したいと考えています。

+2

をお楽しみください使用している場合。 http://swiftmailer.org/ –

+0

Pekkaが言うように、スウィフトメーラーは手作業で添付ファイル付きのメールを送るよりもはるかに良いアイデアです。 – Orbling

答えて

19

これは私のために働くことになった:

<?php 
require('lib/fpdf/fpdf.php'); 

$pdf = new FPDF('P', 'pt', array(500,233)); 
$pdf->AddFont('Georgiai','','georgiai.php'); 
$pdf->AddPage(); 
$pdf->Image('lib/fpdf/image.jpg',0,0,500); 
$pdf->SetFont('georgiai','',16); 
$pdf->Cell(40,10,'Hello World!'); 

// email stuff (change data below) 
$to = "[email protected]"; 
$from = "[email protected]"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>"; 

// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = PHP_EOL; 

// attachment name 
$filename = "test.pdf"; 

// encode data (puts attachment in proper format) 
$pdfdoc = $pdf->Output("", "S"); 
$attachment = chunk_split(base64_encode($pdfdoc)); 

// main header 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

// no more headers after this, we start the body! // 

$body = "--".$separator.$eol; 
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$body .= "This is a MIME encoded message.".$eol; 

// message 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$body .= $message.$eol; 

// attachment 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol; 
$body .= "Content-Disposition: attachment".$eol.$eol; 
$body .= $attachment.$eol; 
$body .= "--".$separator."--"; 

// send message 
mail($to, $subject, $body, $headers); 

?> 
+0

ありがとう、Gmailで完璧に動作しますが、Outlookではメールは空白のページで、メッセージは2番目の添付ファイルです...これは何が原因なのでしょうか? –

+0

本当に良い答え、ありがとう! –

3

私はそこに余分なコマンドがあると思います。あなたがOutput()コマンドの文字列バリアントを使用している:

$doc = $pdf->Output('test.pdf', 'S'); 

次に、あなたがそれにfile_get_contents()を行っています

$attachment = chunk_split(base64_encode(file_get_contents($doc))); 

それは文字列内のファイルで、ファイルではありません、 $docがファイル名の場合、file_get_contents()が返されます。

だけにそれをダウン減らす:任意のより多くのエラーが発生したかどうかを確認後

$attachment = chunk_split(base64_encode($doc)); 

+0

私はもうエラーは出ませんが、ファイルは添付されません。送信された電子メールには0kbの添付ファイル「noname」があります。これがなぜ起こったのかについての考えはありますか?ご協力いただきありがとうございます。 – HWD

+0

手作業でMIMEエンコーディングを取得することは非常に困難です。特に複雑な添付ファイルを追加する場合は、あなたの質問にあるようにメーリングリストを使用することをお勧めします。ちなみに、あなたはファイル名がattachment.zipであり、それがzipファイルであると言っているようですが、決してPDFを圧縮していません。 – Orbling

19

あなたはphpmailerの

$attachment= $pdf->Output('attachment.pdf', 'S'); 

$mailer->AddStringAttachment($attachment, 'attachment.pdf'); 

が、それは目的を学ぶためではない場合は、ではSwiftMailerのようなPHPメーラーライブラリを使用

+0

あなたは私の日を救った。ありがとう。私はあなたのコードを見た前に添付ファイルを追加するためのbase64_encodeとchunk_splitメソッドを試しました。これらの方法をクリアして、すべてが完璧に機能しました。 –

+0

同じです。魅力のように動作します。 – Awena

+0

時間と時間を検索ありがとう、ありがとう、いい答えです! –

1
Create one pdf file name as testing.pdf 
<?php 
    //define the receiver of the email 
    $to = '[email protected]'; 

    //define the subject of the email 
    $subject = 'Test email with attachment'; 

    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash = md5(date('r', time())); 

    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = "From: [email protected]\r\nReply-To: [email protected]"; 

    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

    //read the atachment file contents into a string, 
    //encode it with MIME base64, 
    //and split it into smaller chunks 
    $attachment = chunk_split(base64_encode(file_get_contents('testing.pdf'))); 

    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash; ?> 
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit 

    Hello World!!! 
    This is simple text email message. 

    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit 

    <h2>Email Pdf File Attachements</h2> 
    <p>This is something with <b>HTML</b> formatting.</p> 

    --PHP-alt-<?php echo $random_hash; ?>-- 

    --PHP-mixed-<?php echo $random_hash; ?> 
    Content-Type: application/pdf; name="attachment.pdf" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 

    <?php echo $attachment; ?> 
    --PHP-mixed-<?php echo $random_hash; ?>-- 

    <?php 
    //copy current buffer contents into $message variable and delete current output buffer 
    $message = ob_get_clean(); 
    //send the email 
    $mail_sent = @mail($to, $subject, $message, $headers); 
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 
関連する問題