微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 通过电子邮件发送动态创建的图像

我需要动态创建一个图像,然后通过电子邮件发送给该人.这是图像创建脚本:

//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';
 };

};

有人有主意吗?

提前致谢.

解决方法:

您不需要向服务器/文件写任何内容,但带有一个参数的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';
 };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐