1.Create a directory for project on htdoc directory in xampp if you using wamp server then you create your directry on www directory.
2.Open your project directory with command prompt after that write this command:-
composer require mpdf/mpdf

3.After that install phpmailer through composer:
composer require phpmailer/phpmailer

4.Create index.php file and write this code
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Make PDF</title> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="container"> | |
<form method="post" action="makepdf.php" class="offset-md-3 col-md-6"> | |
<h1 class="m-4">Create Your Own PDF</h1> | |
<div class="row mb-2"> | |
<div class="col-lg-6"> | |
<input type="text" name="fname" placeholder="First Name" class="form-control" required> | |
</div> | |
<div class="col-lg-6"> | |
<input type="text" name="lname" placeholder="Last Name" class="form-control" required> | |
</div> | |
</div> | |
<div class="row mb-2"> | |
<div class="col-lg-6"> | |
<input type="email" name="email" placeholder="Email" class="form-control" required> | |
</div> | |
<div class="col-lg-6"> | |
<input type="tel" name="phone" placeholder="Phone" class="form-control" required> | |
</div> | |
</div> | |
<div class="row mb-2"> | |
<div class="col-lg-12"> | |
<textarea name="message" placeholder="Your Message" class="form-control"></textarea> | |
</div> | |
</div> | |
<button class="btn btn-success btn-lg btn-block"> send PDF</button> | |
</form> | |
</div> | |
</body> | |
</html> |
5.create makepdf.php file and write this code
<?php | |
use PHPMailer\PHPMailer\PHPMailer; | |
use PHPMailer\PHPMailer\Exception; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
// Grab variable | |
$fname = $_POST['fname']; | |
$lname = $_POST['lname']; | |
$email = $_POST['email']; | |
$phone = $_POST['phone']; | |
$message = $_POST['message']; | |
//Create new PDF instance | |
$mpdf = new \Mpdf\Mpdf(); | |
//Create our PDF | |
$data = ''; | |
$data .= '<img src="image/cotocus.png" style="width:100px; height:100px;"><br />'; | |
$data .= '<hr>'; | |
$data .= '<table>'; | |
$data .='<tr>'; | |
$data .='<td>'; | |
//Add data | |
$data .= '<strong>First Name</strong>'; | |
$data .='</td>'; | |
$data .='<td>' . $fname . '</td>'; | |
$data .='</tr>'; | |
$data .='<tr>'; | |
$data .= '<td><strong>Last Name</strong></td><td>' . $lname . '</td>' ; | |
$data .='</tr>'; | |
$data .='<tr>'; | |
$data .= '<td><strong>Email</strong></td><td>'. $email . '</td>' ; | |
$data .= '</tr>'; | |
$data .='<tr>'; | |
$data .= '<td><strong>Phone</strong></td><td>'. $phone . '</td>' ; | |
$data .= '</tr>'; | |
$data .= '</table>'; | |
if($message) | |
{ | |
$data .= '<br /><strong>Message</strong><br />' . $message . '<br />'; | |
} | |
//Write PDF | |
$mpdf->WriteHTML($data); | |
//Output to string | |
$pdf = $mpdf->Output('', 'S'); | |
$enquirydata = [ | |
'First Name' => $fname, | |
'Last Name' => $lname, | |
'Email' => $email, | |
'Phone' => $phone, | |
'Message' => $message | |
]; | |
//Run the function | |
sendEmail($pdf,$enquirydata); | |
function sendEmail($pdf, $enquirydata) | |
{ | |
$emailbody = ''; | |
foreach ($enquirydata as $title => $data) { | |
$emailbody .= '<strong>' . $title . '</strong>: ' . $data . '<br />'; | |
} | |
$mail = new PHPMailer(true); | |
try { | |
//Server settings | |
$mail->SMTPDebug = 2; // Enable verbose debug output | |
$mail->isSMTP(); // Send using SMTP | |
$mail->Host = 'smtp.mailtrap.io'; // Set the SMTP server to send through | |
$mail->SMTPAuth = true; // Enable SMTP authentication | |
$mail->Username = 'XXXXXXXXXXXXXX'; // SMTP username | |
$mail->Password = 'XXXXXXXXXXXXXX'; // SMTP password | |
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted | |
$mail->Port = 2525; // TCP port to connect to | |
//Recipients | |
$mail->setFrom('test@myemail.com', 'Test Form'); | |
$mail->addAddress($enquirydata['Email'], $enquirydata['First Name']); // Add a recipient | |
$mail->addBCC('bcc@example.com'); | |
//Attachment | |
$mail->addStringAttachment($pdf, 'invoice.pdf'); | |
// Content | |
$mail->isHTML(true); // Set email format to HTML | |
$mail->Subject = 'invoice'; | |
$mail->Body = $emailbody; | |
$mail->AltBody = strip_tags($emailbody); | |
$mail->send(); | |
echo 'Message has been sent'; | |
} catch (Exception $e) { | |
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; | |
} | |
} |
Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND