2010-12-01 10 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

다른 사람이있다, 그 들어가는 많은, 내가보기 엔 당신이하고 시간을 보낼 수 있습니다 더 중요한 일이 있습니다 당신 같은

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

관련 문제