Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Business and personalized recommendations.Start Your Free Trial Now
- View Offline
This course was updated on 6/12/2012.
- Understanding dynamic versus static content
- Adding PHP commands to web pages
- Setting and outputting variables
- Using server-side includes
- Creating PHP custom classes
- Adding the Zend Framework to a PHP installation
- Creating a MySQL database
- Adding data in phpMyAdmin
- Building recordsets
- Formatting dynamic data
- Building data entry forms
- Authenticating users
- Deploying a dynamic site
Skill Level Intermediate
Once you've prepared your data entry form, and have captured all of the information you need from the HTML form, you're ready to send e-mail. There are number of ways to do this, but my favorite approach is to use classes in the Zend framework that make it easy to work with a variety of e-mail servers. The first step, working in Dreamweaver, is to add information about the appropriate Zend classes to site-specific code hinting. That will make it a lot easier to create the required PHP code. Now, this exercise is dependent on your having set up the Zend framework, and configured it in your PHP server, which I showed how to do in a previous video in this series.
I'll go to the menu and choose Site > Site-Specific Code Hints. I'll work with my existing Zend framework structure. Right now, I've only included the date classes in my structure. I'm going to add the e-mail classes. I'll click the Plus button. Then I'll browse. I'll drill down to Zend, and I'll choose the Mail folder. The Mail folder contains everything I need: both the Zend Mail classes, and other dependent classes. I'll click Select.
I'll check the Recursive option, so I make sure I include everything in that directory and its subdirectories. I'll add the .php extension to the structure. Then I'll click Add. Then I'll click OK. Click OK again, and click Save. Now I'll get code hinting for my Zend mail classes. Next, I need to include the required class files. I'll place the cursor after the require_ once command for explorecalifornia.php.
That's my database connection definition. And I'll add a new PHP code block by going to the Insert panel, to PHP, and I'll click once on Code Block. I'm working with an SMTP server. Your specific requirements may differ, and you need to know something about your server in order to send mail correctly. My SMTP server requires authentication. So I'm going to need to include two classes. I'll put in a require_once command for the first one, and this will be a class called Zend/Mail.php.
Then I'll add a second require_ once command, and this will refer to Zend/Mail/Transport/Smtp.php. Those two PHP files together will contain everything I need to send mail. Now I'll go down to the conditional code, where I'm handling my form submission. I'll place the cursor after the conditional block and before the echo command. I'll make some empty space. Then I'll press F4 to expand to fullscreen coding.
Again, I'm working with a particular SMTP server, so I need to provide a bunch of information. I'll set up my configuration as an associative array using this code. First, I'll create a variable named config. I'll create it as an array, with named values. The first named value is auth, which I'll set as the value 'login.' Notice that I'm using equals, greater than as the separator between the key name and the key value. Next, I'll set a value named port.
Your particular SMTP server may be different than mine. The default is 25, but my ISP, or Internet Service Provider, requires a value of 8889. Check with your ISP for the details. Next, I'll enter my username. I'll set that to my e-mail address. And finally my password, which I'll set to my e-mail password.
So that configuration array contains authentication information. Once again, check with your ISP, or your SMTP server documentation for the detailed information you might need. Next, I'm going to create an instance of a class named Zend_Mail_Transport_Smtp. The definition for this class is in the Smtp.php file that I already included with the require_once command. I'll create a variable named transport. I'll create it as an instance of the Zend_Mail_Transport_Smtp class.
I'll type in smtp, and show you that I get a list of all classes that have that string in their names. I'll choose the last one that shows up, Zend_Mail_Transport_Smtp, then the DNS name of my e-mail server. Then I'll pass in my configuration array. Then I'll close the call to the constructor function of that class. I'm ready to start sending some mail. The next step is to create a variable that is an instance of the Zend_Mail class.
The code will look like this. I'll name my variable mail. I'll create it as an instance of new Zend_Mail. When you create this object, you don't pass in any values. Next, I'm going to use a set of methods or functions that create various parts of the e-mail. First, I'll call a function which I'll call by typing dash, greater than, and here is a listing of all the different functions that are part of the Zend_Mail class. The first one I'll call will be setBodyText, and I'll pass in the following value: "You are interested in," and then I'll add a dot, and the variable strInterested that I already created.
I'll close the function with the closing parenthesis and a semicolon. Next, I'll call the function setFrom. I'll type in my e-mail address and my name. Next, I'll call a function called addTo. This allows me to add an e-mail address that I want to send the e-mail to, and I can call it as many times as I want. The first value I'll pass in is the actual e-mail address, which I'll get from the data entry form, using the syntax $_POST, then 'email.' The second value will be the user's full name, which I'll get from $_POST, then bracket, 'firstname,' close bracket, then a dot, then a space wrapped in quotes, then another dot, then $_POST open bracket, and then in quotes 'lastname' close quote, close bracket, close parenthesis, semicolon.
So now I've indicated where the e-mail is going to be sent. Next, I'll set the subject. I'll call a function called setSubject. I'll pass in a literal string of 'Welcome to Explore California.' Now I'm ready to send my e-mail. I'll call the mail object send method, and I'll pass in my transport object, which is the variable, transport. So that's all the code.
Now let's give it a try. I'll save my changes, and I'll run the page in the browser, making sure I copy it over to the server. In the form, I'll type in my name. I'll type in my e-mail address. I'll fill in other values that are required by the form. Then I'll select some tours. I'll click the Join button, and I should get the output that I saw before.
In a complete implementation, I would continue processing the form, and display a message instead of the form, saying that the e-mail had been sent. So that's a look at how to use the Zend framework's e-mail capabilities. They're much more complete than what's included in the core PHP libraries. You can use SMTP server authentication. You can build complex functionality in your PHP pages to build either text-based e-mail, such as I've done here, or complex, HTML-based e-mails.
Check the documentation for the Zend_ Mail class, and its related classes, which allow you to send e-mail through a variety of e-mail servers.