22203A00008_practical14
22203A00008_practical14
Experiment No: 14
Aim of Experiment Implement working of phpmailer.
Implement working of
phpmailer. Ans :
Code:
<?php
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\
SMTP; use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'onyyvitrrghxntsa'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->addReplyTo('[email protected]', 'Information');
$mail->addAttachment('c:\Users\Downloads\WBP_14.pdf'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test Mail from PHPMailer';
$mail->Body = '<h1>This is the testmail sent from PHPMailer </h1>'; if(!
$mail-
>send()) {
echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
Page | 1
echo 'Message has been sent';
Page | 2
}
Output :
Page | 3
Page | 4
2. Implement Phpmailer with custom user input.
Ans :
Code :
<?php
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\
SMTP; use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$mail = new PHPMailer(true); try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Replace with your SMTP server
$mail->SMTPAuth = true;
$mail->Username = ' [email protected] ' ; // SMTP username
$mail->Password = 'erxxfdcabasmdikf'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Recipients
$mail->setFrom(' [email protected] ', 'PHPMailer');
$mail->addAddress($_POST['email']);
// Attachments
Page | 5
if (!empty($_FILES['attachment']['name'])) {
$mail->addAttachment($_FILES['attachment']['tmp_name'],
$_FILES['attachment']['name']);
}
// Content
$mail->isHTML(true);
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Send Email with Attachment</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label for="email">Recipient Email:</label>
<input type="email" name="email" required><br><br>
<label for="subject">Subject:</label>
Output :
Page | 6
Page | 7
Page | 8
Grade and Process Related Product Related Dated Sign
Dated (35) (15)
Signature of
Teacher
Page | 9