我需要动态创建图像,然后通过电子邮件发送给此人。这是图像创建脚本:
//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: joe@joesomebody.com\r\nReply-To: joe@joesomeboy.com";
//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';
};
};
有人有什么想法吗?
提前致谢。
答案 0 :(得分:5)
您不需要向服务器/文件写入任何内容,但带有一个参数的imagejpeg
会自动将图像打印到浏览器。要将其添加到变量中,请使用ob_start
和ob_get_contents
:
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: joe@joesomebody.com\r\nReply-To: joe@joesomeboy.com";
//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';
};