How can I dynamically send multiple FPDF's via mail in PHP ?
I am a new user. The scripts work fine individually. mail.php also works when I attach one pdf. But I need to attach 2 of them to the same mail. Could someone please help me find a way around? Thank you.
I have atttached one of the created pdf's. There's another one with the same code snippets. I have also attached the script I used to send the email.
createpdf.php
<?php
$email_id = $_GET['e'];
$connect=mysqli_connect("localhost", "user",
"password","db") or die("Connection failed1:".mysqli_connect_error());
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$email_id = $email_id;
$query = $connect->query("SELECT first_name FROM `db` WHERE email_id = '$email_id'");
$array = Array();
while($result = $query->fetch_assoc()){
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,10,'Dear ' .$result['first_name']. ', This is the 1st doc.');
}
$pdf->Output();
?>
mail.php
<?php
require('createpdf.php');
$to = $_GET['e'];
$subject = "test";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: X < noreply#test.com >\n";
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;
boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream;
name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
?>
Related
i handcraft a php file and it works. it sends a fpdf as attachment. but now, how to use umlauts in sender, subject and text and how use html-content instead of only unformatted text in mail body?
here is my code:
<?php
require('fpdf/fpdf.php');
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10, "this is a pdf example");
$to = "to#blah.com";
$from = "blah <blah#blah.de>";
$subject = "Test";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "Test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; charset=utf-8; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "hallo, test".$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
//name of pdf file
$pdf_file = "test.pdf";
// additional output pdf
$pdf->Output('I', $pdf_file);
?>
can you help me? im simply to stupid.... i have tried many things with utf etc. but it doesnt work...
you should use PHPMailer - Mailing library
https://github.com/PHPMailer/PHPMailer
it is easy to handle & it never fails to send an email
I am trying to send email with an attachment via mail function and FPDF library.
But when I send it, the email comes just with some strings and encoded characters.
Here is my code bellow:
Code to send email
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfGenerate = new RelatorioManutencaoController();
$pdfdoc = $pdfGenerate->imprimir(base64_encode($manutencao->id));
// return $pdfdoc;
$attachment = chunk_split(base64_encode($pdfdoc));
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream;
name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
$sender = "diakuzena#gmail.com";
$emissor = "diakuzena#gmail.com";
$assunto = "Assunto do email";
$header = 'MIME-version:1.0' . "\r\n";
$header .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";
$header .= 'From:' . $sender . '<' . $emissor . '>';
mail($sender, $assunto, $arquivo, $header);
mail("diakuzena#gmail.com", $assunto, $body, $header);
Someby please help me solving this problem.
Try using the command: php artisan make:mail this generates a specific controller for sending mail, in the resources add the mail template and in the controller generated by the command in the build function include the following code :
public function build()
{
// attach method with a file in your system
return $this->view('emails.your mail template')
->attach('/path/to/file');
}
docs of attachments!
or for inline attach in the html template add this:
<body>
Here is an image:
<img src="{{ $message->embed($pathToImage) }}">
</body>
docs of inline-attachments!
to send it, just call this mail handler:
Mail::to($request->user())->send(new YourGenerateMailController());
For more information check the documentation
https://laravel.com/docs/7.x/mail
$path=$_SERVER['DOCUMENT_ROOT']."/unwant/test3.pdf";
$filename="test3.pdf";
$pdf->Output($filename,'F');
i got my output file in this code $pdf->Output($filename,'F'); i want to attach my output file in mail.
require('lib/fpdf/fpdf.php');
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/image.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');
// email stuff (change data below)
$to = "myemail#example.com";
$from = "me#example.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
To use PHPMailer:
Download the PHPMailer script from here:
http://github.com/PHPMailer/PHPMailer
Extract the archive and copy the script's folder to a convenient
place in your project.
Include the main script file --
require_once('path/to/file/class.phpmailer.php');
Now, sending emails with attachments goes from being insanely difficult to incredibly easy:
$attachment= $pdf->Output('attachment.pdf', 'S');
$mailer->AddStringAttachment($attachment, 'attachment.pdf');
I'm using fpdf to output a file to the server and then trigger an email. This works but there will be sensitive information in the pdf. How do I output the pdf to a folder above the public root? And then how do I retrieve that file again through a link?
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output('/applications/doc.pdf','F');
?>
<?php
$to = "oelbaga#newworldgroup.com";
$from = "Omar <oelbaga#newworldgroup.com>";
$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "doc.pdf";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdf));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
?>
Got it, although there are probably major security holes with this.
Code to output the fpdf file above the public webroot:
$pdf->Output('...physical..path.../doc.pdf','F');
Retrieve that pdf and force a download:
$oefilename = "doc.pdf";
$rootDir = realpath('/var/www/vhosts/lavaansmile.com/private/');
$fullPath = realpath($rootDir . '/' . $oefilename);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=' . basename($fullPath));
header('Content-Length: ' . filesize($fullPath));
#readfile($fullPath);
To get the physical path to your page you can use this:
//get real path
$path = 'NameofYourPage.php';
echo realpath($path);
I am using this function for attaching pdf in my mail. It's working fine but when the user tries to open the pdf file he gets a message telling him that file can't be opened, because of a problem with file formate.
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\r\n";
// attachment name
$filename = $invoice.'.pdf';
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode($filename));
//$attachment = $filename;
// main header
$from = "test#productionserver.in";
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
mail($to,$subject,$body,$headers);
Does anyone have an idea what I made wrong ?
$attachment = chunk_split(base64_encode($filename));
You are encoding the filename as your attachment. You need to encode the actual file and not just its name.
$attachment = chunk_split(base64_encode(file_get_contents($filename)));