2010-12-01 13 views
0

PHPのメール機能を使用してメールを送信していますが、そのメッセージを添付する必要があります。添付ファイルの送信PHPのmail()関数

ほとんどの場合、添付される予定のアイテムはテキストファイルになると信じられていましたが、メールにコンテンツをエコーすることはできますが、最終的にファイルファイルをアップロードするユーザーには、プレーンテキストと独自の形式の違いが分からないため、PDF、Word文書などになります。

+1

[どのようにPHPを使用して添付ファイル付きの電子メールを送信する](http://stackoverflow.com/questions/2027069)の可能性の重複。 [この回答](http://stackoverflow.com/questions/2027069/how-to-send-email-with-attachment-using-php/2027112#2027112)をご覧ください。 – netcoder

+0

あなたはSwiftMailerを調べたいかもしれません。 http://swiftmailer.org – Jonah

+0

またはPHPMailer http://phpmailer.worxware.com/ –

答えて

3

ことになりLOTがあります、私は非常にあなたがやって自分の時間を過ごすことができ、より重要なことは、他の誰かが持っている、ありますようにPHPMailer

としてあなたのためのすべての作業を行い、パッケージでの作業をお勧めしたいですすでに仕事を終えた。

+1

+1はホイールを再発明しないためです。 – zzzzBov

0
<?php 
$to = '[email protected]'; 
$from = '[email protected]'; 
$subject = 'See Attachments'; 
$message = 'Please review the following attachments.'; 

// Define a list of FILES to send along with the e-mail. Key = File to be sent. Value = Name of file as seen in the e-mail. 
$attachments = array(
    '/tmp/WEDFRTS' => 'first-attachment.png', 
    '/tmp/some-other-file' => 'second-attachment.png' 
); 

// Define any additional headers you may want to include 
$headers = array(
    'Reply-to' => '[email protected]', 
    'Some-Other-Header-Name' => 'Header Value' 
); 

$status = mailAttachments($to, $from, $subject, $message, $attachments, $headers); 
if($status === True) { 
    print 'Successfully mailed!'; 
} else { 
    print 'Unable to send e-mail.'; 
} 




function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') { 
    $headers['From'] = $from; 

    // Define the boundray we're going to use to separate our data with. 
    $mime_boundary = '==MIME_BOUNDARY_' . md5(time()); 

    // Define attachment-specific headers 
    $headers['MIME-Version'] = '1.0'; 
    $headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"'; 

    // Convert the array of header data into a single string. 
    $headers_string = ''; 
    foreach($headers as $header_name => $header_value) { 
     if(!empty($headers_string)) { 
      $headers_string .= "\r\n"; 
     } 
     $headers_string .= $header_name . ': ' . $header_value; 
    } 

    // Message Body 
    $message_string = '--' . $mime_boundary; 
    $message_string .= "\r\n"; 
    $message_string .= 'Content-Type: text/plain; charset="iso-8859-1"'; 
    $message_string .= "\r\n"; 
    $message_string .= 'Content-Transfer-Encoding: 7bit'; 
    $message_string .= "\r\n"; 
    $message_string .= "\r\n"; 
    $message_string .= $message; 
    $message_string .= "\r\n"; 
    $message_string .= "\r\n"; 

    // Add attachments to message body 
    foreach($attachments as $local_filename => $attachment_filename) { 
     if(is_file($local_filename)) { 
      $message_string .= '--' . $mime_boundary; 
      $message_string .= "\r\n"; 
      $message_string .= 'Content-Type: application/octet-stream; name="' . $attachment_filename . '"'; 
      $message_string .= "\r\n"; 
      $message_string .= 'Content-Description: ' . $attachment_filename; 
      $message_string .= "\r\n"; 

      $fp = @fopen($local_filename, 'rb'); // Create pointer to file 
      $file_size = filesize($local_filename); // Read size of file 
      $data = @fread($fp, $file_size); // Read file contents 
      $data = chunk_split(base64_encode($data)); // Encode file contents for plain text sending 

      $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size. ';'; 
      $message_string .= "\r\n"; 
      $message_string .= 'Content-Transfer-Encoding: base64'; 
      $message_string .= "\r\n\r\n"; 
      $message_string .= $data; 
      $message_string .= "\r\n\r\n"; 
     } 
    } 

    // Signal end of message 
    $message_string .= '--' . $mime_boundary . '--'; 

    // Send the e-mail. 
    return mail($to, $subject, $message_string, $headers_string, $additional _parameters); } 

reference

関連する問題