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

标签 php email image-processing gd

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

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

关于php - 通过电子邮件发送动态创建的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3409430/

相关文章:

php - 如果多维数组中的键值对匹配,如何合并两个具有平均值的数组

PHP Regex匹配最后一次出现的字符串

html - Outlook 2010 电子邮件渲染

asp.net - 通过 amazon ses 发送带有阿拉伯发件人姓名的邮件

python - 对 RGB 颜色分量执行 Sobel 滤镜

将 float 32 位大端字节序转换为小端字节序

php - 图片未显示在页面上-在新标签页中打开会显示index.php

选择项目的 php/mysql 最佳实践

swift - Firebase 使用 "Optional()"包装器包装用户信息

c# - 速度 - 将位图数据复制到数组中还是直接使用它?