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
<team@gmail.com>
$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 = 'your_email@gmail.com'; // Your Gmail
$mail->Password = 'your_app_password'; // App password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Email content
$mail->setFrom('your_email@gmail.com', 'Your Name');
$mail->addAddress('receiver@example.com', '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",
"your_email@gmail.com", "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);
?>