3

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 :(

Community
  • 1
  • 1
Trialcoder
  • 5,816
  • 9
  • 44
  • 66
  • It can happen when that the client collect all the emails coming for the same user and store it in one single history. Suppose you delete all you sent to yourself, and test it again – epsilones Dec 31 '12 at 04:13
  • @Newben i tried even that too..clearing/deleting but still no work, if you gave a try with this code just by changing $to = "myemail@gmail.com"; to your email you will definitely understand the problem better – Trialcoder Dec 31 '12 at 04:21
  • ***Debug Hint:*** Insert the request micro time (`$_SERVER['REQUEST_TIME_FLOAT']`) into the mail. You then will see if the email is sent at the exact same time or if there is some little difference. You can further improve that with a session knowing exactly when more than one email is being sent, by which script, initialized by which exact request. – hakre Jan 05 '13 at 19:28
  • What is your goal? Should the image be embedded into your email or stored on your webserver and simply referenced by the email? – Enno Gröper Jan 06 '13 at 11:07
  • @EnnoGröper Actually I have some dynamic content that i want to embed over the image(kinda watermarking, but not exactly). I have a image and a text (user names) while at a point of code I am sending an email..which contains that image.if its gmail/yahoo its fine..but the problem is with Outlook, i.e i am trying this thing – Trialcoder Jan 07 '13 at 04:34

3 Answers3

2

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;
Community
  • 1
  • 1
Jon Hulka
  • 1,259
  • 10
  • 15
  • Initially Swapnesh code even worked for me but your code is much better..thanks for the solution..very well crafted..thx for every effort :) – Trialcoder Jan 07 '13 at 04:40
1

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
 );
Dracony
  • 842
  • 6
  • 15
  • 1
    Please, don't change the rules of game. – Gabriel Santos Jan 01 '13 at 07:52
  • @GabrielSantos: At first glance, I agree. But if user1594368 wants to embed the image into the email, he has to use other tools. To use mail() function is a bad idea for multi mime mails. The PHP manual recommends using PEAR::Mail_Mime. – Enno Gröper Jan 06 '13 at 11:15
  • @Dracony I dont want to use different tool for this, although +1 for your efforts – Trialcoder Jan 07 '13 at 04:41
1

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

Community
  • 1
  • 1
swapnesh
  • 26,318
  • 22
  • 94
  • 126