1. Sending email with multiple attachment using php’s mail() function
All we know how to send email in php. You might not be aware with sending email with attachment in php. If so than refer my previous article which explains how to send email with attachment(single). Today i have a special requirement where i need to send multiple attachments (more than one) in mail. If you are looking for send function for sending email with multiple attachment in php than here i am sharing the function using which you can send an email with mulitple attachment using php's mail function.
<?php
$files = array("file_1.ext","file_2.ext","file_3.ext",……); // files
$to = "raxit4u2@gmail.com";
$from = "raxit4u2@yahoo.com";
$sub ="Testing for multiple attachment";
$msg = "Attachment mail testing";
$headers = "From: $from";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$msg = "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" . $msg . "\n\n";
// attachments code starts
for($x=0;$x<count($files);$x++)
{ $msg .= "–{$mime_boundary}\n";
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$msg .= "–{$mime_boundary}–\n";
$res = @mail($to, $sub, $msg, $headers);
if($res)
{
echo "<p>mail sent with attachments</p>";
}
else
{
echo "<p>error in processing email!</p>";
}
?>
2. Export MYSQL data into Excel/CSV via php
Export the MYSQL data into CSV/Excel file via PHP function/script. There are such requirement where client needs to Export the MYSQL data (Order data,Member data,Newsletter emails etc) into Excel sheet or CSV file for future reference or need to send to other team for future work. You can give a button or link from where client can click on it and get a Excel or CSV file with all data from MYSQL database tables using(through) PHP.
<?php
function export_excel_csv()
{
$conn = mysql_connect("localhost","root","");
$db = mysql_select_db("database",$conn);
$sql = "SELECT * FROM table";
$rec = mysql_query($sql) or die (mysql_error());
$num_fields = mysql_num_fields($rec);
for($i = 0; $i < $num_fields; $i++ )
{
$header .= mysql_field_name($rec,$i)."\\t";
}
while($row = mysql_fetch_row($rec))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) || ($value == ""))
{
$value = "\\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\\n";
}
$data = str_replace("\\r" , "" , $data);
if ($data == "")
{
$data = "\\n No Record Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\\n$data";
}
?>
What you need to do is…
1) Copy above function and paste it into your file.
2) Change MYSQL connection settings in mysql_connect("localhost","root","").
3) Change database name in mysql_select_db("database",$conn)
4) Change table name in $sql = "SELECT * FROM table".
5) Thats it
3. Get Visitors Real IP Address in PHP
If you want track visitor's or user's IP address than you may use $_SERVER['REMOTE_ADDR']. Well, you might be shocked to know that it may not return the real IP address of the visitor/user at all time. Here are the cases when it happens. If visitor/user is connected to the Internet through Proxy Server then $_SERVER['REMOTE_ADDR'] in PHP will give the IP address of the proxy server and it will not be visitor's/user's IP address.
In PHP , there are other server variables ( HTTP_CLIENT_IP , HTTP_X_FORWARDED_FOR along with REMOTE_ADDR) using which we can find the real IP address even if visitor/user is connected to Proxy Server. Here is a function in PHP to find the real IP address of the visitor's/user's PC.
function get_real_IP_address()In above PHP function, it wil first check for IP address of visitor's/user's PC and return if it get IP address. If client's IP address is not available then it will find for forwarded for IP address using HTTP_X_FORWARDED_FOR. If still it did not get IP address than it return IP address using REMOTE_ADDR.
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
4. Some of Unknown and rarely used but very helpful functions of PHP
1. Swapping two values using list() function
$x = 1;
$y = 2;
list($x, $y) = array($y, $x);
echo $x; // Outputs – 2
echo $y; // Outputs – 1
2. Compress/Uncompress strings
You can compress long strings using gzcompress() and than make an entry in to database. This will save a space and make your database lighter at some level. You can uncompress the compressed string by using gzuncompress() function.
3. Validate real time email address
What we are validating for email address is not a perfect validation. If anyone enter aaaa@aaaaaaaa123.com than this is not a valid email address. You can validate real time email by the checkdnsrr() function. It checks the email's host address if it's a valid DNS record.
3. highlight_file() function
Output HTML string with nice colored using above function. Many servers are configured to automatically highlight files with a phps extension. To use this , you have to enable phps by adding this line to the httpd.conf:
AddType application/x-httpd-php-source .phps
4. Easy manage of IP addresses as strings.
IP address can be stored as integers using the ip2long(). If you want to convert it back to IP address from integer string than use long2ip() function.
Some of the advantages to store IP address as Integers are :
1) Less memory space used
2) Searching of IP address will be faster
3) Easy to check if IP address is inbetween IP address ranges
5. __autoload() method – PHP5
The magic method __autoload() function is a convenience that allows you to include files dynamically when instant of class is created.
This function will search file name as class name and add that file at run time. This will reduce a load time because files will be included as per needs.
6. preg_split function
This function will split string to array of strings. This can also be done using the explode function. Benifits of using preg_split function is , it will eliminate empty strings.
No comments:
Post a Comment