SMTP Setting: username, password, port, all these we have to use in swiftmailer/phpmailer
<?php include("send.php"); ?>
<?php include("common/header.php"); ?>
<?php include("common/navbar.php"); ?>
<br>
<div class="container border w-50 p-2" style="margin-top: 10px;background:#f5f6fa;">
<h3 class="rounded bg-primary w-100 p-2 text-white text-center">Send Email</h3>
<div class="text-center"><?php echo $success;?><?php echo $error; ?></div>
<form method="POST" action="index.php" class="p-2 w-100">
<div class="form-group">
<label for="name">Full Name:</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="Enter Your Full Name">
</div>
<div class="form-group">
<label for="email">Sender Email Address :</label>
<input type="email" class="form-control" name="sender_email" id="sender_email" placeholder="Sender: exampl@gmail.com">
</div>
<div class="form-group">
<label for="email">Recipient Email Address :</label>
<input type="email" class="form-control" name="recipient_email" id="recipient_email" placeholder="Reciever :exampl@gmail.com">
</div>
<div class="form-group">
<label for="subject">Subject :</label>
<input type="text" class="form-control" name="subject" id="subject" placeholder="subject">
</div>
<div class="form-group">
<label for="msg">Message :</label><br>
<textarea name="message" id="message" cols="78" rows="3" class="form-control" placeholder="Write Your Message Here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="send" id="submit" value="Send Email" class="btn btn-primary">
<a href="#" class="btn btn-danger float-right">Close</a>
</div>
</form>
</div><br>
<?php include("common/footer.php"); ?
$dbhost ="localhost";
$dbuser ="root";
$dbpswd ="";
$dbname ="dbdemo";
$dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
if($dbcon){
//echo "Connected............!!!";
return $dbcon;
exit();
}else{
die("Connection failed ? ? ?". mysqli_error($dbcon));
}
mysqli_close($dbcon);
<?php include("dbcon.php");
$success=''; $error='';
//Vendor is SwiftMailer library
require_once ('vendor/autoload.php');
$success=''; $error='';
if(isset($_POST['send'])){
$fullname = filter_var(ucwords( $_POST['fullname']),FILTER_SANITIZE_STRING);
$sender_email = filter_var($_POST['sender_email'],FILTER_SANITIZE_EMAIL);
$recipient_email= filter_var($_POST['recipient_email'],FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if(empty($fullname) || empty($sender_email) || empty($recipient_email) || empty($subject) || empty($message)){
$error ="<div class='alert alert-danger'>All the fields are required</div>";
}else if (!preg_match("/^[a-zA-Z][a-zA-Z\\s]+$/", $fullname)){
return "inavlid_name";
$error ="<div class='alert alert-danger'>The email has been failed to send</div>";
}else if(!filter_var( $sender_email,FILTER_VALIDATE_EMAIL)) {
$error ="<div class='alert alert-danger'>The sender email address is invalid</div>";
}else if(!filter_var( $recipient_email,FILTER_VALIDATE_EMAIL)) {
$error ="<div class='alert alert-danger'>The recipient email address is invalid</div>";
}else if (!preg_match("/^[a-zA-Z][a-zA-Z\\s]+$/", $subject)) {
$error ="<div class='alert alert-danger'>Only alphabets are allowed in subject field</div>";
}else if (strlen($subject) < 4 ){
$error ="<div class='alert alert-danger'>The subject must be minimum 4 characters</div>";
}else{
$sql= "INSERT INTO `email` (`fullname`, `sender_email`,`recipient_email`, `subject`, `message`) VALUES('$fullname','$sender_email','$recipient_email','$subject','$message')";
$run = mysqli_query($dbcon, $sql);
if($run){
//=to send email to user
$transport = (new Swift_SmtpTransport('smtp.mailtrap.io', 2525))
->setUsername('...........') // your username of SMTP from mailtrap
->setPassword('...........'); //password of SMTP from mailtrap
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$body_msg = (new Swift_Message($subject))
->setFrom([$sender_email => $fullname])
->setTo($recipient_email)
->setBody($message, 'text/html');
// Send the message
$result = $mailer->send($body_msg);
if($result){
$success="<div class='alert alert-success'><span style='color:green;'>The email has been sent to {$recipient_email}</span></div>";
} else {
$error="<div class='alert alert-danger'><span style='color:red;'>The email could not be sent !Please check you email address !</span></div>";
}
}
}
}
The email will be sent to your mailtrap inbox, check your inbox.
If any other error, check your code and try to solve the errors (solving errors by yourself will teach you a lot).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<title>Codetechinfo</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
<a class="navbar-brand text-white" href="./index.php">Learn PHP </a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link text-white" href="./index.php">Home </a>
</li>
</ul>
</div>
</nav>
<br><br>
<div class="copyright py-4 text-center text-white">
<div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
</div>
</body>
</html>