KEMBAR78
Chapter 4 Part2 | PDF | Gmail | Internet
0% found this document useful (0 votes)
21 views21 pages

Chapter 4 Part2

Uploaded by

tindepzai91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views21 pages

Chapter 4 Part2

Uploaded by

tindepzai91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

ADVANCED WEB TECHNOLOGY

Chapter 4. MASTER THE ADVANCED SKILLS


FOR BUILDING WEBSITES
Chapter 4. Master the advanced skills
for building websites
Content
4.1. How to create secure websites
4.2. How to send email and access other websites
4.3. How to work with files, uploads and images
4.4. Laravel Framework
4.5. NodeJS
4.6. Open source PHP
4.7. Wordpress, Magento, Opencart

C1, Slide 2
4.2. How to send email and access other websites
Objectives

Applied
1. Use PHPMailer to send email messages from your web applications.
Knowledge
1. In general, describe how you access and use PHPMailer to connect
to an SMTP server and send email.
2. Describe the use of a helper function for sending email with
PHPMailer.
3. How to access other websites

C22, Slide 3
How email is sent

C22, Slide 4
Protocols used in sending email
 SMTP
 POP3
 IMAP

Key terms
 Email client
 Email server

C22, Slide 5
How to create a Gmail account for email testing
1. If you don’t already have a Gmail account, create one.
2. Log in to your Gmail account and go to your Inbox.
3. At the top of the page, select the Settings icon and then the Settings
link.
4. On the Settings page, click the Forwarding and POP/IMAP tab.
5. In the POP Download section, select the “Enable POP for all mail”
option.
6. Click the Save Changes button.

C22, Slide 6
The PHPMailer 5.2 directory that’s included
with this book’s download
/xampp/htdocs/book_apps/PHPMailer

The GitHub URL that provides more recent


versions of PHPMailer
https://github.com/PHPMailer/PHPMailer

C22, Slide 7
How to use PHPMailer to send an email
Step 1: Load the PHPMailer classes
set_include_path('/xampp/htdocs/book_apps/PHPMailer');
require_once 'PHPMailerAutoload.php';

Step 2: Create the PHPMailer object


$mail = new PHPMailer();

C22, Slide 8
How to use PHPMailer to send an email (cont.)
Step 3: Set the parameters for the PHPMailer object
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'YOUR_USERNAME@gmail.com';
$mail->Password = 'YOUR_PASSWORD';

$mail->setFrom('johndoe@example.com', 'John Doe');


$mail->addAddress('janedoe@example.com', 'Jane Doe');
$mail->Subject = 'PHPMailer Test';
$mail->Body = '<p>This body uses HTML to make text
<b>bold!</b></p>';
$mail->AltBody = 'This body does not use HTML.';
$mail->isHTML(true);

C22, Slide 9
How to use PHPMailer to send an email (cont.)
Step 4: Use the PHPMailer object to send the email
if($mail->send()) {
echo '<p>Message has been sent by PHPMailer.</p>';
} else {
echo '<p>Message could not be sent by PHPMailer.</p>';
echo '<p>Error: ' . $mail->ErrorInfo . '</p>';
}

C22, Slide 10
A helper function for sending an email
<?php
set_include_path('/xampp/htdocs/book_apps/PHPMailer');
require 'PHPMailerAutoload.php';

function send_email($to_address, $to_name, $from_address,


$from_name, $subject, $body, $is_body_html = false) {
if (!valid_email($to_address)) {
throw new Exception('This To address is invalid: ' .
htmlspecialchars($to_address));
}
if (!valid_email($from_address)) {
throw new Exception('This From address is invalid: ' .
htmlspecialchars($from_address));
}

$mail = new PHPMailer();


// **** Change to match your SMTP server and account info
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'YOUR_USERNAME@gmail.com';
$mail->Password = 'YOUR_PASSWORD';

C22, Slide 11
A helper function for sending an email (continued)
// Set From address, To address, subject, and body
$mail->setFrom($from_address, $from_name);
$mail->addAddress($to_address, $to_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
if ($is_body_html) {
$mail->isHTML(true);
}

if(!$mail->send()) {
throw new Exception('Error sending email: ' .
htmlspecialchars($mail->ErrorInfo) );
}
}

function valid_email($email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
return false;
} else {
return true;
}
}
?>

C22, Slide 12
How to use the helper function
<?php
require_once 'message.php';

$from_address = 'johndoe@example.com';
$from_name = 'John Doe';
$to_address = 'janedoe@example.com';
$to_name = 'Jane Doe';

$subject = 'How to use PHPMailer';


$body = '<p>The Murach PHP and MySQL book has a chapter on how to use
<a href="https://github.com/PHPMailer/PHPMailer">PHPMailer</a>
to send email.</p>';
$is_body_html = true;

try {
send_email($to_address, $to_name, $from_address, $from_name,
$subject, $body, $is_body_html);
echo '<p>Mail was sent successfully.</p>';
} catch (Exception $e) {
echo '<p>' . $e->getMessage() . '</p>';
}
?>

C22, Slide 13
Register view of the Registration application

C22, Slide 14
The email sent by the Registration application

C22, Slide 15
The controller (index.php)
<?php
require_once('model/message.php');

$action = filter_input(INPUT_POST, 'action');


if ($action === NULL) {
$action = 'reset';
} else {
$action = strtolower($action);
}

switch ($action) {
case 'reset':
// Reset values for variables
$first_name = '';
$last_name = '';
$phone = '';
$email = '';

// Load view
include 'view/register.php';
break;

C22, Slide 16
The controller (index.php) (continued)
case 'register':
// Copy form values to local variables
$first_name = trim(filter_input(INPUT_POST, 'first_name'));
$last_name = trim(filter_input(INPUT_POST, 'last_name'));
$phone = trim(filter_input(INPUT_POST, 'phone'));
$email = trim(filter_input(INPUT_POST, 'email'));

// Set up email variables


$to_address = $email;
$to_name = $first_name . ' ' . $last_name;
$from_address = 'YOUR_USERNAME@gmail.com';
$from_name = 'My Guitar Shop';
$subject = 'My Guitar Shop - Registration Complete';
$body = '<p>Thanks for registering with our site.</p>' .
'<p>Sincerely,</p>' .
'<p>My Guitar Shop</p>';
$is_body_html = true;

C22, Slide 17
The controller (index.php) (continued)
// Send email
try {
send_email($to_address, $to_name,
$from_address, $from_name,
$subject, $body, $is_body_html);
include 'view/success.php';
} catch (Exception $ex) {
$error = $ex->getMessage();
include 'view/register.php';
}
break;
}
?>

C22, Slide 18
Register view (view/register.php)
<?php include 'header.php'; ?>
<main>
<form action="." method="post" >
<fieldset>
<legend>User Information</legend>

<?php if (isset($error)) : ?>


<p class='error'><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>

<label>First Name:</label>
<input type="text" name="first_name"
value="<?php echo htmlspecialchars($first_name);?>">
<br>

<label>Last Name:</label>
<input type="text" name="last_name"
value="<?php echo htmlspecialchars($last_name);?>">
<br>

C22, Slide 19
Register view (view/register.php) (continued)
<label>Phone:</label>
<input type="text" name="phone"
value="<?php echo htmlspecialchars($phone);?>"><br>

<label>E-Mail:</label>
<input type="text" name="email"
value="<?php echo htmlspecialchars($email);?>"><br>
</fieldset>
<fieldset>
<legend>Submit Registration</legend>

<label>&nbsp;</label>
<input type="submit" name="action" value="Register"/>
<input type="submit" name="action" value="Reset" /><br>
</fieldset>
</form>
</main>
<?php include 'footer.php'; ?>

C22, Slide 20
Success view (view/success.php)
<?php include 'header.php'; ?>
<main>
<h2>Success</h2>
<p>The following registration information has been successfully
submitted.</p>
<ul>
<li>First Name: <?php echo htmlspecialchars($first_name); ?>
</li>
<li>Last Name: <?php echo htmlspecialchars($last_name); ?>
</li>
<li>Phone: <?php echo htmlspecialchars($phone); ?></li>
<li>Email: <?php echo htmlspecialchars($email); ?></li>
</ul>
</main>
<?php include 'footer.php'; ?>

C22, Slide 21

You might also like