0% found this document useful (0 votes)
28 views2 pages

PHP Practical No.14 Outputs

The document provides PHP code examples for sending and receiving emails using the PHPMailer library and IMAP. It includes server settings for SMTP configuration to send an email and a script to retrieve emails from a Gmail inbox. Error handling is also demonstrated for both sending and receiving functionalities.

Uploaded by

ARYAN MOHADE
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)
28 views2 pages

PHP Practical No.14 Outputs

The document provides PHP code examples for sending and receiving emails using the PHPMailer library and IMAP. It includes server settings for SMTP configuration to send an email and a script to retrieve emails from a Gmail inbox. Error handling is also demonstrated for both sending and receiving functionalities.

Uploaded by

ARYAN MOHADE
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/ 2

PRACTICAL No.

14
Outputs

Send Script Output


Q.Write a program to send and receive
mail using Message sent successfully!

composer require phpmailer/phpmailer

Php code to send Email.


<?php
use PHPMailer\PHPMailer\PHPMailer; Receive Script Output
use PHPMailer\PHPMailer\Exception;
Subject: Welcome to Gmail
require 'vendor/autoload.php'; From: Gmail Team
<[email protected]>
$mail = new PHPMailer(true); Date: Fri, 11 Apr 2025 10:00:00
+0530
try { Message: Thank you for signing
// Server settings
up!
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your Gmail
$mail->Password = 'your_app_password'; // App password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Email content
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Receiver');
$mail->Subject = 'Test Mail';
$mail->Body = 'This is a test email using PHPMailer.';

$mail->send();
echo 'Message sent successfully!';
} catch (Exception $e) {
PRACTICAL No.14
Outputs

echo "Error: {$mail->ErrorInfo}";


}
?>

Php code to receive email


<?php
$mailbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",
"[email protected]", "your_app_password");

$emails = imap_search($mailbox, 'ALL');

if ($emails) {
rsort($emails); // Latest first
foreach ($emails as $email_number) {
$overview = imap_fetch_overview($mailbox, $email_number, 0)[0];
$message = imap_fetchbody($mailbox, $email_number, 1);

echo "Subject: {$overview->subject}\n";


echo "From: {$overview->from}\n";
echo "Date: {$overview->date}\n";
echo "Message: $message\n\n";
break; // Only first email
}
} else {
echo "No emails found.";
}

imap_close($mailbox);
?>

You might also like