i'm using TCPDF library to generate a PDF (bill) on the fly and send it via email. It all works but i have a weird problem. When i send the email to a gmail account everything is fine, but when i send it to my mail server i get the email with the pdf but when i open it it doesn't open and i get a message "Adobe reader could not open file.pdf because it's either not a supported format or because the file has been damaged." (the pdf in the email is blank).
I save the PDF into a string like so:
$attachment = $pdf->Output("mypdf.pdf","E");
$attachment = chunk_split($attachment);
and send it via email like so:
$header .= "--".$separator.$eol;
$header .= "Content-Type: application/pdf; name='mypdf.pdf'".$eol;
$header .= "Content-Transfer-Encoding: base64".$eol;
$header .= "Content-Disposition= attachment".$eol.$eol;
$header .= $attachment;
i'm sending the email with php mail function.
The funny thing is if i force the download of the pdf, like so:
$attachment = $pdf->Output("mypdf.pdf","D");
the file is OK and opens without a problem! But if i change it back to "E" it doesn't work.
The other weird thing is that some times i can open the pdf (that i got on my mail server) without a problem, but the next time it wont work (even if i send the exact same email).
Does any one have any idea what is going on? I would like to avoid saving the pdf on the local server.
Why you avoid saving pdf file? You can save it with "F" parameter. After mail has sent you can delete it with unlink function
I see some minor flaws:
Content-Disposition= should be Content-Disposition:
You should end your attachment with "--".$separator."--"
I'm not sure, if this fixes your problems. Anyway I think it's quite complicated to create all the headers manually. I use PEARs Mail_Mime for this task since years and you will find a lot of simple ready to use solutions.
Related
I'm working on building a mailing script in PHP. I have this code:
$mime_boundary = "---".md5(time());
$message = 'Content-Type: application/pdf;name="datenschutz.pdf";charset=utf-8\n';
$message .= "Content-Transfer-Encoding: base64\n\n";
$fileContents = file_get_contents("/datenschutz.pdf");
$message .= chunk_split(base64_encode($fileContents)) . "\r\n\r\n";
$message .= "--$mime_boundary--";
mail($to, $subject, $message, $headers);
I'm aware that PHPMailer exists, alongside other libraries which would make this easier, but the problem is that I want to sell this software eventually and host it on different servers, so installing a library every time wouldn't be optimal. Therefore, I want to mostly use built-in PHP features.
I do recieve the email, and there is a PDF file attached to it, but neither Browsers nor Adobe Reader can open it.
I personally believe that it is because of the Base64 encoding, since I decoded the file once on a webpage and it worked just fine.
How Am I able to tell the Email program/Adobe Reader to decode the file from base64?
Thanks in advance
Edit: I replaced all different linebreaks with \n\n and redesigned the Content-Type strings so that they are strings with double-quotes. Sadly, it still does not work.
Hello I have a simple form which collect files. What I mean is that user should be able to put a file into the field and then by submitting the form mail should be sent to the address which is predefined and hardcoded.
here is my form:
<form action='/?page=admin-send' method='post' class='asholder' enctype=\"multipart/form-data\">\n";
<input type='file' name='file' id ='file'/><button name='accept' value='".$ssl->id."' type='submit'>Send</button>
</form>
And now using Php I want to collect this file and put it into email attachment.
$file = $_FILES["file"]["name"];
$filename = basename($file);
$file_size = filesize($file);
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$msg = "Hello, this is email with attachment!";
$mail = new HTMLMail();
$mail->from = 'DO NOT REPLY';
$mail->to = 'tstmail#testhost.com';
$mail->subject = 'admin warrning';
$mail->importance = 'Low';
$mail->body = "<P><FONT SIZE=2 FACE=\"Tahoma\">$msg</FONT></P>";
$mail->headers = "From: ".$from."\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
."This is a multi-part message in MIME format.\r\n"
."--".$uid."\r\n"
."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
.$content."\r\n\r\n"
."--".$uid."--";
$mail->send();
And here is the scenario I have problem with:
I put file with name of xyz.pdf which is some document the most important thing is that this document is all right. When I submit my form I am getting the email. Email has attachment which name is xyz.pdf but when I am trying to open this file I am getting the message that the file is borken.
Can anyone point me what I am doing wrong?
I have done the same kind of task, i expect that my answer will help you for sure.
You are letting user 1. upload a file > *2.* process it at your server side using PHP > *3.* and them you are using HTML mail class to send your mail.
The problem is in third step. If the class does not provide you with sufficient function of mail, then it is not worth using. Writing mail HEADERS by yourself poses a lot of complexity and we as a developer should not go that inside.
I recommend you to use PHPMailer 5.1.2, to do this job. This is very simple and is of not more than 80KB (the core files, excluding demos, guides).
With this you can attach multiple files of any mime type(pdf, jpeg, doc etc.) without worrying about headers. This is very simple and i have tested , the attachments are readable at the mailbox.
If you download complete package with demos, you will see how easy it is to implement with PHP.
PHPMailer is the best free php class to send mails on the net(i think) with no bugs.
I haven't used HTML Mail class what you are using, so don't know whether it has functions for attaching files. But writing mail headers is not good. If you are using it by your wish after knowing all this, then i am sorry for posting this answer.
I have a web application that builds a dynamic PDF with FPDF and allows you to download it. That works fine. When I try to email it to myself as a test instead of downloading, I get an email with a corrupt PDF attachment.
I have tried the code from http://www.astahost.com/info.php/create-email-pdf-file-39on-fly39-php_t6334.html and http://www.daniweb.com/code/snippet217105.html but get the same result each time.
Has anyone come across this or know a way to fix it?
Your best way to get help here is to subset the full text headers and body of a received message, and place them in your question. Email is encoded, and not all email servers pass all types of encoding. The code you're using specifies "Content-Transfer-Encoding: base64".
Here's a valid JPG encoded with same:
--_eba07140-496e-4f3d-91ce-aff8afde8879_
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="DSC03538_AZ_atCape.JPG"
/9j/4Rt/RXhpZgAASUkqAAgAAAALAA4BAgAgAAAAkgAAAA8BAgAFAAAAsgAAABABAgAHAAAAuAAA
ABIBAwABAAAAAQAAABoBBQABAAAAwAAAABsBBQABAAAAyAAAACgBAwABAAAAAgAAADIBAgAUAAAA
....
I am trying to send an e-mail using the following code:
$htmlHeaders="";
$htmlHeaders = "MIME-Version: 1.0 \r\n";
$htmlHeaders .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$htmlHeaders .= "X-Mailer: PHP/" . phpversion(). "\r\n";
$htmlHeaders .= "From: System <test#test.com> \r\n";
mail("dest#example.com","Subject",'New test <img src="http://www.someserver/image.jpg">',$htmlHeaders);
what I noticed is that for some images (I always link the images through an http link) are working fine and the mail is delivered correctly, while for some other images I get problems; that is, the image is NOT delivered.
For example, trying to use this image wont work: http://img826.imageshack.us/img826/2601/gitaallago.png
I guess it is a problem of headers, but I really do not understand how to fix this.
Any hints?
Often times the problem is the mail client. Some mail clients by default will only download images under a certain size, some will not download any images at all.
Your best bet (though it can be quite a lot more work than what you're doing now) is to attach the image to the email with a CID and reference the image that way (<img src="cid:whatever">)
It will increase the size of your email because you have to transmit the image with each email, but it will display in far more mail clients that way.
Is the correct MIME type being served by the server for the images?
You didn't make it clear whether or not you're saving the image on your server, and linking to it from there, or linking to it straight from Imageshack (in this example.) If you're linking straight from Imageshack, then MIME types definitely aren't the problem. Worth looking into, however, if you're hosting the images on your server.
I'm trying to send an email from the php mail command. I've been able to what I've tried so far, but can't seem to get it to work with an attachment. I've looked around the web and the best code I've found led me to this:
$fileatt_name = 'JuneFlyer.pdf';
$fileatt_type = 'application/pdf';
$fileatt = 'JuneFlyer.pdf';
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
$data = chunk_split(base64_encode($data));
$MAEmail = "myemail#sbcglobal.net";
mail("$email_address", "$subject", "$message",
"From: ".$MAEmail."\n".
"MIME-Version: 1.0\n".
"Content-type: text/html; charset=iso-8859-1".
"--{$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" );
There are two problems when I do this. First, the contents of the email dissappear.
Second, there is an error on the attachment. "Adobe Reader could not open June_flyer.pdf because it is either not a supported file type or because the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded)"
Any ideas of how to deal with this?
Thanks,
JB
The very best way how to deal with mail and php is use a reliable well tested library - email with attachments can easily get very nasty. I personally recommend SwiftMailer.
may be problem is within header. if you want to learn the hard way then figure out how you can configure diffrent mimetypes with headers and do the stuff.
or else easy way is use PHPmailer or other email libraries which will do the hard part for you.
One process to learn the correct email format for sending attachments is to try sending yourself an email (with attachment) using Thunderbird, Outlook, etc.
Then view the source of that email. Try copying and pasting that message source into your PHP code (with a little trimming of headers like To and From and Subject that the mail() function already handles) and bada-bing, you have all you need right in front of you.
You can make it dynamic by replacing the chunks of stuff (HTML part, Text part, attachment) with your unique chunks or variables.
Then no fancy library is needed.