PHP Send pdf file as attachment in email
I am trying to send a pdf file as attchment in my email.Using following code.
Send pdf file as attachment in email php |
PHP send pdf file extension mpdf download and include this mpdf.php file in your code. Send pdf file as attachment in email php
include("MPDF53/mpdf.php");
$mpdf = new mPDF('c', 'A4-L', '', '', 0, 0, 0, 0, 0, 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$filename = "Jims_tradenet_form.txt";
content.='<html lang="en-AU"><head><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css">
.form-group { padding-bottom: 5px; }
label { font-size:12px; }
</style>
</head>
<body>
<div class="container">
<section class="header" style="border-bottom:1px solid #000;">
<div class="row">
<div class="col-md-3" style="min-height: 70px; max-height: 150px;">
<img src="https://www.nbaysitsolutions.com.au/images/logo.png" class="pull-left" style="max-width: 300px; opacity: .7;" />
</div>
<div class="col-md-6 text-center">
<h3 class="center" style="opacity:0.7; font-family: helvetica; font-weight: bold;">MY FIRST PDF FILE SEND MAIL</h3>
</div>
<div class="col-md-3 center">
<img src="https://www.jimstradenet.com/basis/images/tradex-logo.png" class="pull-right" style="max-width: 300px; opacity: .7; max-height: 60px;" />
</div>
</div>
</section>'
$file = fopen($filename, "w");
fwrite($file, $content);
$mpdf->WriteHTML(file_get_contents($filename));
$m = time();
$n = $m . '.pdf';
$m = 'files/' . $m . '.pdf';
$mpdf->Output($m, 'F');
$_SESSION['file'] = $m;
//$mpdf->Output();
$emailBody .= '';
$fileatt = $m; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = $n; // Filename that will be used for the file as the attachment
$email_from = "dev@gmail.com"; // Who the email is from
$email_subject = "Attached File"; // The Subject of the email
$email_message = $emailBody;
$email_to = 'dev2tricks@gmail.com'; // Who the email is to
$headers = "From: " . $email_from;
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers.= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message.= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message.= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message.= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data.= "\n\n" .
"--{$mime_boundary}--\n";
mail($email_to, $email_subject, $email_message, $headers);
$msg = "Successfully Inserted";
<style type="text/css">
.form-group { padding-bottom: 5px; }
label { font-size:12px; }
</style>
</head>
<body>
<div class="container">
<section class="header" style="border-bottom:1px solid #000;">
<div class="row">
<div class="col-md-3" style="min-height: 70px; max-height: 150px;">
<img src="https://www.nbaysitsolutions.com.au/images/logo.png" class="pull-left" style="max-width: 300px; opacity: .7;" />
</div>
<div class="col-md-6 text-center">
<h3 class="center" style="opacity:0.7; font-family: helvetica; font-weight: bold;">MY FIRST PDF FILE SEND MAIL</h3>
</div>
<div class="col-md-3 center">
<img src="https://www.jimstradenet.com/basis/images/tradex-logo.png" class="pull-right" style="max-width: 300px; opacity: .7; max-height: 60px;" />
</div>
</div>
</section>'
$file = fopen($filename, "w");
fwrite($file, $content);
$mpdf->WriteHTML(file_get_contents($filename));
$m = time();
$n = $m . '.pdf';
$m = 'files/' . $m . '.pdf';
$mpdf->Output($m, 'F');
$_SESSION['file'] = $m;
//$mpdf->Output();
$emailBody .= '';
$fileatt = $m; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = $n; // Filename that will be used for the file as the attachment
$email_from = "dev@gmail.com"; // Who the email is from
$email_subject = "Attached File"; // The Subject of the email
$email_message = $emailBody;
$email_to = 'dev2tricks@gmail.com'; // Who the email is to
$headers = "From: " . $email_from;
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers.= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message.= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message.= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message.= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data.= "\n\n" .
"--{$mime_boundary}--\n";
mail($email_to, $email_subject, $email_message, $headers);
$msg = "Successfully Inserted";