【问题标题】:The code is sending 2 times without proper image代码在没有正确图像的情况下发送 2 次
【发布时间】:2012-12-13 16:51:26
【问题描述】:

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

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

我关注了这个 URL,但很难从这个页面获得任何帮助-http://php.net/manual/en/function.imagejpeg.php

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

【问题讨论】:

  • 当客户端收集发送给同一用户的所有电子邮件并将其存储在一个历史记录中时,可能会发生这种情况。假设您删除所有发送给自己的内容,然后再次测试
  • @Newben 我也试过了..clearing/deleting 但仍然没有工作,如果你只是通过更改 $to = "myemail@gmail.com"; 来尝试使用此代码;到您的电子邮件中,您肯定会更好地理解问题
  • 调试提示: 将请求微时间 ($_SERVER['REQUEST_TIME_FLOAT']) 插入邮件。然后,您将查看电子邮件是否在完全相同的时间发送,或者是否有一些小的差异。您可以通过会话准确地知道何时发送多封电子邮件、由哪个脚本、由哪个确切请求初始化来进一步改进这一点。
  • 你的目标是什么?图像应该嵌入到您的电子邮件中还是存储在您的网络服务器上并由电子邮件简单引用?
  • @EnnoGröper 实际上我有一些动态内容我想嵌入到图像上(有点水印,但不完全是)。我有一个图像和一个文本(用户名),而在一个代码点我正在发送一封电子邮件..其中包含该图像。如果它的 gmail/yahoo 很好..但问题出在 Outlook 上,即我正在尝试这个东西

标签: image php php-gd


【解决方案1】:

您的 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;

【讨论】:

  • 最初 Swapnesh 代码甚至对我有用,但您的代码要好得多..感谢您的解决方案..精心制作..感谢您的每一次努力 :)
【解决方案2】:

是的,你做错了。 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
 );

【讨论】:

  • 请不要改变游戏规则。
  • @GabrielSantos:乍一看,我同意。但是如果 user1594368 想要将图像嵌入到电子邮件中,他必须使用其他工具。对于多 mime 邮件,使用 mail() 函数是个坏主意。 PHP 手册建议使用 PEAR::Mail_Mime。
  • @Dracony 我不想为此使用其他工具,尽管为您的努力 +1
【解决方案3】:

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

<?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

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2018-04-25
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    相关资源
    最近更新 更多