Page 1 of 1
How to send dynamically generated image as email attachment?
Posted: Fri Oct 28, 2011 5:24 am
by crazyphpprogrammer
Hi all,
I want to send a dynamically generated image as an email attachment.The image is generated using php to add a text to an existing image, the text is given to the file using get method. "image.php?text=hello" I want to attach this image to the email. Can any one please help.
Thanks.
Re: How to send dynamically generated image as email attachm
Posted: Fri Oct 28, 2011 6:07 am
by Celauran
What have you tried so far? What isn't working?
mail()
Re: How to send dynamically generated image as email attachm
Posted: Fri Oct 28, 2011 7:42 am
by crazyphpprogrammer
Celauran wrote:What have you tried so far? What isn't working?
mail()
Hi Celauran,
Now I have created a function to create the image and now the image is getting attached but is not showing in the mail body. This is the code I have used
Code: Select all
<?php
//print_r($_REQUEST);
$name=addslashes(trim($_POST['name']));
$email=addslashes(trim($_POST['email']));
$friendsname=addslashes(trim($_POST['friendsname']));
$friendsemail=addslashes(trim($_POST['friendsemail']));
$mes=addslashes(trim($_POST['message']));
$rad=$_POST['radio'];
$mes=str_replace(" ","%20",$mes);
$s=create_image($mes,$rad);
sendmail($s);
function sendmail($src)
{
$headers = "From: Me <me@email.com>";//put you own stuff here or use a variable
$to = 'email@dummydomain.com';// same as above
$subject = 'Testing Inline attachment HTML Emails';//your own stuff goes here
$html ="<img src='image.jpg'><br /><br />
<b>This</b> is HTML <span style='background:cyan'>and this is a cyan highlight</span>
<br />So this should be a new line.<br /><br />This should be a new line with a space between the above.
<br />";//make up your own html or use an include
//the below is your own plain text message (all the $message(x))
$message0 = 'Dear valued customer,';// or make up your own for plain text message
//Now lets set up some attachments (two in this case)
//first file to attach
$fileatt_name2 = "image.jpg";//just the name of the file here
// Generate a boundary string that is unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"<font face=Arial>" .
$html."\r\n";
$message .= "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message0 . "\n\n";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Base64 encode the file data
$data2 = chunk_split(base64_encode($src));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: image/jpg;\n" . // {$fileatt_type}
" name=\"{$fileatt_name2}\"\n" .
"Content-Disposition: inline;\n" .
" filename=\"{$fileatt_name2}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data2 . "\n\n" .
"--{$mime_boundary}--\n";
// Add another file attachment to the message as many as you have
// Send the message
$send = mail($to, $subject, $message, $headers);
if ($send) {
echo "<p>Email Sent to intended recipients successfully!</p>";
} else {
echo "<p>Mail could not be sent. You missed something in the script. Sorry!</p>";
}
}
/*-------------image create----------*/
function create_image($text,$rad)
{
if(empty($text)) fatal_error('Error: No text specified.') ;
if(empty($text))
fatal_error('Error: Text not properly formatted.') ;
// customizable variables
$font_file = 'MTCORSVA.TTF';
$font_size = 14 ; // font size in pts
$font_color = '#ffffff' ;
if($rad==1)
{
$image_file = 'images/ecards/e1_hover.jpg';
}
elseif($rad==2)
{
$image_file = 'images/ecards/e2_hover.jpg';
}
elseif($rad==3)
{
$image_file = 'images/ecards/e3_hover.jpg';
}
elseif($rad==4)
{
$image_file = 'images/ecards/e4_hover.jpg';
}
elseif($rad==5)
{
$image_file = 'images/ecards/e5_hover.jpg';
}
elseif($rad==6)
{
$image_file = 'images/ecards/e6_hover.jpg';
}
elseif($rad==7)
{
$image_file = 'images/ecards/e7_hover.jpg';
}
elseif($rad==8)
{
$image_file = 'images/ecards/e8_hover.jpg';
}
// x and y for the bottom right of the text
// so it expands like right aligned text
$x_finalpos = 250;
$y_finalpos = 323;
// trust me for now...in PNG out PNG
$mime_type = 'image/jpg' ;
$extension = '.jpg' ;
$s_end_buffer_size = 4096 ;
// check for GD support
if(!function_exists('ImageCreate'))
fatal_error('Error: Server does not support PHP image generation') ;
// check font availability;
if(!is_readable($font_file)) {
fatal_error('Error: The server is missing the specified font.') ;
}
// create and measure the text
$font_rgb = hex_to_rgb($font_color) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
$text_width = abs($box[2]-$box[0]);
$text_height = abs($box[5]-$box[3]);
$image = imagecreatefromjpeg($image_file);
if(!$image || !$box)
{
fatal_error('Error: The server could not create this image.') ;
}
// allocate colors and measure final text position
$font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;
$image_width = imagesx($image);
//$put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);
//$put_text_y = $y_finalpos;
$put_text_x =30;
$put_text_y =320;
// Write the text
imagettftext($image, $font_size, 0, $put_text_x, $put_text_y, $font_color, $font_file, $text);
ob_start();
ImageJPEG($image) ;
$image_as_string = ob_get_clean();
ImageDestroy($image) ;
return($image_as_string);
}
/*
decode an HTML hex-code into an array of R,G, and B values.
accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
*/
function hex_to_rgb($hex) {
// remove '#'
if(substr($hex,0,1) == '#')
$hex = substr($hex,1) ;
// expand short form ('fff') color to long form ('ffffff')
if(strlen($hex) == 3) {
$hex = substr($hex,0,1) . substr($hex,0,1) .
substr($hex,1,1) . substr($hex,1,1) .
substr($hex,2,1) . substr($hex,2,1) ;
}
if(strlen($hex) != 6)
fatal_error('Error: Invalid color "'.$hex.'"') ;
// convert from hexidecimal number systems
$rgb['red'] = hexdec(substr($hex,0,2)) ;
$rgb['green'] = hexdec(substr($hex,2,2)) ;
$rgb['blue'] = hexdec(substr($hex,4,2)) ;
return $rgb ;
}
?>