Saving IMAP XLS Attchements with PHP - php

I have written a script to check my Gmail account and extract XLS file attachments from the message. I am using the following code to grab the attachment from the body:
$mege = imap_fetchbody($connection,$message_number,2);
The message is being retrieved just fine. Here is a sample of the output for the above code:
-Apple-Mail=_9EBAFC63-4E12-4E64-A4F9-F8D5834F3523 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=us-ascii --Apple-Mail=_9EBAFC63-4E12-4E64-A4F9-F8D5834F3523 Content-Disposition: attachment; filename=test.xls Content-Type: application/octet-stream; x-mac-type=584C5338; x-mac-creator=5843454C; x-unix-mode=0644; name="test.xls" Content-Transfer-Encoding: base64 0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAAQAAAAAAAAAA EAAAIQAAAAEAAAD+////AAAAAAAAAAD///////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// ///////////////bABpAGIAcgBpADEAHgDwAAAACACQAQAAAAIA2gcBQwBhAGwAaQBiAHIAaQAxAB4A8AAAAAgAkAEA...
This is expected since the XLS file is base64 encoded. However, when i decode the message and save to a file, I am getting an empty excel file. But, it is an excel file.
I am using this to decode the content before writing to a file:
$message=imap_base64($mege);
I am wondering if there is anything more I am supposed to be doing to the attachment in order to populate the file.

You are fetching the full body of a mail message, not just the attachment. What is missing is the code to parse the MIME structure, locate the actual interesting part, extract that part from the MIME container and only after that decode according to the Content-Transfer-Encoding header (your "base64" in this particular case). You just cannot blindly base64-decode the whole body and hope to get an attachment in return.

Related

Send pdf output as stringAttachment using PHPMailer

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.

Send HTML email with base64 image using Laravel as API

I'm working on a project that includes sending mails.
For the front-end I'm using VueJS and Laravel for the backend/API part.
There's a part where the user will compose his/her own email in a rich text editor(wysiwyg). Then when the user embeds some image in the said editor, then when I pass it to the API, the image source becomes a base64 encoded image, then when it sends to the email recipient, it just displays the plain base64 text as the image source.
<img src="data:image/png:base64,............" />
The question is how can I display that base64 image in the email, instead of showing plain text base64 text.
I have tried adding headers to the mail, but it seems to not work.
$swiftMessage = $message->getSwiftMessage();
$headers = $swiftMessage->getHeaders();
$headers->addTextHeader('Content-Transfer-Encoding', 'base64');
$headers->addTextHeader('Content-Type', 'multipart/alternative;');
$headers->addTextHeader('Content-Type', 'text/html; charset=ISO-8859-1');
Thanks in advance!
EDIT
If you guys wanna see the headers I received in the email, here it is
Content-Type: text/html; charset=utf-8
Content-Type: multipart/alternative;
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Transfer-Encoding: base64

PHP mail mime and Outlook 2010 Untitled attachments

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

How to include header image to a .ics file using php?

I am trying to create a .ics file to send meeting request using php. Everything is well so far but now I must add an image to the invitation as a header for the body of the message.
How can I add a header image?
I tried to create a meeting using outlook, attach an image to it and then save as .ics file but I get a warning that the attached file may not be view able by all mail clients.
I tried ti add this code but this did not work
Note I am trying to create the .ics file using php.
Thanks
Outlook is trying to add an image in the event. This is indeed not supported by standard iCalendar format.
But from what I understand you want to add an image in the email invitation that will go to each attendee. In that case, you simply replace the text/html bodypart in your invitation with a multipart/related containing a text/html bodypart and an image/xxx bodypart, and link the two with a content-id. See https://www.rfc-editor.org/rfc/rfc2392
To get a concrete example, send an invitation from a Yahoo Calendar as they do send images along with invitation.
Here is the type of MIME structure that they send:
Content-Type: multipart/mixed;
Content-Type: multipart/alternative;
Content-Type: text/plain;
Content-Type: multipart/related;
Content-Type: text/html;
... your html version, which will include the image as <img src="cid:someuniqueid"/>
Content-Type: image/gif;
Content-ID: <someuniqueid>
... your image
Content-Type: text/calendar; charset=utf-8; method=REQUEST
Content-Type: application/ics; name="invite.ics"

Corrupt PDF email attachment when generated by FPDF and PHP

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
....

Categories