3

My three concerns, tried different combos with no result, googled but little or no help --

  1. I received two times the email,change myemail@emailserver.com to the email id to see the result.
  2. While executing this file I am getting image, however I want to have text "Email Sent".
  3. Full HTML content with tag is passed instead of HTML rendering in email.

My Working Code -->

<?php
    header('Content-type: image/jpeg');
    $jpg_image = imagecreatefromjpeg('http://dummyimage.com/600x400/f5f5f5/fff.jpg');
    $black = imagecolorallocate($jpg_image, 1, 1, 1);
    $font_path = 'myfont/arial.ttf';
    $text = "Swapnesh Sinha!";
    imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
    $tip = imagejpeg($jpg_image);
    $imageData = base64_encode($tip);
    //$src = 'data: '.mime_content_type($jpg_image).';base64,'.$imageData;
    imagedestroy($jpg_image);
?>

<html>
<head></head>
<body>
<p>

<?php

$to = 'myemail@emailserver.com';
$subject = "Thisa is a email test to find image work";
$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 SITE TITLE</title>
</head><body><table><tr><td>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</td></tr><tr><img src="'.'http://mysiteurl/addtext.php'.'" /></tr></table></body></html>';
$headers = 'From: myemail@emailserver.com' . "\r\n" .
           'Reply-To: myemail@emailserver.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

$bool = mail($to, $subject, $message, $headers);

if($bool)
echo "Email Sent"; 
else
echo "Email Not Sent";

?>

</body>
</html>

NOTE - In <img src="'.'http://mysiteurl/addtext.php'.'" />

http://mysiteurl/addtext.php is the same where we have all this above content.

swapnesh
  • 26,318
  • 22
  • 94
  • 126

1 Answers1

2

First thing I'd do is check your apache/IIS logs to ensure that the URL isn't being called twice (just a sanity check).

If the PHP page you've added to your OP is http://mysiteurl/addtext.php, then it would be called twice, once renderering the HTML, then the browser would call it again when rendering the <img ...> tag.

To fix this you need to either split it into two PHP files (recommended), or pass a GET parameter to toggle the image processing.

You'll also need to add $headers .= "Content-type: text/html\r\n"; so that the email is rendered as html and not plain text.

hafichuk
  • 10,351
  • 10
  • 38
  • 53
  • from where to where or which code i need to separate from this file ? – swapnesh Dec 27 '12 at 05:38
  • Create a second file that doesn't return HTML and only the image, say `img-with-text.php` that *only* does the image processing and returns the image content type. I.e. the first php block you have in your OP. Then in the HTML page change the image tag to – hafichuk Dec 27 '12 at 05:47
  • very close now ..the only thing is my html tags are coming in the same manner in the email(rendering of html tag is not processed :( in received email ) – swapnesh Dec 27 '12 at 05:53
  • 1
    You just need to set the headers for the email. `$headers .= "Content-type: text/html\r\n";` – hafichuk Dec 27 '12 at 05:56
  • ...Perfect..thx for all your help..already upvoted and going to accept it as an answer..just add headers thing in your answer so that it clearly reflects your efforts once done i will accept as an answer then..cheers!! :) – swapnesh Dec 27 '12 at 06:04