代码发送2次没有正确的图像

时间:2012-12-28 11:59:35

标签: image php php-gd

我在帖子中尝试了此代码,但此代码在一次运行的文件中发送了两封电子邮件。

Email file is sending email two times using php mail function

让我知道我做错了什么 -

<?php
function mytextoverimage( $mytext ) {
$headurl = 'http://dummyimage.com/600x400/ffffee/00d5ff.jpg';
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg($headurl);
$black = imagecolorallocate($jpg_image, 1, 1, 1);
$font_path = 'myfont/arial.ttf';
$text = $mytext;
imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
}

$to = "myemail@gmail.com";
$subject = "This is a image conversion from Developer Zone";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: developer@phpdev.com' . "\r\n" .
'Reply-To: testabc@testabc.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Title</title>
</head>
<body>
<table width="100%" cellspacing="5" cellpadding="0" border="0" bgcolor="#f0f0f0" style="color:666666;text-align:left; font:12px Verdana, Geneva, sans-serif">
<tr>
<td >'.mytextoverimage('Developer').'</td></tr></table></body></html>';

    mail($to,$subject,$message,$headers); die;

让我知道我做错了什么,这是我正在使用的正确方法 -

<img src="'.mytextoverimage('Developer').'" />

我关注了此网址,但很难从此页面获取任何帮助 - http://php.net/manual/en/function.imagejpeg.php

我甚至尝试将该方法mytextoverimage()保留在另一个文件中,但仍无法提供帮助,电子邮件发送两次:(

3 个答案:

答案 0 :(得分:2)

你的mytextoverimage()函数没有返回任何内容 - 它只是将jpeg图像发送到浏览器。

我已将您的代码重新设计为通过电子邮件发送相同的图像 - 请注意,只发送图像,没有HMTL。

如果要将图像作为HTML文档的一部分发送,则需要更进一步,创建一个多部分消息 - 请查看How to attach and show image in mail using php?

适用于Iceweasel 10.0.11上的Gmail。

<?php
function mytextoverimage( $mytext )
{
    $headurl = 'http://dummyimage.com/600x400/ffffee/00d5ff.jpg';
    $jpg_image = imagecreatefromjpeg($headurl);
    $black = imagecolorallocate($jpg_image, 1, 1, 1);
    $font_path = 'myfont/arial.ttf';
    $text = $mytext;
    imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
    ob_start(); //Get the image data from the output buffer
    imagejpeg($jpg_image);
    imagedestroy($jpg_image);
    return chunk_split(base64_encode(ob_get_clean())); //return the image data, encoded for email transfer
}

$to = "myemail@gmail.com";
$subject = "This is a image conversion from Developer Zone";
// --- Note the change from text/html to image/jpeg ---
$headers = "Content-type: image/jpeg;\r\n";
//$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: developer@phpdev.com' . "\r\n" .
'Reply-To: testabc@testabc.com' . "\r\n" .
'Content-Transfer-Encoding: base64' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = mytextoverimage('Developer');

    mail($to,$subject,$message,$headers); die;

答案 1 :(得分:1)

是的,你做错了。 Imagejpg函数返回和图像,但您需要一个URL将其放在标记内。您应该做的是使用SWIFT邮件程序并将您创建的图像作为附件发送到电子邮件。 你可以在这里阅读: http://swiftmailer.org/docs/messages.html

这就像这样:

 //Create the message
 $img = $message->embed(Swift_Image::fromPath('body1.jpg'));

 //Set the body
 $message->setBody(
   '<html>' .
   ' <head></head>' .
   ' <body>' .
   " <img src='$img'/>"
   ' </body>' .
   '</html>',

   'text/html' //Mark the content-type as HTML
 );

答案 2 :(得分:1)

就我的问题而言,我解决了它,就像这样 -

<?php
function myimagecreate( $name ) 
{
$headurl = 'http://dummyimage.com/600x300/f5ebf5/f2f2f7.jpg';
header('Content-type: image/jpeg');
$text = $name;
$name =$name.".jpg";
$filepath = 'http://MY_SITE_URL.com/'."myfont";
$jpg_image = imagecreatefromjpeg($headurl);
$black = imagecolorallocate($jpg_image, 1, 1, 1);
$font_path = 'myfont/Ayuma2yk.ttf';
imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
imagejpeg($jpg_image,$name);
imagedestroy($jpg_image);
return $name;
}

$to      = 'YOUREMAIL@gmail.com';                
$subject = 'Swapnesh Sinha - For PHP GD Library';           
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Swapnesh Sinha</title>
            </head>
            <body>
            <table width="600px" bgcolor="#f0f0f0" style="color:666666;text-align:left; font:12px Verdana, Geneva, sans-serif">
            <tr>
            <td>
            <img src="http://MY_SITE_URL.com/'.myimagecreate('Swapnesh').'" style="display:block" />
            </td>
            </tr>
            </table>
            </body>
            </html>';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Swapnesh Sinha <MyMAIL@gmail.com>'. "\r\n";

$bool = mail($to,$subject,$message,$headers);
if($bool)
echo "Email is sent successfully";
else
echo "Something is missing in the code, please check the code properly!!";          

?>

只需将代码保存在任何根文件“Yourfile.php”中并运行。

这将创建一个图像并保存到根位置(您也可以强制将其保存到另一个位置)。

也可以关注这两个链接 -

LINK 1 LINK 2