Send emails using PHP

In PHP there is a function called “mail” for send emails from our web applications. To use this this function, we have to do some configurations in php.ini file of our web server. If you are using PHP-equipped web host, you don’t need to worry about this mail function setup configuration because all required configurations are done by the hosting providers.

 

Setting up mail function in PHP

=====================

step 1> Goto “php.ini” file in your web server.

step 2> Find “[mail function]” part in the php.ini file.

step 3 for window mail server> Give the mail servers address to “SMTP”, give the port of the SMTP to “smtp_port” (default is 25) and set “sendmail_from” to sender’s email address.

eg:

[mail function]

SMTP = smtp.my.isp.net

smtp_port = 25

sendmail_from = you@yourdomain

step 3 for unix mail server> Give the path of the mail program on your server and set “sendmail_from” to sender’s email address.

eg:

[mail function]

sendmail_path = /usr/sbin/sendmail -t

sendmail_from = you@yourdomain

 

PHP mail function sample code

======================

<?php

// The message

$message = “Line 1\nLine 2\nLine 3”;

// In case any of our lines are larger than 70 characters, we should use wordwrap()

$message = wordwrap($message, 70);

// Send

mail(‘first_user@example.com,second_user@example.com‘, ‘The Subject’, $message);

?>

refer :http://php.net/manual/en/function.mail.php

Advertisements

Easy way to select numerical data from MySQL table

I’ll explain this using a example,


Lets thing we have table called EMP and column called SALARY.
Now suppose there are numerical data and string data in the SALARY column. (ex: 12000, 25000, not available, null…)
For this kind of columns we can use following method to select only numerical data;


SELECT SALARY FROM EMP WHERE (SALARY+0)!=0;

Simple PHP,AJAX application for get updates without doing any action at the client side using prototype JavaScript framework

This sample program contains 5 php files as follows;

1) config.php – This file use for manage the database access configuration.
2) add_name.php – This file use for send data to insert_name.php using AJAX.
3) insert_name.php – This file use for insert new record (which are receive from the add_name.php file) to the database.
4) view_names.php – This file use for view the database and database changes.
5) select_names.php – This file use for get all database data and print.

I have user a javascript framework called “PROTOTYPE” to develop this example (don’t worry, its not gona be a hard job for you). It is a single javascript file. So you have to download the latest version of it from http://prototypejs.org/ (the file called prototype.js)

config.php is required for manage your database connection information easily.
config.php
————-
<?php
// change the parameters to connect to your DB
// CHANGE ONLY $db_host, $db_name, $username, $password
$db_host=”localhost”;
$db_name=”test”;
$username=”root”;
$password=”password”;

$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>

add_name.php is provide a text field for enter the data (eg: names) into the database. By clicking the insert button the text form data send to the insert_name.php through ajax functions of prototype javascript framework.
add_name.php

——————









function insertName(){
new Ajax.Request(‘insert.php’, {
parameters: $(‘name’).serialize(true),
});
}





insert_name.php is insert post data to the database.
insert_name.php
——————–
<?php
if(isset($_POST[‘name’])){
// Connection to Database
include(‘config.php’);
// Remove HTML tag to prevent query injection
$name = strip_tags($_POST[‘name’]);

$sql = ‘INSERT INTO USER (name) VALUES(
“‘.$name.'”)’;
mysql_query($sql);
echo $name;
} else { echo ‘0’; }
?>

view_names.php is print the data of the database. It call the select_names.php file and get the data.
view_names.php
——————–

DB names

new Ajax.PeriodicalUpdater(‘names’, ‘select_names.php’,
{
method: ‘get’,
insertion: Element.update,
frequency: 1,
decay: 1
});

selest_names.php is get the data from the database and print.
select_names.php
———————
“;
while ($row = mysql_fetch_array($result)){
echo “”.$row[0].””;
}
echo “”;

?>


How to send HTTP request using PHP?

There is a predefined php class called HttpRequest. By using this class we can create object for handle HTTP requests. There are list of function to handle it. Refer PHP manual and get some idea about this class.


In WAMP before use this objects you need to make some changes in php.ini file. Add following line into the php.ini file (somewhere near in the extension).
“extension=php_http.dll”
Example
======
user HttpRequest object to handle get method http request
$r= new HttpRequest(‘www.example.com?valueone=1&valuetwo=2’, HttpRequest::METH_GET);
$r->send();
echo $r->getResponseBody();

Simple php sample code for privent SQL injection attacts

<?php

if(isset($_POST[“un”]) && isset($_POST[“pw”])){

mysql_connect(“localhost”,”root”,””);
mysql_select_db(“my_db”);

$username = mysql_real_escape_string($_POST[“un”]);
$password = mysql_real_escape_string($_POST[“pw”]);

$sql = “SELECT * FROM user WHERE id = ‘$username’ AND name = ‘$password’;”;
$result = mysql_query($sql);
if($result){
if(mysql_num_rows($result)>0){
echo “you loged in….”;
}
}

}
?>

Id :

Password :

How to execute php files without loading it

This method will execute the file_handle.php file and return the html result. So we can simply use this to database updates using this (use get method for pass inputs).
<?php
echo “Start”;
echo “end”;
?>

Php function for search from a file

If you have the start and end points, you can get the content of that two points. (as a example if the start and end point are and then u can get the content of these tags using this function. eg: content)

“,””,$contents);
$t = my_strip(“abc”,”xyz”,$contents);

echo $t;
?>

aaa.txt
=====
ntbfjkb n jABCjergnv
fvb j njrbn ntbfjkb n jegnb ef
g45yyyXYZtrgjrgbXYZk oi iABCyji oni jij

output
=====
jergnv fvb j njrbn ntbfjkb n jegnb ef g45yyy

%d bloggers like this: