The code is sending 2 times without proper image

南楼画角 提交于 2019-12-12 16:04:06

问题


I tried this code from the post, but this code is sending two emails on a single run of file.

Email file is sending email two times using php mail function

Let me know what i am doing wrong -

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

Let me know what i am doing wrong, is this the correct method I am using this --

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

I followed this URL but hard to crack any help from this page- http://php.net/manual/en/function.imagejpeg.php

I even tried keeping that method mytextoverimage() in another file but still no help, email sending twice :(


回答1:


Your mytextoverimage() function doesn't return anything - it just sends a jpeg image to the browser.

I've reworked your code to send the same image via email - note that just the image is sent, no HMTL.

If you want to send an image as part of an HTML document, you need to go a step further and create a multipart message - check out How to attach and show image in mail using php?

This works for Gmail on Iceweasel 10.0.11.

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



回答2:


Yes you are doing it wrong. Imagejpg function returns and image, but you need a url to put it inside a tag. What you should do is use SWIFT mailer and send that image you created as ann attachment to the email. you can read on it here: http://swiftmailer.org/docs/messages.html

It would belike this:

 //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
 );



回答3:


As per as my question was concerned I solved it, like in this way -

<?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!!";          

?>

Just save the code in any root file "Yourfile.php" and run.

This will create an image and save to root location(you can force to save it another location also).

Follow these two links as well -

LINK 1 LINK 2



来源:https://stackoverflow.com/questions/14069231/the-code-is-sending-2-times-without-proper-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!