Assignments On Class 32: Email Handling in PHP

Rashmi Mishra
0

  

Assignments  On Class 32: Email Handling in PHP

Assignments on Email Handling in PHP

Here are some step-by-step assignments with solutions to help students practice email handling in PHP. Each assignment includes explanations for better understanding.


Assignment 1: Sending a Basic Email Using PHP’s mail() Function

Objective: To understand and implement the basic PHP mail() function to send a simple text email.

Instructions:

1.   Write PHP code to send an email to your email address with the subject "PHP Test Email" and the message "Hello! This is a test email from PHP."

2.   Display a success or failure message based on whether the email was sent.

Solution:

php

Copy code

<?php

// Step 1: Set up the email parameters

$to = "your_email@example.com";  // Replace with your email

$subject = "PHP Test Email";

$message = "Hello! This is a test email from PHP.";

$headers = "From: sender@example.com";  // Replace with the sender's email

 

// Step 2: Use the mail() function to send the email

if (mail($to, $subject, $message, $headers)) {

    echo "Email sent successfully!";

} else {

    echo "Failed to send email.";

}

?>

Explanation:

  • $to: Defines the recipient’s email.
  • $subject: Defines the subject of the email.
  • $message: Contains the email body.
  • $headers: Specifies the sender’s information.
  • The mail() function attempts to send the email, returning true on success and false on failure.

Assignment 2: Sending an HTML Email Using PHP’s mail() Function

Objective: Learn how to send an HTML-formatted email using the mail() function.

Instructions:

1.   Write PHP code to send an email with HTML content.

2.   Use HTML tags to format the message body.

3.   Display a success or failure message.

Solution:

php

Copy code

<?php

// Step 1: Set up email parameters

$to = "your_email@example.com";

$subject = "HTML Test Email";

$message = "

<html>

<head>

    <title>HTML Email Test</title>

</head>

<body>

    <h1>Hello!</h1>

    <p>This is a test email with <strong>HTML formatting</strong>.</p>

</body>

</html>

";

 

// Step 2: Set content-type header for HTML email

$headers = "MIME-Version: 1.0" . "\r\n";

$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= "From: sender@example.com";

 

// Step 3: Send the email

if (mail($to, $subject, $message, $headers)) {

    echo "HTML email sent successfully!";

} else {

    echo "Failed to send HTML email.";

}

?>

Explanation:

  • The Content-type header ensures that the email content is interpreted as HTML.
  • The HTML tags in $message format the email.
  • This allows sending styled content, which is more visually appealing.

Assignment 3: Setting Up PHPMailer and Sending an Email

Objective: Install PHPMailer and use it to send a basic email with a subject and message body.

Instructions:

1.   Install PHPMailer using Composer.

2.   Write PHP code to send an email using PHPMailer with a simple text message.

3.   Handle potential errors if the email fails to send.

Solution:

1.   Install PHPMailer: Open your terminal and navigate to your project folder, then run:

bash

Copy code

composer require phpmailer/phpmailer

2.   PHP Code:

php

Copy code

<?php

// Import PHPMailer classes

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

 

require 'vendor/autoload.php';

 

// Step 1: Initialize PHPMailer

$mail = new PHPMailer(true);

 

try {

    // Step 2: SMTP server settings

    $mail->isSMTP();

    $mail->Host = 'smtp.example.com';  // SMTP server

    $mail->SMTPAuth = true;

    $mail->Username = 'your_email@example.com';

    $mail->Password = 'your_password';

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    $mail->Port = 587;

 

    // Step 3: Set email details

    $mail->setFrom('your_email@example.com', 'Your Name');

    $mail->addAddress('recipient@example.com');  // Recipient's email

 

    $mail->isHTML(true);  // Set email format to HTML

    $mail->Subject = 'PHPMailer Test Email';

    $mail->Body    = 'This is a test email sent using PHPMailer.';

    $mail->AltBody = 'This is a test email sent using PHPMailer.';

 

    // Step 4: Send the email

    $mail->send();

    echo 'Message has been sent';

} catch (Exception $e) {

    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

}

?>

Explanation:

  • SMTP is configured to ensure reliable email sending.
  • $mail->setFrom()$mail->addAddress()$mail->Subject$mail->Body set the email details.
  • $mail->send() attempts to send the email; any errors are caught and displayed.

Assignment 4: Sending an Email with an Attachment Using PHPMailer

Objective: To use PHPMailer to send an email with an attached file.

Instructions:

1.   Create a PDF or text file (e.g., example.pdf) in your project directory.

2.   Write PHP code to send an email with the file as an attachment.

3.   Verify that the recipient receives the email with the attachment.

Solution:

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');

 

    $mail->isHTML(true);

    $mail->Subject = 'Email with Attachment';

    $mail->Body    = 'Please find the attached document.';

    $mail->AltBody = 'Please find the attached document.';

 

    // Step 1: Attach a file

    $mail->addAttachment('example.pdf');  // Attach file

 

    // Step 2: Send email

    $mail->send();

    echo 'Email sent with attachment';

} catch (Exception $e) {

    echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}";

}

?>

Explanation:

  • $mail->addAttachment('example.pdf'); adds a file attachment.
  • PHPMailer can handle multiple attachments if required.
  • Test by checking if the attachment is received with the email.

Assignment 5: Debugging and Troubleshooting with PHPMailer

Objective: Practice enabling debugging in PHPMailer to troubleshoot issues in email sending.

Instructions:

1.   Add debugging code to any PHPMailer setup.

2.   Observe the debug output to understand the email sending process.

Solution:

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;

 

    // Step 1: Enable debugging

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;

 

    $mail->setFrom('your_email@example.com', 'Your Name');

    $mail->addAddress('recipient@example.com');

 

    $mail->isHTML(true);

    $mail->Subject = 'Debugging Test Email';

    $mail->Body    = 'Testing debug mode in PHPMailer.';

 

    $mail->send();

    echo 'Message sent successfully!';

} catch (Exception $e) {

    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

}

?>

Explanation:

  • $mail->SMTPDebug = SMTP::DEBUG_SERVER; enables detailed debug output.
  • Useful for diagnosing issues with SMTP settings or server configuration.


Post a Comment

0Comments

Post a Comment (0)