Wednesday, February 17, 2010

PHP - Send email with attachment | Email script

I've tested this in gmail, yahoo, and hotmail. So far, it worked fine.

code:


function send_email($data, $subject, $content, $attachment = NULL) {

$name_from = $data["name_from"]; // Who the email is from
$email_from = $data["email_from"]; // Who the email is from
$email_to = $data["email_to"]; // Who the email is to
$email_cc = $data["email_cc"]; // Who the email is cc
$email_bcc = $data["email_bcc"]; // Who the email is bcc
$fileatt = $attachment["tmp_name"]; // Path to the file
$fileatt_type = $attachment["type"]; // File Type
$fileatt_name = $attachment["name"]; // Filename that will be used for the file as the attachment
$email_subject = $subject; // The Subject of the email
$email_message = $content; // Message that the email has in it

$semi_rand = md5(time());
$mime_boundary = $semi_rand;

$headers = "From: $name_from<$email_from>\r\n";
$headers .= ($email_bcc != "")? "Bcc: ". $email_bcc ."\r\n" : "";
$headers .= ($email_cc != "")? "Cc: ". $email_cc ."\r\n" : "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$mime_boundary."\r\n";

// This two steps to help avoid spam
//$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
//$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

if (!is_null($attachment)) {
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
} else {
$email_message .= "--{$mime_boundary}--\n";
}
$ok = mail($email_to, $email_subject, $email_message, $headers);
return ($ok)? true : false;
}


note: $data and $attachment are arrays

Thursday, January 21, 2010

PHP - Dynamically resize an image without storing it

It is really a hassle to resize an image uploaded to be stored in different sizes, why not we resize it dynamically? Here I will show you how to generate an image with php for different sizes as we wish:

code:

<?php
$newwidth = $_GET["width"];
$newheight = $_GET["height"];
$image = $_GET["url"];
$ratio = ($_GET["ratio"] != "")? $_GET["ratio"] : "false";
$dst_x = ($_GET["dst_x"] != "")? intval($_GET["dst_x"]) : 0;
$dst_y = ($_GET["dst_y"] != "")? intval($_GET["dst_y"]) : 0;
$src_x = ($_GET["src_x"] != "")? intval($_GET["src_x"]) : 0;
$src_y = ($_GET["src_y"] != "")? intval($_GET["src_y"]) : 0;

$src = imagecreatefromjpeg($image);
list($width,$height)=getimagesize($image);
$newwidth = ($newwidth != "")? $newwidth : $width;
$newheight = ($newheight != "")? $newheight : $height;
if ($ratio == "true") {
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,$dst_x,$dst_y,$src_x,$src_y,$newwidth,$newheight,$width,$height);
imagejpeg($tmp);
header("Content-type: image/jpeg");
?>


How to use it :
-Save above as image.php
-Use it like : image.php?url=http://t2.gstatic.com/images?q=tbn:ZqzO2ew7hmrDJM%3Ahttp://farm3.static.flickr.com/2204/2403693037_0b63bdc4b4.jpg&width=100&height=100&ratio=true

Monday, December 21, 2009

Thursday, November 19, 2009

AS3 ExternalInterface | How to use ExternalInterface in AS3 | javascript to call function in flash using ExternalInterface

With ExternalInterface function in AS3, we can call javascript functions that are outside flash and vice versa.


code (flash):


//we can set a callback to our function in flash to enable javascript function to call it
if (ExternalInterface.available) {
ExternalInterface.addCallback("send_from_external", send_from_external);
}
function send_from_external(newText) {
trace(newText);
}

//to call a javascript function from flash
if (ExternalInterface.available) {
ExternalInterface.call("updateMsg", "your string");
}


code (html):

<script language="JavaScript">
function getFlashId(idIe, idMoz) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? idIe : idMoz;
}
function send_message() {
$("#txt_area").attr("disabled", "disabled");
getFlashMovie(getFlashId("swfObject", "swfEmbed")).send_from_external("test_string"); //trigger function inside flash
}
function updateMsg(string) {
alert(string); //from flash
}
</script>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="swfObject"
width="1"
height="1"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
<param name="movie" value="test.swf" />
<param name="quality" value="low" />
<param name="allowScriptAccess" value="always" />
<param name="flashVars" value="" />
<embed id="swfEmbed"
src="test.swf"
quality="low"
width="1"
height="1"
name="ExternalInterfaceExample"
align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
flashVars="">
</embed>
</object>

Wednesday, November 11, 2009

AS3 LocalConnection | local connection between flash | flash internal connection

Here're the basic structure to simple LocalConnection for flash as3. LocalConnection is used when you want to connect 2 or more swf in a pc to exchange data.

To receive:

//receive
var receivingLC:LocalConnection;
receivingLC = new LocalConnection();
receivingLC.client = this;
receivingLC.connect('session_name');

function myMethod(textRecieved:String){
//do your things
}


To send:

//sending
var sendingLC:LocalConnection;
sendingLC = new LocalConnection();
sendingLC.send('message_input', 'myMethod', "test");
sendingLC.addEventListener(StatusEvent.STATUS, onStatus);

function onStatus(event:StatusEvent){
switch (event.level) {
case "error":
trace("LocalConnection.send() failed");
break;
case "status":
trace("LocalConnection.send() succeeded");
break;
default:break;
}
}

Wednesday, October 28, 2009

AS3 simple email validation check

function checkMail(pmail:String) {
if (! (pmail.lastIndexOf(".") <= pmail.indexOf("@") || pmail.indexOf("@")==-1)) {
return true;
} else {
return false;
}
}

Tuesday, October 27, 2009

Chrome and safari css hacks

@media screen and (-webkit-min-device-pixel-ratio:0) {
#div { properties:value; }
}