Archive for July, 2009

PHP mail function used to send attachments

PHP attachement mail function,
Using this function which is very usefull for sending mail with attachment.

You can attached jpg, gif, png, pdf, els, doc etc.
This function is used attach multiple files.

The size of attchement is totaly depends on server php.ini setting
$AttmFiles is an array.

You just pass multiple file path which you want to attach.

This mail function return true after succesion other wise return false.

 

 

Veriable description

Function parameters

$From —> Sender mail address
$FromName —> Sender’s Name
$To —> Receiver’s Mail address
$ToName —> Receivers’s Name
$Subject —> Subject
$Text —> Plain text message
$Html —> Html message
$AttmFiles —>Array. files to send in attachment

function SendMail($From,$FromName,$To,$ToName,$Subject,$Text,$Html,

$AttmFiles){

$OB=”—-=_OuterBoundary_000″;

$IB=”—-=_InnerBoundery_001″;

$Html=$Html?$Html:preg_replace(“/\n/”,”{br}”,$Text) or die(“neither text nor html part present.”);

$Text=$Text?$Text:”Sorry, but you need an html mailer to read this mail.”;

$From or die(“sender address missing”);

$To or die(“recipient address missing”);

$headers =”MIME-Version: 1.0\r\n”;

$headers.=”From: “.$FromName.” <“.$From.”>\n”;

$headers.=”To: “.$ToName.” <“.$To.”>\n”;

$headers.=”Reply-To: “.$FromName.” <“.$From.”>\n”;

$headers.=”X-Priority: 1\n”;

$headers.=”X-MSMail-Priority: High\n”;

$headers.=”X-Mailer: My PHP Mailer\n”;

$headers.=”Content-Type: multipart/mixed;\n\tboundary=\””.$OB.”\”\n”;

//Messages start with text/html alternatives in OB

$Msg =”This is a multi-part message in MIME format.\n”;

$Msg.=”\n–“.$OB.”\n”;

$Msg.=”Content-Type: multipart/alternative;\n\tboundary=\””.$IB.”\”\n\n”;

//plaintext section

$Msg.=”\n–“.$IB.”\n”;

$Msg.=”Content-Type: text/plain;\n\tcharset=\”iso-8859-1\”\n”;

$Msg.=”Content-Transfer-Encoding: quoted-printable\n\n”;

// plaintext goes here

$Msg.=$Text.”\n\n”;

// html section

$Msg.=”\n–“.$IB.”\n”;

$Msg.=”Content-Type: text/html;\n\tcharset=\”iso-8859-1\”\n”;

$Msg.=”Content-Transfer-Encoding: base64\n\n”;

// html goes here

$Msg.=chunk_split(base64_encode($Html)).”\n\n”;

// end of IB

$Msg.=”\n–“.$IB.”–\n”;

// attachments

if($AttmFiles){

foreach($AttmFiles as $AttmFile){

$patharray = explode (“/”, $AttmFile);

$FileName=$patharray[count($patharray)-1];

$Msg.= “\n–“.$OB.”\n”;

$Msg.=”Content-Type: application/octetstream;\n\tname=\””.$FileName.”\”\n”;

$Msg.=”Content-Transfer-Encoding: base64\n”;

$Msg.=”Content-Disposition: attachment;\n\tfilename=\””.$FileName.”\”\n\n”;

//file goes here

$fd=fopen ($AttmFile, “r”);

$FileContent=fread($fd,filesize($AttmFile));

fclose ($fd);

$FileContent=chunk_split(base64_encode($FileContent));

$Msg.=$FileContent;

$Msg.=”\n\n”;

}

}

//message ends

$Msg.=”\n–“.$OB.”–\n”;

// mailing section

if(mail($To,$Subject,$Msg,$headers)){

return true;

}

else{

return false;

}

}

cookies

Lastly, a quick word about cookies. While it’s easy enough to set cookies in .htaccess without any mod_rewrite..

create a cookie called “example-cookie”, and set its value to “true”…..
Header set Set-Cookie “example-cookie=true”….

..you will need it to read the cookie information back again, and “do stuff” with it. It’s easy. For example, to check if the above cookie exists and has the correct value set, we could simply do..

check for that same cookie + value..
Options + FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !my-tools=true
RewriteRule .* /err/401.php

..which could easily form the basis of a simple authentication system. As with any RewriteCond, you can get pretty complex, checking multiple cookies, utilizing regexp and more, but that’s enough to get you started.

Hope you will enjoy this…… 🙂

Prevent hot-linking using htaccess

Believe it or not, there are some webmasters who, rather than coming up with their own content will steal yours. Really! Even worse, they won’t even bother to copy to their own server to serve it up, they’ll just link to your content! no, it’s true, in fact, it used to be incredibly common. These days most people like to prevent this sort of thing, and .htaccess is one of the best ways to do it.

This is one of those directives where the mileage variables are at their limits, but something like this works fine for me..

how DARE they!
Options +FollowSymlinks
# no hot-linking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com/ [NC]
RewriteRule .*\.(gif|jpg|png)$ http://mydomain.com/img/hotlink.png [NC]

You may see the last line broken into two, but it’s all one line (all the directives on this page are). Let’s have a wee look at what it does..

We begin by enabling the rewrite engine, as always.

The first RewriteCond line allows direct requests (not from other pages – an “empty referrer”) to pass unmolested. The next line means; if the browser did send a referrer header, and the word “mydomain.com” is not in the domain part of it, then DO rewrite this request.

The all-important final RewriteRule line instructs mod_rewrite to rewrite all matched requests (anything without “mydomain.com” in its referrer) asking for gifs, jpegs, or pngs, to an alternative image.