Sunday, December 30, 2012

PHP Tutorial (Faq's) part-6

PHP Mail

You can use PHP to dynamically send emails to one or more recipients. This can be handy for a lot of reasons, for example:
  • Sending newsletters to a mailing list
  • Sending a "welcome" email to new members of your website
  • A user has just made a purchase and you need to send them a receipt by email
  • Sending an email alert to your technical administrator whenever an error occurs
  • Many more reasons...

The PHP mail() Function

To send email using PHP, you use the mail() function. This accepts 5 parameters as follows (the last 2 are optional).
Code

Below is an explanation of the parameters.
Parameter Description
to Required. The recipient's email address.
subject Required. The email's subject line.
message Required. The actual email body.
headers Optional. Additional header fields such as "From", "Cc", "Bcc" etc.
parameters Optional. Any additional parameters.

Sending an Email

You could send email by simply doing this:
Code

Although, in reality, you would probably set your parameters up as variables. Also, if the email was triggered by a user, you would probably provide them with feedback to say that the email had been sent.
Code

HTML Emails

To send an HTML email, the process is the same, however, you need to provide additional headers (as well as an HTML formatted message).
Note that you need to separate each header with a carriage return.

Windows

For Windows systems, use this code:
Code

UNIX

For UNIX systems, use this code:
Code

Difference Between UNIX and Windows Code?

You probably noticed in the above example that there's a Windows version and a UNIX version.
The only difference is in the way the carriage returns are specified. On Windows it's "\r\n", on UNIX it's "\n".
The PHP specification stipulates that you should use "\r\n" for creating the carriage returns. This should work fine on Windows systems. UNIX systems however, have a tendency to add "\r" to the "\n", therefore resulting in "r\r\n", which of course, wouldn't work. Therefore, we simply leave out the "\r" on the UNIX version.

Configuring Mail on PHP

The above steps assume that your PHP installation is configured to send mail. Your hosting provider should already have configured PHP to send mail, so you shouldn't need to do anything further if your website is hosted with a third party hosting provider.
If you need to send mail from your local computer, you may need to configure PHP to send mail. The next lesson explains this.

 PHP Mail Configuration

In the previous lesson, we used mail() to send mail in PHP. That lesson assumes that your PHP installation is configured for sending mail. If your system isn't configured for sending mail, all is not lost - you can change the configuration. This lesson explains how to configure PHP for sending mail.

The php.ini File

The php.ini file is where you configure your PHP installation. This is the file you need to edit in order to configure PHP to send mail.
You need to ensure that the php.ini file contains details of the mail server that should be used whenever your application sends mail.
To check/change your PHP mail configuration:
  1. Open your php.ini file (if you don't know where this is, see below)
  2. Search for the line that reads [mail function]
  3. Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
  4. Save/close the php.ini file
  5. Restart your web server
Here's an example of what the mail settings could look like when you first open the php.ini file:
Code

If you're using a UNIX based system, you will need to change the line that reads ;sendmail_path =. You will also need to remove the semicolon from the start of this line (semicolons indicate that the line is a comment). For example, sendmail_path = /usr/sbin/sendmail.
If you're using a Windows system, you should change the line that reads SMTP = localhost to include your mail server (or your ISP's mail server). You could leave it at localhost if you're using your own local SMTP server. If you aren't using your own local SMTP server, you will need to enter a mail server that you have access to (such as your ISP's mail server). For example, SMTP = mail.earthlink.net.
You should also set a default "From" email address by changing the line that reads ;sendmail_from = me@example.com. For example, sendmail_from = you@earthlink.net.

Don't Know Your php.ini Location or sendmail_path Path?

If you don't know where your php.ini is, or what your sendmail_path setting should be, read on...
Your php.ini may be located here: /private/etc/php.ini. And there's a chance that your sendmail path will be at /usr/sbin/sendmail). Having said this, each PHP installation is different, so you should check using phpinfo().
Fortunately, this is easy to do.
phpinfo() is used to view your PHP configuration details. You can do this by creating a .php file with the following line on it: <?php phpinfo(); ?>. When you run this in your browser, you will see a full list of PHP configuration variables. Simply search for the lines that contain php.ini and sendmail_path to see the values you need to use.

 PHP Database Driven Website

You can use PHP, (in conjunction with SQL and HTML), to create database driven websites.
To create a database driven website in PHP, you need a database management system (DBMS). Common database systems include MySQL, Microsoft SQL Server, and Oracle.
Your DBMS can be located either on the same computer that the website is on, or on another server. It's good practice to separate your database server from your web server, but if you've only got one machine to develop on, sharing the same machine shouldn't cause any problems (as long as it's powerful enough to run a web server and database server etc).
Anyway, once you have a database with some tables and some data, you can connect to it and query it.
MySQL is a database system commonly used with PHP websites. The following examples demonstrate how to connect and query a MySQL database.

Connecting to the Database

Before you can query your database, you need to connect to the database server, then locate the database. Once you've done this, you can send in your SQL code to do your queries.
To connect to the database server:
Code

The above code uses the mysql_connect function to connect to the database server. We provide the following parameters: Server, Username, Password. PHP needs this info so that it knows which server to connect to. In this example we are just connecting to the local machine so we use "localhost" as the server.
We have also used the PHP die and mysql_error functions to be used in the event there's an error and PHP can't connect to the server. This will display the error message which can assist us in determining the cause of the problem.
To select the database:
Code

The above code uses the mysql_select_db function to select the database from the database server. You need to do this because, your database server could contain many databases. You need to tell PHP which database to use.
Again we use the die and mysql_error functions to be used in the event of an error.

Querying the Database

You can use the mysql_query function to send a SQL query to the database:
Code

What we do here is, assign the results of a query to the $result variable. The query is acheived by passing a SQL statement to the mysql_query as a parameter. In this SQL statement, we are selecting all records from the "Individual" table.
Once again we use the die and mysql_error in case there's an error.

Displaying the Results

To display the results, you need to loop through the results of the query and display each record with each iteration of the loop:
Code

Here we use a while loop to loop through the results of the query. The while loop keeps iterating until it finishes the last result. This means we can display each result as it iterates past that result. We display the result using $echo and the PHP $row variable, indicating which columns we want to display.

The Whole Code

Combining the above code, (and adding comments), results in something like this:
Code

No comments:

Post a Comment