I have a php script that will an catch email passed to it and process it.
#!/usr/local/bin/php -q
<?php
while (!feof(STDIN)) {$s .= fgets(STDIN);}
// Now do some work on the email source in $s.
?>
This works fine. My question is how to save an attachment into the file system from the source. For example, if I isolate the section below, how do I need to process it before saving it into a gif file to create a valid gif?
I assume I need to change the encoding, or otherwise process it, but does anyone know exactly?
Thanks!
--------------090607000609050308090504
Content-Type: image/gif;
name="tfk.gif"
Content-Transfer-Encoding: base64
Content-ID: <part1.06050801.05020504#etc....>
Content-Disposition: attachment;
filename="tfk.gif"
R0lGODlheAA8APcAAAAAAP/////Wgv+xEgliOAEcDxAnFiAyHTA+JEBKK0BKLFBWM2BhOv//
nf//tfz60dDEPdnPT//0Z3BtQf/kBv/pKM/KoO3UHendgoB5SOTGB/HSCNm8D7+oEvrhRcW1
U+visL2dA3hlAqqNBM+vB52DBo+ET4+EUJp+AnNgCmdUCZV9EredLJuKQ4x9PVNCBo1yC39n
C6OGFYBpFp+QV4RtI1pMIJWBPq+cXkExBDQuHQwLCDYoBnRdIM+zbMCnZaiWabeleH1zWu/K
e9+/dNi5dntsSaSPYpWDXHJmS6CQbl1UQOOYB/6xEv2wEv2vEvuuEvqtEvmsEvirEvaqEvWq
EvSpEvOoEvGmE+6kE+yjE+uiE+qhE+mgE+efE+SdE+OcE9+ZEykdBtWvZejAcuS9cfHIeO/H
d/nQffXMe/PLevzTf/rRfvnRfvjPffbNfPLKeurDdv7Vgf3UgfzTgPvTgPrSf/fPfvbOffTN
fPHKe+/Jeu7Ied+7cty5cP7Vgv3VgvXOfvPMfdOxbOvGete0b7+hZfjRhNGyc+rIgti5eLWb
aL+lb9K3gMWtfYh4V3dpTKyadGRcTOGaE9qVFNWRFNGOFM6MFMyKFMqJFMiHFMWFFMOEFMGC
Fbx/FTQoE9CpYs2mYduzadiwZ962a921a9yzauC4beO7b5F7UeG/gO/Mis2vd3ZpUZaGaW5j
T4l9aLd7FbJ3Fa50FatxFahvFqZuFh0VCLaPUMKZV7yUVMigXEhD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gBozJmGlgBmZwBmdATiXGDJwABl7wBaJlEXOAC7UQ
cF7QexSRLGszB3LwB62pEa5pUprjkX4zEnKAC7aQBS3WBE2AfhYhbC45QZ3QlJ9Xm8b5e9cQ
nAMgeRLxB2sgCt+AS4uXXqlpNMd5nRNBedrmQZdgDoepBuhgDtr/UIyRZw4mV5zYaRI/cAIL
oB4n0CUScQIKgANiIp8SYQbccI4TNAsf9Q3VYAvB53FiZw7yV53p2RII4Cx3cQKBcQB88SEC
kAGBsQCfIgfiJUhDVQmUQFFARgWTEAzFwGSndKArQSoSQaHg8SnrKQAn8CntKRF/NAtbgGRb
FAVVgAVcMAmZ4AvgoAtviFAkOhKEsQAS0yoTehcr2qLTQlrH0AyycFuUUAmawAmxMAu9MAzN
sA3SkAsjlBHJEqQdoQB8gQMKsBclwhmEQQMnkKJDoABJMxyjcAxtBQ3ZIDvGgAy6EAqmYAZa
96Vg+hFZMxFEkDTfwRdOozUfaRJ++qeMGvoRi9qokKoRjxqplFo0EHGpmJqpmrqpCBEQADs=
--------------090607000609050308090504--
Mailparse
Try the PEAR_Mail_MimeDecode class from Pear. You can feed it the entire mail, and get back the various MIME chunks... and no need to install an extension.
For a PHP solution (no C extensions required), there's PEAR Mail_mimedecode.
After parsing the e-mail, you already have the attachment bytestream, no need to base64 decode yourself.
Related
I am trying to attach a file using the PHPMailer's addStringAttachment method.
I generated the pdf from TCPDF using:
$b64_enc_doc = $pdf->Output($file_name, 'E');
Then inside of the script to send the email, I have
...
$mail->addStringAttachment($b64_enc_doc, $file_name)
However, the PDF attachment is corrupted when I try opening it.
I tried debugging the output of the PDF generated from TCPDF and it looks something similar to this:
Content-Type: application/pdf;
name="file.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="file.pdf"
JVBERi0xLjcKJeLjz9MKMjEgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgL1BhcmVudCAxIDAgUiAvTGFz
dE1vZGlmaWVkIChEOjIwMjIwMTA0MTMwODQ2KzAwJzAwJykgL1Jlc291cmNlcyAyIDAgUiAvTWVk
aWFCb3ggWzAuMDAwMDAwIDAuMDAwMDAwIDU5NS4yNzYwMDAgODQxLjg5MDAwMF0gL0Nyb3BCb3gg
WzAuMDAwMDAwIDAuMDAwMDAwIDU5NS4yNzYwMDAgODQxLjg5MDAwMF0gL0JsZWVkQm94IFswLjAw
MDAwMCAwLjAwMDAwMCA1OTUuMjc2MDAwIDg0MS44OTAwMDBdIC9UcmltQm94IFswLjAwMDAwMCAw
LjAwMDAwMCA1OTUuMjc2MDAwIDg0MS44OTAwMDBdIC9BcnRCb3ggWzAuMDAwMDAwIDAuMDAwMDAw
IDU5NS4yNzYwMDAgODQxL...
Please I do not want to save the file to the server but send the file straight to the user's email. Please help.
Thanks!
You don't show the code you're using, but that string is a fragment of an email message, not a PDF binary PDF string as it should be.
Your code should be along the lines of (I don't remember tcpdf's syntax offhand):
//Render the PDF to a string in memory
$pdf = $tcpdf->output('S');
$mail->addStringAttachment($pdf, 'file.pdf');
PHPMailer will take care of the rest.
I'm using Pears mail mime to send out emails with attachments in php.
http://pear.php.net/package/Mail_Mime/redirected
It seems Outlook is renaming PDF's to "Untitled..." but it works correctly for gmail, hotmail etc.
Prior to this problem I had another problem where Outlook would not show the file as PDF and attach it as text file unless I specifically tell the mime addAttachment the file type
see the code here:
if (endsWith($file,".pdf")) {
$mime->addAttachment($file,'application/pdf',basename($file));
}
else {
$mime->addAttachment($file);
}
You can see above I've also tried setting the 3rd argument of addAttachment to set the filename, but that hasn't worked either.
Outlook does not rename other file types so it's a catch 22 either set no filetype and have Outlook send as txt file or set the filetype and Outlook renames the files to 'Untitled'
ok found out that Outlook doesn't seem to like long filenames, shortening the filename works.
Pear mime sends a long filename header broken up into parts like this:
Content-Transfer-Encoding: base64
Content-Type: application/pdf;
name*0*=US-ASCII''THIDSO5749_010.901%20-%2013.95505142_GARUDA%20RETARGETI;
name*1*=NG%20CPA%20CAMPAIGN_PT%20GARUDA%20INDONESIA%20%28PERSERO%29%20TBK;
name*2*=._NOV2013.pdf
Content-Disposition: attachment;
filename*0*=US-ASCII''THIDSO5749_010.901%20-%2013.95505142_GARUDA%20RETAR;
filename*1*=GETING%20CPA%20CAMPAIGN_PT%20GARUDA%20INDONESIA%20%28PERSERO;
filename*2*=%29%20TBK._NOV2013.pdf;
size=68426
Outlook can't handle this but it can handle the format:
Content-Type: application/pdf;name="a long filename.....pdf"
Unfortunately it seems Pear mime can't send this format without editing the core code of mime.php
So I'm just sending shorter filenames for now
My problem is exactly like this one, I am getting the attachments without the extension, if I put the extension to the file manually, the file is still not recognized. What could cause this?
This is the attachment-adding part, rest is standard:
foreach($_FILES['uploads']['name'] as $key => $file){
$mail->attachment[] = array($_FILES['uploads']['tmp_name'][$key], $_FILES['uploads']['name'][$key]);
$mail->boundary = "_b" . md5(uniqid(time()));
}
EDIT:
In the beginning of every attachment I get this:
name="phpmailer.inc.php"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="phpmailer.inc.php"
is this normal?
EDIT:
Also, I copied the files to the server and I am adding them to the attachment from there, still, all the files are "noname" and with the correct size.
I think you can not use $_FILES['uploads']['tmp_name'][$key] file as an attachment as $_FILES['uploads']['tmp_name'] are temperory files.
First you need to upload that document/file to the server and than attached it to the mail. Than only it will work.
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
....
Say, I have an application/xhtml+xml content and a gif image. How can I serve (not render) these two in a single http get request (using PHP)? It should be a multipart/related content. After reading this RFC, I tried something; but it did not work. (No, I am not trying to send emails with attachment)
Thanks in advance.
EDIT: Finally I succeeded doing that. I think, it will be useful if I write how I did that. I have added this as an answer. Please look below.
I'm afraid you can't for the purposes you want.
If you want to do this to serve web pages, as far as I can tell, the browsers won't work with such MIME responses for rendering pages.
If you want an example of how this messages works, send yourself an email with an attachment and on your email client (not an webmail) go to the "View source" option on the email body.
You'll see your message, the attachment and possibly other parts on the same message using the MIME Multipart encoding.
OTOH, if you want it to send email, there are libraries, like PHPMailer, that will do all the encoding for you.
If that's what you want, check this example at their website.
Edit:
You could use PHPMailer to build the message, then you just use the result, instead of actually sending the email.
Try something like this:
** This is untested code, just for a starting point **
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mime_message = $mail->CreateBody();
echo $mime_message;
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
Here is it [worked for me] (No, I did not used PHPMailer; may be it is useful, but I could not make it working):
// Two contents : application/xhtml+xml and image/gif
// Here we go
<?php
$boundary = "ghorar#deem";
$im_len = filesize("path/to/abc.gif")
header("Content-Type:multipart/related; boundary=\"$boundary\"; type=\"application/xhtml+xml\"");
$xml_cnt = <<<EOD
<media xmlns="http://www.sth.com/sth.xsd">
<objectURI>cid:112509abc#syz.com</objectURI>
<size>$im_len</size>
<type>image/gif</type>
<name>abcxyz</name>
<description>blah blah</description>
</media>
EOD;
$xml_len = strlen($xml_cnt);
$im = imagecreatefromgif("path/to/abc.gif");
$to_send = "This is a multi-part message example in MIME format
--$boundary
Content-Type: application/xhtml+xml;
Content-ID: <e4509xml#asd.com>
Content-Length: $xml_len
$xml_cnt
--$boundary
Content-Type: image/gif;
Content-ID: <112509abc#syz.com>
Content-Length: $im_Len
Content-Transfer-Encoding: binary
";
echo $to_send;
imagegif($im);
echo "\r\n--$boundary--";
imagedestroy($im);
?>
You can load the xml from file also. But, note that if your xml refers the image (like I did here) you need to refer that with its Content-ID. Also, note the '<' '>' characters in the Content-ID field of the image (after the boundary) and the 'cid:' notation in the place where it is referred. It worked for me. Thanks to Carlos Lima for spending time for me.
I've been trying to achieve the same thing.
It appears that MSIE is the only browser to currently support multipart/related natively (Opera may have some support - but I;ve not played around with it) however when loading such files, it seems to totally ignore the HTTP headers regarding mime type and other stuff (such as caching information, disposition etc). Indeed MSIE will only open the file if the URL has a .mht file extension (i.e. its breaking most of the rules about HTTP).
Also, MSIE will happily load anything which contains an embedded multipart/related file (provided it is encoded as plain text) without rendering the data that the attachment is contained within. It looks like the functionality for handling these files is a quick hack to allow viewing of the files
If you reconfigure your webserver to parse .mht urls using php (or use mod_rewrite to remap the URL) then you'll probably find that it may work (although I wouldn't hold out a lot of hope of it working with a query string appended). But expect problems if you are generating dynamic content (it won't expire from the cache / refresh when you expect).
To cut a long story short - its not going to work.