2010-01-27 9 views
9

मैं पीडीएफ का उपयोग एफपीडीएफ का उपयोग करता हूं और मुझे इसे किसी ग्राहक को ईमेल करने की आवश्यकता है।PHP फ़ाइल बनाने के बिना पीडीएफ अटैचमेंट के साथ ईमेल भेजें?

चूंकि मैं डीबी से पीडीएफ उत्पन्न कर सकता हूं, इसलिए मैं सभी पीडीएफ को स्थानीय रूप से सहेजना नहीं चाहता क्योंकि यह मेरे सर्वर को अव्यवस्थित कर देगा।

मैं एक पीडीएफ संलग्न के साथ एक ईमेल भेजने में सक्षम होना चाहता हूँ। क्या मुझे एक अस्थायी फ़ाइल बनाने की आवश्यकता है, फिर इसे हर बार हटा दें या ऐसा करने का कोई और तरीका है?

उत्तर

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

दिलचस्प .. बहुत बहुत धन्यवाद! मैंने पाया कि एफपीडीएफ फ़ाइल लिखने के बजाए एक स्ट्रिंग वापस कर सकता है: http://www.fpdf.org/en/doc/output.htm क्या आप इसका उपयोग करेंगे? – Samuurai

+0

यह अच्छा लगता है। – Pentium10

1

यह निश्चित रूप से संभव है। आप कोई भी PHP मेलर क्लास ले सकते हैं जो संलग्नक को संभाल सकता है, और addAttachment फ़ंक्शन को फिर से लिख सकता है जहां यह फ़ाइल से डेटा को आपके वैरिएबल को स्वीकार करने के लिए पढ़ता है।

Zend_Mail किसी भी पुनर्लेखन के बिना स्ट्रिंग को सीधे संलग्नक के रूप में पचाने में सक्षम प्रतीत होता है।

संबंधित मुद्दे