Saturday, January 14

PHP – send email with attachment file

how to send email with attachment file in PHP

In this post we will show you how to send email with attachment in php. by using this following code is used for send email with attachment in php. following code will sending pdf file in email.

$file_name = "example.pdf"; // attachment file name
$path = "folder_name"; // path of attachment file
$user_name = "user name"; // user name
$mail_to = "onlinecode@email.com"; // to eamil
$email_subject = "Notification of attachment for user"; // subject of eamil
$body_text = "Hello ".$user_name.",\nThis demo message with file attachment. You will be notified when their review has been completed.\n Regard \n onlinecode Team";

$attachment_file = $path . "/" . $file_name; // path of file
$attachment_file_size = filesize($attachment_file);
$email_handle = fopen($attachment_file, "r");
$content = fread($email_handle, $attachment_file_size);
fclose($email_handle);// file close of email handle
$content = chunk_split(base64_encode($content));
$time_separator = md5(time());// a random hash will be necessary to send mixed content

// carriage return type (we use a PHP end of line constant)
$email_eol = PHP_EOL;
$body_message = $body_text;
// main header (multipart mandatory)
$email_headers = "From: onlinecode.org Team <onlinecode@email.co>" . $email_eol;
// sender eamil detail
$email_headers .= "MIME-Version: 1.0" . $email_eol;//MIME-Version
$email_headers .= "Content-Type: multipart/mixed; boundary=\"" . $time_separator . "\"" . $email_eol;//Content-Type
$email_headers .= "Content-Transfer-Encoding: 7bit" . $email_eol;//Content-Transfer-Encoding
$email_headers .= "This is a MIME encoded message." . $email_eol;

// eamil message
$email_headers .= "--" . $time_separator . $email_eol;
$email_headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $email_eol;
$email_headers .= "Content-Transfer-Encoding: 8bit" . $email_eol;
$email_headers .= $body_message . $email_eol;

// attachment
$email_headers .= "--" . $time_separator . $email_eol;
$email_headers .= "Content-Type: application/octet-stream; name=\"" . $file_name . "\"" . $email_eol;
$email_headers .= "Content-Transfer-Encoding: base64" . $email_eol;
$email_headers .= "Content-Disposition: attachment" . $email_eol;
$email_headers .= $content . $email_eol;
$email_headers .= "--" . $time_separator . "--";
mail($mail_to, $email_subject, ", $email_headers); //SEND Mail TO USER

 

insert category programmatically in magento

In this post we’ll show you how to magento insert category programmatically using magento. By using following code we can insert category programmatically and this code also work in exteranl magen to file.

$parentId = '2'; // prenet catagory
try{
    $category = Mage::getModel('catalog/category');
    $category->setName('category-name'); // category name
    //$category->setUrlKey('your-category-url-key'); // url key
    $category->setIsActive(1); // is active
    $category->setDisplayMode('PRODUCTS');
    $category->setIsAnchor(1); //for active anchor
    $category->setStoreId(Mage::app()->getStore()->getId());
    $_parent_category = Mage::getModel('catalog/category')->load($parentId);
    $category->setPath($_parent_category->getPath());
    $cat_data= $category->save(); // insert catagory
    //echo $cat_data->getEntityId();// print catagory id
}
catch(Exception $e)
{
    print_r($e);//Exception in insert catagory
}

Friday, January 13

customer registration programmatically in Magento code from an external php file

In this post we’ll show you how to customer registration programmatically in magento using with exteranl php file. By using following code we can register user and create seeeion on user or customer.
  • This post is use for register user or customer registration programmatically in Magento.
  • first we create user ,if user is not exist then insert data into database then we will go for login to user programmatically.
  • add you detail in variable and enjoy the code.       
set_time_limit(0);
ini_set('memory_limit', '1024M');
include_once "app/Mage.php";  // Mage file include
include_once "downloader/Maged/Controller.php"; // Controller file include
error_reporting(E_ALL | E_STRICT);

$website_Id = Mage::app()->getWebsite()->getId();
$store_detail = Mage::app()->getStore();

$setFirstname = "Firstname"; // add  First name
$setLastname = "setLastname"; // add Last name
$setEmail = "Ttest123@gm.co"; // add  Email id
$setPassword = "password@789"; // add  password
/*$setPostcode = "989898"; // add  Post code
$setCity = "Sydney "; // add  city of user
$setRegion = "New South Wales";
$setTelephone = "99999999999";
$setFax = "123456";
$setCompany = "Australia";
$setStreet = "in Australia some place";*/
$customer = Mage::getModel("customer/customer");
$customer ->setWebsiteId($website_Id)
->setStore($store_detail)
->setFirstname($setFirstname)
->setLastname($setLastname)
->setEmail($setEmail)
->setPassword($setPassword);
try{
    $results = $customer->save();   
    $getEntityId = $results->getEntityId(); // get user id
}
catch (Exception $e) {
    // bug or user is exist
    echo $e->getMessage(); // print Exception Message
}

Mage::getSingleton('core/session', array('name' => 'frontend'));

$sessionCustomer = Mage::getSingleton("customer/session");
$red_url = "http://www.onlinecode.org/";
if($sessionCustomer->isLoggedIn()) {
    header('Location: '. $red_url);
    break;
}
else{
    //echo $setEmail." ".$setPassword;
    loginUser($setEmail,$setPassword);
    //header('Location: '. $red_url);
    //break;
}
// function for login user programmatically
function loginUser($email,$password){
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));
    $website_Id = Mage::app()->getWebsite()->getId();
    $store_detail = Mage::app()->getStore();
    $customer_detail = Mage::getModel("customer/customer");
    $customer_detail->website_id = $website_Id;
    $customer_detail->setStore($store_detail);
    try
    {
        $customer_detail->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer_detail);
        $session->login($email,$password);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();// print Exception Message
    }
}

Customer / user registration programmatically in Magento

$website_Id = Mage::app()->getWebsite()->getId();
$store_detail = Mage::app()->getStore();

$setFirstname = "Firstname"; // add  First name
$setLastname = "setLastname"; // add Last name
$setEmail = "Ttest123@gm.co"; // add  Email id
$setPassword = "password@789"; // add  password
/*$setPostcode = "989898"; // add  Post code
$setCity = "Sydney "; // add  city of user
$setRegion = "New South Wales";
$setTelephone = "99999999999";
$setFax = "123456";
$setCompany = "Australia";
$setStreet = "in Australia some place";*/
$customer = Mage::getModel("customer/customer");
$customer ->setWebsiteId($website_Id)
->setStore($store_detail)
->setFirstname($setFirstname)
->setLastname($setLastname)
->setEmail($setEmail)
->setPassword($setPassword);
try{
    $results = $customer->save();   
    $getEntityId = $results->getEntityId(); // get user id
}
catch (Exception $e) {
    // bug or user is exist
    echo $e->getMessage(); // print Exception Message
}

Mage::getSingleton('core/session', array('name' => 'frontend'));

$sessionCustomer = Mage::getSingleton("customer/session");
$red_url = "http://www.onlinecode.org/";
if($sessionCustomer->isLoggedIn()) {
    header('Location: '. $red_url);
    break;
}
else{
    //echo $setEmail." ".$setPassword;
    loginUser($setEmail,$setPassword);
    //header('Location: '. $red_url);
    //break;
}
// function for login user programmatically
function loginUser($email,$password){
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));
    $website_Id = Mage::app()->getWebsite()->getId();
    $store_detail = Mage::app()->getStore();
    $customer_detail = Mage::getModel("customer/customer");
    $customer_detail->website_id = $website_Id;
    $customer_detail->setStore($store_detail);
    try
    {
        $customer_detail->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer_detail);
        $session->login($email,$password);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();// print Exception Message
    }
}

php json encode and decode example

<?php
echo "<br><h1>JSON Encode_decode with php</h1></br>";
echo"JSON_DECODE<br/>--------------------------------------------";
echo"<br/>";
$json = '{"a":1,"b":2,"c":3}';//Example 1
$j=json_decode($json);

echo "Value a= ". $j->{'a'};
echo "Value b= ". $j->{'b'};
echo "Value c= ". $j->{'c'};

echo"<br/>--------------------------------------------";
echo"<br/>";
echo"JSON_ENCODE<br/>--------------------------------------------";
echo"<br/>";

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);//Example 2
echo json_encode($arr);
echo"<br/>";
// Output {"a":1,"b":2,"c":3,"d":4,"e":5}


$arr = array ( 1, 2, 3, 4, 5 ); //Example 3
echo json_encode($arr);
echo"<br/>";
// Output [1,2,3,4,5]


$arr['x'] = 10;  //Example 4
echo json_encode($arr);
echo"<br/>";
// Output {"0":1,"1":2,"2":3,"3":4,"4":5,"x":10}


echo json_encode(54321); //Example 5
echo"<br/>";
// Output 54321
echo"<br/>--------------------------------------------";

//Example 1

echo "<br/>Example #1 : The following example shows how to convert an ARRAY INTO JSON WITH PHP
<br/><br/><br/>";

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
echo "<br/>";

//Example 2


echo "<br/>Example #2 : The following example shows HOW THE PHP OBJECTS CAN BE CONVERTED INTO JSON
<br/><br/><br/>";

class Emp {
public $name = "";
public $hobbies = "";

}
$e = new Emp();
$e->name = "sachin";
$e->hobbies = "sports";

echo json_encode($e);

//Example 3

echo "<br/>Example #3 : The following example shows HOW PHP CAN BE USED TO DECODE JSON OBJECTS
<br/><br/><br/>";

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo "<pre>".var_dump(json_decode($json))."</pre>";
echo "<pre>".var_dump(json_decode($json, true))."</pre>";
echo "<br/>";

//Example 4

echo "<br/>Example #3 : The following example shows HOW PHP CAN BE USED TO DECODE JSON OBJECTS
<br/><br/><br/>";

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo "<pre>".var_dump(json_decode($json))."</pre>";
echo "<pre>".var_dump(json_decode($json, true))."</pre>";
echo "<br/>";


?>

get longitude and latitude from an address with PHP and Google Maps API :Example

<?php
$url = "http://maps.google.com/maps/api/geocode/json?address=india+gujrat+rajkot&sensor=false&region=India";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response);
echo "<pre>";
print_r($response);
echo "<pre>";

echo "Latitude : ".$lat = $response->results[0]->geometry->location->lat;
echo "<br/>";
echo "Longitude :".$long = $response->results[0]->geometry->location->lng; 
?>

Example 2:

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://completion.amazon.com/search/complete?search-alias=aps&client=amazon-search-ui&mkt=1&q=google");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
echo curl_getinfo($curl, CURLINFO_HTTP_CODE); // code here
curl_close($curl);

die();
echo "<pre>";
print_r(json_decode($result));//return amazone autocomplete suggestion
echo "</pre>";
?>

Get City Country By IP Address in PHP

index.php

<?php
$ip_addressget=$_SERVER['REMOTE_ADDR'];

/*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_addressget;

$addrDetailsArr = unserialize(file_get_contents($geopluginURL));

/*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city'];

/*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];

/*Comment out these line to see all the posible details*/
/*echo '<pre>';
print_r($addrDetailsArr);
die();*/

if(!$city){
   $city='Not Define';
}if(!$country){
   $country='Not Define';
}
echo '<strong>IP Address is</strong>:- '.$ip_address.'<br/>';
echo '<strong>City is </strong>:- '.$city.'<br/>';
echo '<strong>Country is </strong>:- '.$country.'<br/>';
?>

PHP String Functions with Example

<?php
//Get The Length of a String :Example
echo strlen("Welcome WEB!"); // outputs 10

//Count The Number of Words in a String :Example
echo str_word_count("Welcome WEB!"); // outputs 2

//Reverse a String :Example
echo strrev("Hello world!1"); // outputs 1!dlrow olleH

//Search For a Specific Text Within a String :Example
 echo strpos("Hello world!", "world"); // outputs 6

//Replace Text Within a String :Example
echo str_replace("world", "King", "Hello world!"); // outputs Hello King!


//PHP Strlen() Function :Example

$name="king";
if ( strlen( $name ) != 5 ) 
{
    echo "Correct input.";
}
else
{
    echo "incorrect input";
}

//PHP chr() FUNCTION :Example
echo chr(35); //#

//PHP strcmp()FUNCTION :Example
<?php echo strcmp("Hello world!","Hello world!"); ?> //Returns 0

//PHP explode()FUNCTION :Example

 $str = "Hello world. It's a beautiful day.";
 print_r (explode(" ",$str));
/* Output :
Array
 (
 [0] => Hello
 [1] => world.
 [2] => It's
 [3] => a
 [4] => beautiful
 [5] => day.
 )
*/

//PHP implode()FUNCTION :Example

$arr = array('Hello','World!','Beautiful','Day!');
 echo implode(" ",$arr);
/*
Output:
Hello World! Beautiful Day!
*/

//PHP str_replace()FUNCTION :Example

 $phrase  = "You should eat fruits, vegetables, and fiber every day.";
 $healthy = array("fruits", "vegetables", "fiber");
 $yummy   = array("pizza", "beer", "ice cream");
 $newphrase = str_replace($healthy, $yummy, $phrase);

/*
Output :
You should eat pizza, beer, and ice cream every day
*/

//PHP substr()FUNCTION :Example

 echo substr("Hello world",6);  //Returns world
 echo substr("Hello world",6,4); // Returns worl
 echo substr("Hello world", -1); // Returns  d
 echo substr("Hello world", -3, -1); // Returns rl

?>

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...