πŸš€ DevOps & SRE Certification Program πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 πŸ” Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to send pdf file through phpmailer?

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

This image has an empty alt attribute; its file name is image.png

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}";
}
}

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.