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 |
|---|
|
|
| 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 |
|---|
|
|
| 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 usedmail()
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:
- Open your
php.inifile (if you don't know where this is, see below) - Search for the line that reads
[mail function] - Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
- Save/close the
php.inifile - Restart your web server
php.ini file:| Code |
|---|
|
|
;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 |
|---|
|
|
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 |
|---|
|
|
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 |
|---|
|
|
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 |
|---|
|
|
The Whole Code
Combining the above code, (and adding comments), results in something like this:| Code |
|---|
|
|
No comments:
Post a Comment