SEND AN EMAIL WITH ATTACHMENT USING PHPMAILER

In this tutorials we SEND AN EMAIL WITH ATTACHMENT USING PHPMAILER

PHPMailer is one of the most popular open source PHP libraries to send emails. PHPMailer have code library by which We can easily and safely send emails.

SEND AN EMAIL WITH ATTACHMENT USING PHPMAILER

What we do in this :

  • We make and design the form
  • Through PHPMailer we send the mail, which have these attribute or fields Subject, Email, Message and Attachment.

ADVANTAGES OF PHPMAILER:

  • It can print various kinds of errors messages, when it fails to send an email.
  • We can send email as Plain text, HTML and multipart batched files.
  • It support SMTP (SSL and TLS).

NOW WE START WITH CODING:-

Index.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Contact Form</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
	<div class="container" style="margin-top: 100px">
		<div class="row justify-content-center">
			<div class="col-md-6 col-md-offset-3 developerguidance" align="center">
				<h2 class="title">DEVELOPER GUIDANCE</h2><br><br>
                <?php if ($msg != "") echo "$msg<br><br>"; ?>
				<form method="post" action="mail.php" enctype="multipart/form-data">
					<input class="form-control" name="subject" placeholder="Subject..."><br>
					<input class="form-control" name="email" type="email" placeholder="Email..."><br>
					<textarea placeholder="Message..." class="form-control" name="message"></textarea><br>
					<input class="form-control" type="file" name="attachment"><br>
					<input class="btn btn-primary" name="submit" type="submit" value="Send Email">
				</form>
			</div>
		</div>
	</div>
</body>
</html>

Now we add PHP code to check the condition and send the form on mail

Put this css in index.php

body{background-color: #fff;}
	.developerguidance{
		    box-shadow: 0 10px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19) !important;
	}
	.title{
			 padding-top: 10px;
	}
	.btn{
		    margin-bottom: 10px;
	}

First we download the PHPMailer library through link
https://github.com/PHPMailer/PHPMailer

Mail.php

$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";
if (isset($_POST['submit'])) {
		$subject = $_POST['subject'];
		$email = $_POST['email'];
		$message = $_POST['message'];
		if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
			$file = "attachment/" . basename($_FILES['attachment']['name']);
			move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
		} else
			$file = "";
		$mail = new PHPMailer();
		$mail->addAddress('[email protected]');
		$mail->setFrom($email);
		$mail->Subject = $subject;
		$mail->isHTML(true);
		$mail->Body = $message;
		$mail->addAttachment($file);
		if ($mail->send())
		    $msg = "Your email has been sent, thank you!";
		else
		    $msg = "Please try again!";
		unlink($file);
	}

In above we send normal email IF you want the send the email in php using smtp then add the below code.

//if we want to send via SMTP
		$mail->Host = "smtp.gmail.com";
		//$mail->isSMTP();
		$mail->SMTPAuth = true;
		$mail->Username = "[email protected]";
		$mail->Password = "5C1bcnPkDI4Wd%#";
		$mail->SMTPSecure = "ssl"; //TLS
		$mail->Port = 465; //587

How to add these code in the above mail.php