Chapter 22
Chapter 22
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 1
Objectives
Applied
1. Install and use the PEAR Mail package to send email messages
from your web applications.
2. Use the cURL library to work with the data on web sites that
provide APIs for doing that.
Knowledge
1. Describe the use of the PEAR Mail package, including its static
factory method and its mailer object.
2. Describe the use of a helper function for sending email with the
PEAR Mail package.
3. Describe the use of the cURL library for working with the data on
a web site that provides an API for doing that.
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 2
How email is sent
Sender SMTP Server
Email Client Email server
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 3
Protocols used in sending email
SMTP
POP3
IMAP
Terms
email client
email server
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 4
Limitations of the built-in PHP mail function
You must modify the php.ini file to change email servers.
You cannot use an encrypted connection to the email server.
You cannot provide a username and password when connecting to
the email server.
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 5
How to install the PEAR Mail package in Windows
1. Click the Start menu, select the ProgramsAccessories group,
and click on Command Prompt to open a command prompt
window.
2. In the Command Prompt window, type these commands:
cd \xampp\php
pear install --alldeps Mail
exit
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 6
How to create a Gmail account for email testing
1. If you don’t already have a Gmail account, create one. To do that,
you can go to www.gmail.com, click on the “Create an Account”
link, and follow the instructions.
2. Login to your Gmail account and go to your Inbox.
3. Click the “Settings” link at the top of the page.
4. On the Settings page, click the “Forwarding and POP/IMAP” link.
5. In the POP download section, select the “Enable POP for all mail”
option.
6. Click the “Save Changes” button.
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 7
Configuration info for a Gmail SMTP server
Setting Value
Host name smtp.gmail.com
Encryption SSL
Port number 465
Authentication Yes
Username [email protected]
Password yourGmailPassword
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 8
Step 1: Load the PEAR Mail package
require_once 'Mail.php';
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 9
Step 2: Set the parameters for the mailer object
Example 1: A simple SMTP server
$options = array();
$options['host'] = 'mail.example.com';
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 10
Step 3: Create the mailer object
$mailer = Mail::factory('smtp', $options);
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 11
Step 4: Send the message
// 1. Set the SMTP headers
$headers = array();
$headers['From'] = '[email protected]';
$headers['To'] = '[email protected],
[email protected]';
$headers['Cc'] = '[email protected]';
$headers['Bcc'] = '[email protected]';
$headers['Subject'] = 'How to use PEAR Mail';
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 12
Step 4: Send the message (continued)
// 4. Send the message
$result = $mailer->send($recipients, $headers, $body);
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 13
How to use HTML formatting
in the body of the message
// 1. Set the Content-type header
$headers['Content-type'] = 'text/html';
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 14
The message.php file (a helper function)
<?php
require_once 'Mail.php';
require_once 'Mail/RFC822.php';
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 15
The message.php file (continued)
$smtp = array();
// **** You must change the following to match your
// **** SMTP server and account information.
$smtp['host'] = 'ssl://smtp.gmail.com';
$smtp['port'] = 465;
$smtp['auth'] = true;
$smtp['username'] = '[email protected]';
$smtp['password'] = 'supersecret';
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 16
The message.php file (continued)
// Set the headers
$headers = array();
$headers['From'] = $from;
$headers['To'] = $to;
$headers['Subject'] = $subject;
if ($is_body_html) {
$headers['Content-type'] = 'text/html';
}
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 17
The message.php file (continued)
function valid_email($email) {
$emailObjects = Mail_RFC822::parseAddressList($email);
if (PEAR::isError($emailObjects)) return false;
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 18
The message.php file (continued)
// Validate the mailbox
$atom = '[[:alnum:]_!#$%&\'*+\/=?^`{|}~-]+';
$dotatom = '(\.' . $atom . ')*';
$address = '(^' . $atom . $dotatom . '$)';
$char = '([^\\\\"])';
$esc = '(\\\\[\\\\"])';
$text = '(' . $char . '|' . $esc . ')+';
$quoted = '(^"' . $text . '"$)';
$localPart = '/' . $address . '|' . $quoted . '/';
$localMatch = preg_match($localPart, $mailbox);
if ($localMatch === false || $localMatch != 1) {
return false; }
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 19
The message.php file (continued)
// Validate the host
$hostname =
'([[:alnum:]]([-[:alnum:]]{0,62}[[:alnum:]])?)';
$hostnames =
'(' . $hostname . '(\.' . $hostname . ')*)';
$top = '\.[[:alnum:]]{2,6}';
$domainPart = '/^' . $hostnames . $top . '$/';
$domainMatch = preg_match($domainPart, $host);
if ($domainMatch === false || $domainMatch != 1) {
return false; }
return true;
}
?>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 20
How to use the send_email function
require_once 'message.php';
$body =
'<p>The Murach PHP and MySQL book has a chapter on how
to use the
<a href="http://pear.php.net/Mail/">
PEAR Mail package</a>
to send email.</p>';
$is_body_html = true;
try {
send_email($to, $from, $subject, $body, $is_body_html);
} catch (Exception $e) {
$error = $e->getMessage();
echo $error;
}
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 21
How to configure PHP to use the cURL library
in Windows
1. Open the php.ini file in a text editor.
2. Find the line that contains the text “php_curl.dll”.
3. Remove the semicolon from the beginning of the line. It should
read as follows:
extension=php_curl.dll
4. Restart the Apache web server as described in the appendix.
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 22
How to configure PHP to use the cURL library
in Mac OS X
1. Open the php.ini file in a text editor.
2. Find the line that contains the text “curl.so”.
3. If necessary, remove the semicolon from the beginning of the line.
It should read as follows:
extension=curl.so
4. Restart the Apache web server as described in the appendix.
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 23
Common cURL functions
curl_init($url)
curl_setopt($curl, OPTION, $value)
curl_exec($curl)
curl_close($curl)
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 24
How to use the cURL functions
// Initialize the cURL session
$curl = curl_init('http://www.example.com');
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 25
The URL for the YouTube API documentation
http://code.google.com/apis/youtube/2.0/reference.html
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 26
Four parameters that you can add to the base URL
alt Specifies an alternate data format. The default is
“atom” which returns XML data, but you can use
“json” to return data in the JavaScript Object Notation
(JSON) format.
q Specifies the search string.
orderby Specifies how to sort the results of the search. The
default is “relevance”; you can specify other values
such as “published”, “viewCount”, and “rating”.
safeSearch Filters videos by removing restricted content. The
default value is “moderate”; you can specify other
values such as “none” and “strict”.
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 27
How to use cURL to query YouTube
// Set up the URL for the query
$query = 'space shuttle';
$query = urlencode($query);
$base_url = 'http://gdata.youtube.com/feeds/api/videos';
$params = 'alt=json&q=' . $query;
$url = $base_url . '?' . $params;
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 28
How to use cURL to query YouTube (continued)
// Access the data for each video
foreach ($videos as $video) {
$image_url =
$video['media$group']['media$thumbnail'][0]['url'];
$video_url = $video['link'][0]['href'];
$text = $video['title']['$t'];
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 29
Search view
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 30
Email view
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 31
The controller (index.php)
<?php
require_once 'message.php';
session_start();
function search_youtube($query) {
// Set up the URL for the query
$query = urlencode($query);
$base_url = 'http://gdata.youtube.com/feeds/api/videos';
$params = 'alt=json&q=' . $query;
$url = $base_url . '?' . $params;
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 32
The controller (continued)
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else {
$action = 'search';
}
if (isset($_POST['query'])) {
$query = $_POST['query'];
$_SESSION['query'] = $query;
} else if (isset($_SESSION['query'])) {
$query = $_SESSION['query'];
} else {
$query = '';
}
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 33
The controller (continued)
switch ($action) {
case 'search':
if (!empty($query)) {
$videos = search_youtube($query);
}
include 'search_view.php';
break;
case 'display_email_view':
$url = $_POST['url'];
$text = $_POST['text'];
include 'email_view.php';
break;
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 34
The controller (continued)
case 'send_mail':
// Get the data from the Mail View page
$from = $_POST['from'];
$to = $_POST['to'];
$subject = $_POST['subject'];
$text = $_POST['text'];
$url = $_POST['url'];
$message = $_POST['message'];
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 35
The controller (continued)
try {
// Send the email
send_email($to, $from, $subject, $body);
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 36
The controller (continued)
default:
include 'search_view.php';
break;
}
?>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 37
Search view (search_view.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YouTube Search</title>
<link rel="stylesheet" type="text/css"
href="main.css"/>
</head>
<body>
<div id="page">
<div id="header"><h1>YouTube Search</h1></div>
<div id="main">
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 38
Search view (continued)
<h2>Search</h2>
<form action="." method="post">
<input type="text" name="query"
value="<?php echo $query; ?>"/>
<input type="submit" value="Search"/>
</form>
<?php if (count($videos) != 0) : ?>
<h2>Results</h2>
<table>
<?php foreach ($videos as $video) :
$imgsrc =
$video['media$group']['media$thumbnail'][0]['url'];
$url = $video['link'][0]['href'];
$text = $video['title']['$t'];
?>
<tr>
<td>
<a href="<?php echo $url; ?>">
<img src="<?php echo $imgsrc; ?>">
</a>
</td>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 39
Search view (continued)
<td>
<form action="." method="post">
<input type="hidden" name="action"
value="display_email_view"/>
<input type="hidden" name="url"
value="<?php echo $url; ?>"/>
<input type="hidden" name="text"
value="<?php echo $text; ?>"/>
<input type="submit"
value="Email this Link"/>
</form>
<a href="<?php echo $url; ?>">
<?php echo $text; ?>
</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 40
Search view (continued)
</div><!-- end main -->
</div><!-- end page -->
</body>
</html>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 41
Email view (mail_view.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YouTube Search</title>
<link rel="stylesheet" type="text/css"
href="main.css"/>
</head>
<body>
<div id="page">
<div id="header"><h1>YouTube Search</h1></div>
<div id="main">
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 42
Email view (continued)
<?php if (isset($error)) : ?>
<p>Error Sending E-mail: <?php echo $error; ?></p>
<?php endif; ?>
<label>From:</label>
<input type="text" name="from"/>
Your email address<br />
<label>To:</label>
<input type="text" name="to"/>
Your friend's email address<br />
<label>Subject:</label>
<input type="text" name="subject"
value="YouTube Video Link"/><br />
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 43
Email view (continued)
<label>Video:</label>
<?php echo $text; ?><br />
<label>Link:</label>
<?php echo $url; ?><br />
<label>Message:</label>
<textarea name="message">
I thought you might enjoy this video.
</textarea>
<br />
<label> </label>
<input type="submit" value="Send"/>
</form>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 44
Email view (continued)
<form action="." method="post" id="cancel_form">
<label> </label>
<input type="submit" value="Cancel"/>
</form>
Murach's PHP and MySQL, C22 © 2010, Mike Murach & Associates, Inc. Slide 45