Thursday, December 30, 2010

PHP: Send Mail with Attachments using Local Mail Server

I have modified some codes from the forums for sending email with attachments using local mail server. This script is tested with Gmail and Hotmail. Both mail servers are able to receive the emails.

However, when I include the attachments in the sending mail, Hotmail accounts are unable to receive them. As for Gmail accounts, there are no problems at all.

I am still trying to find out why Hotmail is unable to receive the attachments. If anyone knows how to solve the problem, do let me know. 



You can download the PHP files for the class here and the class tester here.
Alternatively you can visit my website here.


As usual, here are some snapshots of the output and the code:


1. Message received by Gmail.


2. Message output from server.










Codes:





























Monday, December 20, 2010

PHP: Gmail Class

I have created a class for Gmail using PHP.This class allows file attachments to be automatically downloaded into your server file directory.
Currently, I am still writing the functions for sending mail.

Do take note that in order for the server to be able to access gmail, imap-ssl must be configured for your PHP. You can check whether imap-ssl is installed by running a PHP which contains phpinfo(). Under the 3rd column, Configure Command, you should be able to see: ' --with-imap-ssl'
If you do not see that, please reconfigure your PHP installation to include that.

You can download the PHP files for the class here and the class tester here.
Alternatively, you can visit my website here.

Here are some snapshot of the output and the codes.

Output:

When there is a same file in the directory in which the file is to be downloaded into.
The 2nd one shows a file being downloaded.



Here are some snapshots of the code...

















This is the PHP to test the Gmail Class



Saturday, December 18, 2010

PHP: Vigenere Cipher

I needed to use Vigenere Cipher to encrypt my data for my server-side applications. After a quick research,  I got a code from a forum and made some modifications to them. Now, the default code is also able to encrypt more than just 26 alphabets. An EnsureKeyValid function is added to ensure that only valid indexes are used.

You can download the Vigenere Cipher Class here and the class tester here.
Alternatively, you can go to my personal website to download them.

The output of my tester is:
attackatdawn = lxFopveFrnHr = attackatdawn

'lxFopveFrnHr' is the encrypted form of 'attackatdawn'

Here are some snapshots:





















To test the class:















Output:

Friday, December 10, 2010

PHP and MySQL Part 2

There are 2 methods of using mysqli. One method of using it is by object orientated and another way is by procedural.

Personally, I prefer to use object orientated.

Here is a function which I have created to connect to the database. It will return an Object if it connects successfully. If not, it will return a false which indicates that the connection failed.
$result = new mysqli ('hostname','username','password','database_name');

The 4 string values to provide mysqli are the hostname (such as localhost), username, password and the name of the database. The username and the password must be registered as a user of the database and have sufficient privileges to access the database.

Below is a simple database connect function.



We are interested to perform a query on the database so that we can obtain the results we need. How do we do so? For the object orientated method, mysqli->query will return an Object. The object will contain certain attributes such as num_rows (no. of rows), error, close, etc. For a whole list of available attributes, you can read here.

Below is a simple query function.



Now, we are able to connect to the database and perform a simple query using PHP. I will try to include some MySQL tutorials soon. =)

Thursday, December 2, 2010

PHP and MySQL

In order for PHP to "connect" with MySQL, the mysqli extension must be enabled.

This can be done by running the configure script with a certain option. This must be done prior to building PHP.

For PHP 5.0, 5.1 and 5.2, the option is:
-with-mysqli=mysql_config_path/mysql_config. 
The mysql_config_path represents the location of the mysql_config program. The above option will enable mysqli and it will use the MySQL Client Library to communicate with the MySQL server.


For PHP 5.3.0 or newer, mysqli uses the MySQL Native Driver by default. For more information about MySQL Native Driver, click here.

How to use MySQL Native Driver with mysqli?
You need to configure the PHP source code using this option: --with-mysqli=mysqlnd

./configure --with-mysql=/usr/bin/mysql_config  \
--with-mysqli=mysqlnd \

If you do not know what ./configure is, do refer to my previous blog post here.
Currently, I am using PHP 5.3. You should see this in your phpinfo(), under configure command.

You can use mysqli in 2 forms. The first is object orientated and the second is procedural.
Do check this page for more information on the classes for mysqli.

Bravo! so now, we have connected PHP and MySQL! Next, I will move on to explain the fundamentals of MySQL first before we start to query the database. I will also start putting in some codes which I have done to share with everyone.

All the best and Happy coding!

PS: I love to learn new things, do drop me a msg if you have any programming problems to discuss!!