Lecture Notes On Class 32:
Email Handling in PHP
Objective:
- Understand
how to send emails using PHP.
- Explore
the use of PHPMailer for sending emails with attachments.
Outcome:
- Students
will be able to send emails using PHP’s mail()
function and the PHPMailer library for advanced email features.
Lecture
Outline
1. Introduction
to Email Handling in PHP
o
Importance of email communication in web
applications.
o
Use cases: account verification, password resets,
notifications.
2. PHP mail() Function
o
Syntax: mail(to, subject, message, headers, parameters)
o
Basic Parameters:
§ to: Recipient’s email address.
§ subject: Subject
line of the email.
§ message: Body
content of the email.
§ headers:
Additional headers (e.g., "From", "CC", "BCC").
§ parameters:
Optional settings like specifying the sender’s address.
o
Example of Basic Email Using mail():
php
Copy code
<?php
$to = "example@example.com";
$subject = "Test Email";
$message = "This is a test
email sent from PHP!";
$headers = "From:
sender@example.com";
if (mail($to, $subject, $message,
$headers)) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
?>
o
Limitations of mail():
§ Lack of
advanced features (e.g., attachments, HTML formatting).
§ Dependent
on server configuration (may require a local SMTP server).
3. Introduction
to PHPMailer
o
Why Use PHPMailer?
§ Easy to
add attachments and HTML content.
§ Supports
SMTP (Simple Mail Transfer Protocol) for reliable delivery.
§ Provides
better error handling and debugging.
o
Installing PHPMailer:
§ Use
Composer to install PHPMailer: composer require phpmailer/phpmailer
§ Manual
installation: download from GitHub and include it in your project.
4. Sending
Email with PHPMailer
o
Basic Setup:
§ Initialize
PHPMailer and configure basic settings like SMTP server, authentication, and
email details.
o
Example of Sending a Simple Email Using PHPMailer:
php
Copy code
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('your_email@example.com',
'Your Name');
$mail->addAddress('recipient@example.com');
// Content
$mail->isHTML(true);
$mail->Subject = 'Test Email Using
PHPMailer';
$mail->Body = '<p>This is a
<strong>test email</strong> sent using PHPMailer.</p>';
$mail->AltBody = 'This is a test email
sent using PHPMailer.';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent.
Mailer Error: {$mail->ErrorInfo}";
}
?>
o
Explanation of Code:
§ Setting
up SMTP for secure email transmission.
§ Specifying
email details like sender, recipient, subject, and body.
5. Sending
Emails with Attachments
o
Adding Attachments in PHPMailer:
php
Copy code
$mail->addAttachment('/path/to/file.txt'); // Add a file attachment
$mail->addAttachment('/path/to/image.jpg',
'new.jpg'); // Optional: Rename attachment
o
Example of Sending an Email with an Attachment:
php
Copy code
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('your_email@example.com',
'Your Name');
$mail->addAddress('recipient@example.com');
// Adding an attachment
$mail->addAttachment('/path/to/file.pdf');
$mail->isHTML(true);
$mail->Subject = 'Document Attached';
$mail->Body = 'Please find the attached
document.';
$mail->AltBody = 'Please find the
attached document.';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent.
Mailer Error: {$mail->ErrorInfo}";
}
?>
o
Key Points:
§ Attachments
allow sending files like PDFs, images, or documents.
§ Attachments
enhance the functionality of emails in applications (e.g., sending invoices,
reports).
6. Error
Handling and Debugging in PHPMailer
o
Use $mail->SMTPDebug = SMTP::DEBUG_SERVER; to
enable detailed debug output.
o
$mail->ErrorInfo provides information if email fails to send.
7. Comparison
of mail() and PHPMailer
o
mail(): Simple, limited functionality, suitable for basic emails.
o
PHPMailer: More flexible, supports HTML,
attachments, and SMTP authentication.
8. Practice
Exercise
o
Task: Send a basic email to yourself using the mail()
function.
o
Task: Send an HTML-formatted email with an
attachment using PHPMailer.
Summary
- PHP
provides the mail() function for simple email
sending.
- PHPMailer
offers advanced email capabilities, including SMTP support, HTML
formatting, and attachments.
- Error
handling in PHPMailer allows easier debugging of email-related issues.
Note to Students:
PHPMailer is a powerful tool for professional email handling. Although it may
seem complex initially, mastering it will greatly enhance your web development
skills.