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"));
Related
I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!
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 am using Codeigniter as a PHP framework and DOM PDF to generate pdf files. I have the following codes in my Controller.
// Some other codes
include_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$base_path = $_SERVER['DOCUMENT_ROOT'];
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("invoice_$studentid.pdf");
redirect("my_Controller");
The problem is after generating the pdf file it is not redirecting to the Controller. Could you please tell me how to solve this problem?
$dompdf->stream is sending the PDF to the browser. You can't also send a redirect header. You're trying to output two responses to one request, which is impossible.
This doesn't seem like it should be a problem. The browser will stay on whichever page the user was on when they clicked the link to download the PDF. If you really want them to be forced elsewhere (you probably don't, that's a very different user experience from how download links work everywhere else) you could do something with JavaScript.
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!
hi i want to send a image in html format using php mailer class but image show in mail after downloading. but i want to display the image without downloading. is there any option in mailer class or there is another method for this.
or i have to send the the image in another format.
Well, there can be only two possible answers:
you do not want to embed the actual image file with the eMail, then simply put an <img> element into the eMail linking to the image at the remote location, just like you would with any other HTML page. Then cross fingers and hope the client has HTML email enabled and allows display of remote images.
or
you dont want to reference the file from a remote server, but embed it with the eMail. In that case, refer to How To Embed Images in HTML EMail or Attaching an image to an email
If your using PHP Mailer...
$mail = new PHPMailer();
$mail->SetFrom("blah#blah.com");
$mail->AddAddress("blah#blah.com");
$mail->Subject = "Blah"
$mail->MsgHTML('<html><body><img src="logo.jpg">Hello</body></html>');
$mail->AddAttachment("logo.jpg");
$mail->Send();
Using AddAttachment PHP Mailer will check your HTML for a reference to that file and automatically embed it for you.
buddy, its really simple. Write html code as if you write in developing a web page. Give the complete url to the image in the src attribute.
Dont forget to user the function eregi_replace() on body html
$html_message = eregi_replace("[\]",'',$body_html_string);
engoy !!!!! ;)