Email file is sending email two times using php mail function - gd

My three concerns, tried different combos with no result, googled but little or no help --
I received two times the email,change myemail#emailserver.com to the email id to see the result.
While executing this file I am getting image, however I want to have text "Email Sent".
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.

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.

Related

Print INR symbol in PDF using DomPDF

I want to print INR (Indian rupee) symbol in PDF but failed, when PDF got generated it shows ? instead on ₹.
"barryvdh/laravel-dompdf": "^0.8.3"
I have generated PDF like :
use Barryvdh\DomPDF\Facade as PDF;
$pdf = PDF::loadView('report-test')->setPaper('a4');
return $pdf->stream('payslip.pdf');
PDF Html :
<p>Price ₹ 9500 = ₹ 9500</p>
<p>Price ₹ 5500 = ₹ 5500</p>
<p>Price ₨ 500 = ₨ 500</p>
<p>Price ₨ 2000 = ₨ 2000</p>
<p>Price ₹ 2000 = ₹ 2000</p>
<p style="font-family: dejavusans !important;">Price र rupee </p>
<p>I will display ₹</p>
<p>I will display ₹</p>
Also i have added <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> in header, still no luck.
Anything i am missing?
Thanks in Advance

noCaptcha: Prevent auto reload to catch failed attempt

I am integrating NoCaptcha to a website and want to catch failed captchas myself. Sadly, the "image selection" Captcha reloads immediately if it was not correctly solved.
So, if there is a challenge like "Pick all images showing coffee" and a user does not select all corresponding images correctly, the challenge reloads immediately. But I want the data (and form) to be posted anyway and check the Captcha correctness on my own.
Here is a minimalistic example of how it should work. I am sure, it would work, if the Captcha not reloaded instantly.
<?php
require_once('recaptchalib.php');
$publickey = "-----";
$privatekey = "-----";
try{
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
if(!$captcha){ exit; }
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false){
// Do something
} else {
// Do something else
}
}
else
{
?>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form method="post" action="thisfile.php">
<div class="g-recaptcha" data-sitekey="<?php echo $publickey; ?>"></div>
<input type="submit" value="Absenden" />
</form>
</body>
</html>
<?php
}
} catch (Exception $e) {
error_log($e->getMessage());
}
?>
Do you know a way to prevent auto-reloading?
Thank you!
You should know operations behind reCaptcha. The Google reCaptcha API (https://www.google.com/recaptcha/api.js) does all the work for reCaptcha operation; allowing no site owner interactions (except the site verify) with its reCaptcha.
So your attempts to prevent reCaptcha auto-reloading or similar whether break its operation or cause its misbehave toward end user.

Optimize website to show reader view in Firefox

Firefox 38.0.5 added a "Reader View" to the address bar:
But not all sites get this icon, It only appears when readable content page is detected. So how do I enable this for my site?
I tried media print and an extra stylesheet for print-view, but that has no effect:
<html>
<head>
<style>
#media print { /* no effect: */
.no-print { display:none; }
}
</style>
<!-- no effect either:
<link rel="stylesheet" href="print.css" media="print"><!-- -->
</head><body>
<h1>Some Title</h1>
<img class="no-print" src="http://dummyimage.com/1024x100/000/ffffff&text=This+banner+should+vanish+in+print+view">
<br><br><br>This is the only text
</body></html>
What code snippets do I have to add into my website sourcecode so this book icon will become visible to the visitors of my site?
As the code stands in May '20 the trigger function (isProbablyReaderable) scores only p or pre elements and div elements that contain at least one decedent br.
A slight oversimplification of the scoring heuristic is:
For each element in ['p', 'pre', 'div > br']:
If textContent length is > 140 chars, increase score by sqrt(length - 140)
if cumulative score > 20, return true
You have to add <div> or <p> tags to achieve a page to iniciate the ReaderView.
I created a simple html that works:
<html>
<head>
<title>Reader View shows only the browser in reader view</title>
</head>
<body>
Everything outside the main div tag vanishes in Reader View<br>
<img class="no-print" src="http://dummyimage.com/1024x100/000/ffffff&text=This+banner+should+vanish+in+print+view">
<div>
<h1>H1 tags outside ot a p tag are hidden in reader view</h1>
<img class="no-print" src="http://dummyimage.com/1024x100/000/ffffff&text=This+banner+is resized+in+print+view">
<p>
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789 123456
</p>
</div>
</body>
</html>
This is the minimum needed to activate it. This is a somewhat multi-faceted process where scores are added for text chunks.
You can for example activate the reader view in forum's software if you add a <p>-tag around each message block in the view-posts template.
Here are some more details about the mechanism

The code is sending 2 times without proper image

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

Codeigniter Image and Source URL

I have a problem with Codeigniter URL. I have a controller "welcome.php" :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$data['tiung'] = 'index';
$this->load->view('welcome_message',$data);
}
public function dor($bus)
{
$data['tiung'] = $bus;
$this->load->view('welcome_message',$data);
}
}
and a view "welcome_message.php" :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
sesuatu <?php echo $tiung?>
<img src="fragor.jpg" width="720" height="246" alt=""/>
ladalah
</body>
</html>
If I want to access the controller function 'dor' with a parameter I used this :
localhost/hostname/index.php/welcome/dor/something
and it works, but the problem is the image isn't loaded. I tried to put the image file in the webroot folder, 'application' folder, and even in the 'views' folder. But the image still can't load. Where should I put the image file?
The best practice for this is to create an /images/ directory in your webroot (the folder with index.php) and use the base_url() function to get the link to the image, I.E.
<img src="<?php echo base_url('images/fragor.jpg'); ?>" width="720" height="246" alt=""/>
Don't forget to load the url helper either with the autoloader or manually before using site_url() or base_url().
Also if you're using CodeIgniter's rewrite rules to remove the index.php from the URL (which it doesn't look like you're doing), don't forget to exclude the /images/ directory from the rewrite.
In CodeIgniter there is a specific way to indicate an image...like
echo img('images/gautam.gif');
put this in your view file and you need to create "images" folder at your root
Though you have to add CI helper 'html' in autoload config, but this goes easy after.
<?php
$image_properties = array(
'src' => 'images/picture.jpg',
'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time',
'class' => 'post_images',
'width' => '200',
'height'=> '200',
'title' => 'That was quite a night',
'rel' => 'lightbox'
);
img($image_properties);
?>
/* <img src="site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a nigh`enter code here`t" rel="lightbox" />*/
OR JUST simple:
<?php
echo img('images/picture.jpg'); ?>
// gives <img src="site.com/images/picture.jpg" />

Resources