2010-01-27 3 views

답변

5
<?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('attachment.zip'))); 
//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"; 
?> 

이 라인 $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));을 알 당신은 메모리 파일에 맞게 변경합니다.

+0

흥미로운 ... 정말 고마워! FPDF가 파일에 쓰는 것이 아니라 문자열을 반환 할 수 있다는 것을 알게되었습니다 : http://www.fpdf.org/en/doc/output.htm 사용 하시겠습니까? – Samuurai

+0

그건 좋은 소리. – Pentium10

1

확실히 가능합니다. 첨부 파일을 처리 할 수있는 PHP 메일러 클래스를 가져 와서 파일에서 데이터를 읽는 대신 addAttachment 함수를 다시 작성하여 대신 변수를 사용할 수 있습니다.

Zend_Mail은 문자열을 직접 다시 첨부하지 않고 직접 첨부 파일로 소화 할 수있는 것처럼 보입니다.

관련 문제