Skip to content

Instantly share code, notes, and snippets.

@amardeep18
Created November 14, 2019 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amardeep18/068ca2f2a56ec538c769787c87b96b68 to your computer and use it in GitHub Desktop.
Save amardeep18/068ca2f2a56ec538c769787c87b96b68 to your computer and use it in GitHub Desktop.
<?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}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment