Attach on-the-fly JPG to email.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
atomique
Forum Newbie
Posts: 6
Joined: Fri May 23, 2008 3:08 pm

Attach on-the-fly JPG to email.

Post by atomique »

Hello all -

I'm trying to export an image from flash and send that image through email using PHP. The jpg is exporting just fine, but I am uncertain as to how to attach it to an email. I'm using sephiroth's print class to export the jpg. Here is the code that renders the image in a browser:

Code: Select all

<?php
 
error_reporting(0);
/**
 * Get the width and height of the destination image
 * from the POST variables and convert them into
 * integer values
 */
$w = (int)$_POST['width'];
$h = (int)$_POST['height'];
 
// create the image with desired width and height
$img = imagecreatetruecolor($w, $h);
 
// now fill the image with blank color
// do you remember i wont pass the 0xFFFFFF pixels
// from flash?
imagefill($img, 0, 0, 0xFFFFFF);
 
$rows = 0;
$cols = 0;
 
// now process every POST variable which
// contains a pixel color
for($rows = 0; $rows < $h; $rows++){
    // convert the string into an array of n elements
    $c_row = explode(",", $_POST['px' . $rows]);
    for($cols = 0; $cols < $w; $cols++){
        // get the single pixel color value
        $value = $c_row[$cols];
        // if value is not empty (empty values are the blank pixels)
        if($value != ""){
            // get the hexadecimal string (must be 6 chars length)
            // so add the missing chars if needed
            $hex = $value;
            while(strlen($hex) < 6){
                $hex = "0" . $hex;
            }
            // convert value from HEX to RGB
            $r = hexdec(substr($hex, 0, 2));
            $g = hexdec(substr($hex, 2, 2));
            $b = hexdec(substr($hex, 4, 2));
            // allocate the new color
            // N.B. teorically if a color was already allocated
            // we dont need to allocate another time
            // but this is only an example
            $test = imagecolorallocate($img, $r, $g, $b);
            // and paste that color into the image
            // at the correct position
            imagesetpixel($img, $cols, $rows, $test);
        }
    }
}
 
// print out the correct header to the browser
header("Content-type:image/jpeg");
// display the image
imagejpeg($img, "", 90);
?>
Here is the mailform I am using. It is currently working in flash without trying to attach the image:

Code: Select all

<?
include("proc_form_vars.php");
//standard form values
$toMail     = procformvar("post","toEmail","");
$toName     = procformvar("post","toName","");
$fromMail     = procformvar("post","fromEmail","");
$fromName     = procformvar("post","fromName","");
$dl         = procformvar("post","dl","");
 
//mailformvars("a@a.com","");
 
$subject = "$emailSubject";
$headers = "From: $fromMail\n";
$headers = $headers . "MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1";
$scriptpath = str_replace("send_email.php","/",$_SERVER["SCRIPT_NAME"]);
$serverPath = "http://". $_SERVER["HTTP_HOST"].$scriptpath;
$linkUrl = $serverPath."test.html";
$emailSubject     = "You've received a customized wallpaper!";
//$linkUrl = $siteURL.$dl;
 
//$linkUrl = $serverPath;
$mailText = "<font face=\"Arial\"> $toName - <br>
$fromName has sent you a customized wallpaper!
</font>";
mailformvars("a@a.com");
if (mail($toMail,$fromName." "."$emailSubject",$mailText,$headers)) {
    echo "&script_message=ok";
} else {
    echo "&script_message=failed";
}
?>
Any suggestions on how I go about combining the two?

Thanks for any help you can give!
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Attach on-the-fly JPG to email.

Post by dbemowsk »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Attach on-the-fly JPG to email.

Post by John Cartwright »

I would suggest you take a look at Swiftmailer.org
atomique
Forum Newbie
Posts: 6
Joined: Fri May 23, 2008 3:08 pm

Re: Attach on-the-fly JPG to email.

Post by atomique »

Okay, thanks. I haven't really worked with PHP before so I'm very confused.

The drquincy link looks like something I could figure out, but it calls for a file name... is there a way to attach an image that's created on the fly? In the original script that renders the image once it's exported from flash, it isn't even given a file name. I'm basically trying to write the script that renders it and then sends it along to the recipient. The recipent and the sender variables are defined through a flash form, so the php file has to pick up those variables as well.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Attach on-the-fly JPG to email.

Post by Chris Corbyn »

atomique wrote:Okay, thanks. I haven't really worked with PHP before so I'm very confused.

The drquincy link looks like something I could figure out, but it calls for a file name... is there a way to attach an image that's created on the fly? In the original script that renders the image once it's exported from flash, it isn't even given a file name. I'm basically trying to write the script that renders it and then sends it along to the recipient. The recipent and the sender variables are defined through a flash form, so the php file has to pick up those variables as well.
Use Swift Mailer:

Code: Select all

$message = new Swift_Message('Subject here');
$message->attach(new Swift_Message_Part('Message here'));
$message->attach(new Swift_Message_Attachment($imageData, 'filename-to-be-displayed.jpg', 'image/jpg'));
atomique
Forum Newbie
Posts: 6
Joined: Fri May 23, 2008 3:08 pm

Re: Attach on-the-fly JPG to email.

Post by atomique »

I don't understand how to implement that. Is there any way I can combine the two scripts I already have?
atomique
Forum Newbie
Posts: 6
Joined: Fri May 23, 2008 3:08 pm

Re: Attach on-the-fly JPG to email.

Post by atomique »

Alright, this is what I've come up with, but it's still not working. Am I close?

Code: Select all

<?
error_reporting(0);
include("proc_form_vars.php");
 
$toMail     = procformvar("post","toEmail","");
$toName     = procformvar("post","toName","");
$fromMail   = procformvar("post","fromEmail","");
$fromName   = procformvar("post","fromName","");
$dl         = procformvar("post","dl","");
$subject = "You've received a Custom Wallpaper!";
$headers = "From: $fromMail\n";
$headers = $headers . "MIME-Version: 1.0\nContent-type: multipart/mixed; boundary=\"BOUNDARY\"; \n";
$headers = $headers . "Content-Transfer-Encoding: 7bit\n";
$headers = $headers . "This part of the E-mail should never be seen.\n--BOUNDARY\n";
$scriptpath = str_replace("send_email.php","/",$_SERVER["SCRIPT_NAME"]);
$serverPath = "http://". $_SERVER["HTTP_HOST"].$scriptpath;
$linkUrl = $serverPath."test.html";
$mailText = "<font face=\"Arial\"> $toName - <br>
$fromName has sent you a customized wallpaper!
</font>";
 
 
$w = (int)$_POST['width'];
$h = (int)$_POST['height'];
 
$img = imagecreatetruecolor($w, $h);
imagefill($img, 0, 0, 0xFFFFFF);
 
$rows = 0;
$cols = 0;
for($rows = 0; $rows < $h; $rows++){
    $c_row = explode(",", $_POST['px' . $rows]);
    for($cols = 0; $cols < $w; $cols++){
        $value = $c_row[$cols];
        if($value != ""){
            $hex = $value;
            while(strlen($hex) < 6){
                $hex = "0" . $hex;
            }
            $r = hexdec(substr($hex, 0, 2));
            $g = hexdec(substr($hex, 2, 2));
            $b = hexdec(substr($hex, 4, 2));
            $test = imagecolorallocate($img, $r, $g, $b);
            imagesetpixel($img, $cols, $rows, $test);
        }
    }
}
 
//$headers .= "Content-type:image/jpeg; filename=\"wallpaper.jpg\";");
//imagejpeg($img, "", 100);
$fileatt = $img;
$fileatttype = "image/jpg"; 
$fileattname = "wallpaper.jpg";
$file = fopen( $fileatt, 'rb' ); 
$data = fread( $file, filesize( $fileatt ) ); 
fclose( $file );
 
$semi_rand = md5( time() ); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
$headers .= "\nMIME-Version: 1.0\n" . 
            "Content-Type: multipart/mixed;\n" . 
            " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . 
            "--{$mime_boundary}\n" . 
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
             "Content-Transfer-Encoding: 7bit\n\n" . 
            $message . "\n\n";
            $data = chunk_split( base64_encode( $data ) );
?>
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Attach on-the-fly JPG to email.

Post by dbemowsk »

OK, this is the function that I use.

Code: Select all

 
        function sendmsg($from, $to, $subject, $msgtext, $fileatt, $fileattname = NULL) {
            $fileatttype = $this->getMimeType($fileatt);
            $fileattname = (is_null($fileattname)) ? basename($fileatt) : $fileattname;
            if ($fileatttype) {
                $headers = "From: ".$from;
 
                $file = fopen( $fileatt, 'rb' );
                $data = fread( $file, filesize( $fileatt ) );
                fclose( $file );
 
                $semi_rand = md5( time() );
                $mime_boundary = "==Multipart_Boundary_x".$semi_rand."x";
 
                $headers .= "\nMIME-Version: 1.0\n" .
                            "Content-Type: multipart/mixed;\n" .
                            " boundary=\"".$mime_boundary."\"";
 
                $message = "This is a multi-part message in MIME format.\n\n" .
                        "--".$mime_boundary."\n" .
                        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                        "Content-Transfer-Encoding: 7bit\n\n" .
                        $msgtext . "\n\n";
 
                $data = chunk_split( base64_encode( $data ) );
 
                $message .= "--".$mime_boundary."\n" .
                         "Content-Type: ".$fileatttype.";\n" .
                         " name=\"".$fileattname."\"\n" .
                         "Content-Disposition: attachment;\n" .
                         " filename=\"".$fileattname."\"\n" .
                         "Content-Transfer-Encoding: base64\n\n" .
                         $data . "\n\n" .
                         "--".$mime_boundary."--\n";
                return (mail($to, $subject, $message, $headers)) ? true : false;
            } else {
                return false;
            }
        } //End function sendmsg
 
This function has 6 arguments $from, $to, $subject, $msgtext, $fileatt, $fileattname. Most of them are self explanatory. $fileatt is the attachement file that you want to include with the e-mail. $fileattname is an optional argument that allows you to specify a name for the file other than the original file name.

You should be able to figure it out from there.
Post Reply