2010-08-04 57 views
4

我需要動態創建圖像,然後將其通過電子郵件發送給此人。這裏是圖像創建腳本:通過電子郵件發送動態創建的圖像

//Get the blank coupon 
$rImg = ImageCreateFromJPEG('coupon.jpg'); 

$color = imagecolorallocate($rImg, 0, 0, 0); 

imagestring($rImg,5,135,140,'$first_name', 'black'); 
imagestring($rImg,5,135,160,'$last_name', 'black'); 
imagestring($rImg,5,135,180,'$email_address', 'black'); 

從那裏我想拍攝該圖像並通過電子郵件發送給該人。我可以將它包含在電子郵件的附件中,還是必須將其寫入服務器?

我也試着這樣做:

function email_coupon($first_name, $last_name, $email_address){ 
//Get the blank coupon 
$rImg = ImageCreateFromJPEG('coupon.jpg'); 

$color = imagecolorallocate($rImg, 0, 0, 0); 

imagestring($rImg,5,135,140,'$first_name', 'black'); 
imagestring($rImg,5,135,160,'$last_name', 'black'); 
imagestring($rImg,5,135,180,'$email_address', 'black'); 

//define the receiver of the email 
$to = '$email_address'; 
//define the subject of the email 
$subject = 'Your coupon'; 
//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 
ob_start(); 
imagejpeg($rImg); 
$i = ob_get_clean(); 
$attachment = chunk_split(base64_encode($i)); 
//define the body of the message. 
ob_clean(); 
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: image/jpeg; name="coupon.jpg" 
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($email_address, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
if($mail_sent == true) { 
    echo 'sent'; 
} else { 
    echo 'failed'; 
}; 

};

任何人有任何想法?

在此先感謝。

回答

5

您不需要向您的服務器/文件寫入任何內容,但imagejpeg帶有一個參數將自動將圖像打印到瀏覽器。爲了得到它的變量,可以使用ob_start和​​:

ob_start(); 
imagejpeg($rImg); 
$i = ob_get_contents(); 
$attachment = chunk_split(base64_encode($i)); 
// clear the buffer, but don't destroy it 
ob_clean(); 
// echo out mail like usual 
echo("Mail body here"); 
$message = ob_get_clean(); 

http://www.php.net/manual/en/ref.outcontrol.php

爲您的代碼:

function email_coupon($first_name, $last_name, $email_address){ 
//Get the blank coupon 
$rImg = ImageCreateFromJPEG('coupon.jpg'); 

$color = imagecolorallocate($rImg, 0, 0, 0); 

imagestring($rImg,5,135,140,'$first_name', 'black'); 
imagestring($rImg,5,135,160,'$last_name', 'black'); 
imagestring($rImg,5,135,180,'$email_address', 'black'); 

//define the receiver of the email 
$to = '$email_address'; 
//define the subject of the email 
$subject = 'Your coupon'; 
//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 
ob_start(); 
imagejpeg($rImg); 
$i = ob_get_contents(); 
$attachment = chunk_split(base64_encode($i)); 
//define the body of the message. 
ob_clean(); 

?> 
--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: image/jpeg; name="coupon.jpg" 
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($email_address, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
if($mail_sent == true) { 
    echo 'sent'; 
} else { 
    echo 'failed'; 
}; 
+0

媽的你是好。謝謝! – 2010-08-04 20:24:18

+0

問題。這很好,但我使用ob_start();後來在代碼中,它打破了一切......任何想法? – 2010-08-05 15:37:28

+0

當然,更新了我的答案。您不需要調用ob_start兩次,而是使用ob_clean和ob_get_contents,而不是銷燬緩衝區。 – 2010-08-05 15:57:49

相關問題