I am having some issues with trying to get SwiftMailer to attach a file I have created with FPDF. Basically I have a page called createPDF.php that is dynamically generated based on the ID number in the URL. This page is set to output the PDF inline using $pdf->Output("filename.pdf",I);. What I want to do is to be able to attach this file to an email using SwiftMailer from another page simply by calling my createPDF.php?id=xxx link.
From the PHP page where I want to send the email from, everything works, except the attachment. It attaches something, but not what I want and it is not viewable in a PDF viewer on my local machine. The line specific to the attaching the file is:
->attach(Swift_Attachment::fromPath('createPDF.php?id=xxxx'))
This does not work, but surely, it must be possible without saving the file on my web server by FPDF.
Is this possible? If so, how?
Thanks!
The problem here is Swiftmailer gets the file contents, it does not execute your php file. So the contents of your PDF will the code that is in createPDF.php.
why cant you safe the file first? You should be able to safe it and delete it when your email is sent.
<?php
$id = "xxx";
$fileName = "tmp/".sha1(time()+mt_rand(0,99999999));
include "createPDF.php"; //saves it to $fileName
->attach(Swift_Attachment::fromFile( $fileName )->setFilename('blaha.pdf'));
unlink($fileName);
Ok, so I just figured this out.
Basically I made a new PHP file with the bulk of my createPDF.php file as a function and simply passed in two variables into the function as my $id and an $output variable. $output is simply the way that FPDF outputs the file — inline, etc... I then set the function to return the output of the FPDF. In my createPDF.php file I simply call my function passing in $id and 'I' as the variables so it displays the correct PDF inline in the browser.
In my sendEmail function I simply pass in $id and 'S' and set it to a variable $content, which I pass into SwiftMailer as an attachment.
Works great.
Thanks for your help!
Related
I'm using PHPMailer to send automatic email (obviously using PHP as programming language).
I would like to add a PDF attachment generated with TCPdf.
Unfortunately I cannot generate the PDF inside the php page where I'm using PHPMailer, and I cannot create a function that generate it.
I can only use a link to generate it, like this:
www.mypage.com/app-pdf/link_generate_pdf.php?IDToGenerate=131&PDFOutput=I
I was thinking that I can recall the page with the PDFOutput=S and "return" in some way the text of the PDF and add it as attachment.
Otherwise I can call the page with PDFOutput=F and save it to a temp folder and then attach it to the email.
The problem is that I don't know how to "call" a page as it were a function and return what the recalling page actually returns.
Do you have some suggestion?
Thank you
edit: I now understand the problem! The problem is that the URL is accessible only from autentication (login page). I thought that as I was logged in, the script was automatically capable of read the page. How can I solve this?
Use this to save the file on the server first and then attach
file_put_contents("Tmpfile.pdf", fopen("http://example.com/file.pdf", 'r'));
Attach as
$mail->AddAttachment('path_to_pdf/Tmpfile.pdf', $name = 'Name_of_pdf_file',
$encoding = 'base64', $type = 'application/pdf');
Hope this helps
Edit: Try this. works fine over here
file_put_contents("path_to_pdf/Tmpfile.pdf",
file_get_contents("http://example.com/file.pdf"));
I am using FPDF plugin. I want to send email with pdf file. Everything is working fine. But, the problem is, after sending email my browser still shows the pdf file because of the content type header. Is there any way to disable that thing in certain condition?
Thanks.
If you use $pdfContent = $pdf->Output('', 'S'); there shouldn't be any content send. See here for more details.
I have a script that currently takes some data from a wordpress DB and then loops over the returned rows and uses fputcsv(). The file handle was setup with:
$fh = fopen('php://output', 'w');
The script is linked to a link of a webpage and when you click the link it downloads the CSV (using the content-disposition header).
Is it possible to write to php://output this CSV file and then use PHP's mail() function to send it in an attachment?
I have a mail function I've written that will set the MIME type to multipart/mixed, I'm just not sure how to create the actual attachment to be emailed.
Use tmpfile() instead. Write your CSV data to that temp file, then attach it to your email.
Plus, don't write your own mime handling/generating functions. Use a library like PHPMailer or Swiftmailer to do it for you. Far easier and far more reliable. Part of their attachment handling code allows you specify the filename the user sees, so even though it might be "/tmp/abc123def", it'll show up as "data.csv" (or whatever you specify) in the actual email.
I'm generating a pdf file with html2fpdf.
$pdf = new HTML2FPDF();
$pdf->HTML2FPDF("P","mm","A4");
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->output('sample.pdf');
This sample works great. But:
How do I delete the pdf after the output? I just want to have links in my tool, the users can download the pdf and after that it shoud be deleted on the server.
How can I 'clean up' after generating the pdf?
You can use PHP's file deletion function called unlink()
Call this function with the full path to the generated PDF file (or any file for that matter) and PHP will delete that file.
http://php.net/manual/en/function.unlink.php
You don't necessarily have to delete the file immediately after the user has downloaded it. You can just as easily place all the generated files in one central folder and have a cron job execute a more general clean up script simply removing the older files.
One method could be -
Scan the contents of the folder using scandir().
Iterate over its files in a foreach loop..
Inspect the creation time of each file using filemtime().
If the creation time was over hour ago, delete the file using unlink().
Because you are generating the PDF file yourself within your PHP code, I didn't mention the permissions consideration. Here would be a good place to mention that your PHP must have the correct file system permissions in order to perform any action on the file system. You are creating a PDF file so it's safe to assume that you have the correct permissions to make changes to the file system but if you plan on using this unlink() function in other scripts make sure that the files you are dealing with have the correct permissions set.
If you don't add the 'F' flag to the output function there will be no pdf files stored on the server at all:
$pdf->output('sample.pdf', 'F'); //stores PDF on server
In your case the script itself behaves like an actual pdf file. So, creating a link to the script is just like a link to the pdf, except that the PDF is created every time the script is requested. To tell the browser it's a PDF the content-type response header must be set to application/pdf:
content-type: application/pdf
This way the broser knows that it's a pdf even if the URL is ending in a .php. You can use rewrite engine to make it end in pdf or whatever else.
Sending the headers is done by the fpdf/tcpdf. In short: you don't have to do any cleanup, because no pdf file is stored on the server.
If you wonder what the name is for than, try saving the pdf file. The recommanded name when saving will be sample.pdf.
Reference:
PHP header() function, at the examples there is one for sending pdf
FPDF::Output()
TCPDF::Output()
I was just wondering if I could have a variable to hold an image, I'm using phpmailer to send email and I need an image to be attached to it,
so I was wondering if I could put the image in a variable and use
$mailer->AddAttachment($image);
to send the email with attachment.
thanks for your help.
With PhpMailer adding an attachment is done the way you wrote it in the question
$mailer->AddAttachment('/home/mywebsite/file.jpg', 'file.jpg');
If you want to use a variable you can change the string by a variable without problem.
$imagePath = '/home/mywebsite/file.jpg';
imageName = 'file.jpg'
$mailer->AddAttachment($imagePath, $imageName);
I guess $image should contain local path to the image file.
If you look at phpMailer source, at line 1218:
http://phpmailer.svn.sourceforge.net/viewvc/phpmailer/phpmailer/trunk/class.phpmailer.php?revision=444&view=markup
you'll see that it verifies at first that what you have given is path to existing file. There is no other option.
Unless I'm missing something, that's exactly how it's supposed to be used.
According to this document, you'd do something like this:
$myImg = '/some/path/to/image.jpg';
$mailer->AddAttachment($myImg);
Is that not what you're trying?
Why cant you do this this way? Sending email attachments in PHP Using phpmailer class !