Ex.
No: 11 PHP MAIL SERVICES
Date: 12.10.2023
Aim:
To develop a PHP program to make use of PHP mail services to send and receive
messages.
Program:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
if(isset($_POST['submit'])){
$mail = new PHPMailer;
$name = $_POST['name'];
$femail = $_POST['femail'];
$toemail = $_POST['email'];
$subject = $_POST['subject'];
$bodycontent = $_POST['message'];
$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 = '8220541626'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom($femail, $name);
$mail->addReplyTo($femail, $name);
$mail->addAddress($toemail); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $bodycontent;
$mail->AddAttachment($_FILES['image']['tmp_name'], $_FILES['image']['name']);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo '<font color = "Black">Mailer Error: ' . $mail->ErrorInfo.'</font>';
} else {
echo '<font color = "Blue">Message has been sent</font>';
?>
<html>
<body>
<form action="" method="POST" enctype='multipart/form-data'>
<table>
<tr>
<td> Name : </td>
<td><input type="text" id="name" name="name" placeholder='User Name'/>
<div id="invalid-name"></div>
</td>
</tr>
<tr>
<td> From E-mail: </td>
<td><input type="email" id="femail" name="femail" placeholder='From E-mail'/>
<div id="invalid-name"></div>
</td>
<tr>
<td> To E-mail : </td>
<td><input type="email" id="email" name="email" placeholder='E-Mail'/>
<div id="invalid-email"></div>
</td>
</tr>
<td> Subject : </td>
<td><input type="text" id="subject" name="subject" placeholder='Subject'/>
<div id="invalid-femail"></div>
</td>
</tr>
<tr>
<td> Image : </td>
<td><input type="file" id="image" name="image" placeholder='Image' accept="image/*"> <div
id="invalid-image"></div></td>
</tr>
<tr>
<td> Message : </td>
<td><textarea cols="27" rows="5" id="message" name="message" placeholder='Message'
value=''></textarea><div id="invalid-message"></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="Send Mail!" id='submit_btn'
name="submit"/></td>
</tr>
<table>
</form>
</body>
</html>
Output:
Conduct of Experiment (30)
Record(20)
Viva(10)
Total(60)
Result:
Thus, the development of PHP program to send and receive email has been done
successfully.