3

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

电子邮件文件使用php邮件功能发送电子邮件两次

让我知道我做错了什么 -

<?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').'" />

我跟着这个 URL 但很难从这个页面破解任何帮助 - http://php.net/manual/en/function.imagejpeg.php

我什至尝试将该方法保存mytextoverimage()在另一个文件中,但仍然没有帮助,电子邮件发送两次 :(

4

3 回答 3

2

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

我已经修改了您的代码以通过电子邮件发送相同的图像 - 请注意,仅发送图像,没有 HMTL。

如果您想将图像作为 HTML 文档的一部分发送,您需要更进一步并创建多部分消息 - 查看如何使用 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;
于 2013-01-02T05:33:28.133 回答
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
 );
于 2012-12-31T11:23:44.017 回答
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”中并运行。

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

也遵循这两个链接 -

链接 1 链接 2

于 2013-01-01T07:50:01.710 回答