How to include header image to a .ics file using php? - 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"

Related

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

Using Content-ID and cid for embedded email images in Thunderbird

I'm generating emails in a PHP application which attach multiple files to an HTML email. Some of the files are Excel spreadsheets, some of the files are company logos which need to be embedded in the HTML so they load by default using Content-ID and cid identifiers to refer to the attached images.
As far as I can see, my syntax is correct, but the images don't ever load inline (they are attached successfully, however).
From: email#example.com
Reply-To: email#example.com
MIME-Version: 1.0
Content-type: multipart/mixed;boundary="d0f4ad49cc20d19bf96d4adf9322d567"
Message-Id: <20150421165500.0A5488021B#server>
Date: Tue, 21 Apr 2015 12:54:59 -0400 (EDT)
--d0f4ad49cc20d19bf96d4adf9322d567
Content-type: text/html; charset=utf-8
Content-transfer-encoding: 8bit
<html>
Html message goes here, followed by email.<br/>
<img src="cid:mylogo" />
</html>
--d0f4ad49cc20d19bf96d4adf9322d567
Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name=excelsheet.xlsx
Content-Description: excelsheet.xlsx
Content-Disposition: attachment;
filename="excelsheet.xlsx"; size=24712;
Content-transfer-encoding:base64
[base64 encoded string goes here.]
--b19e863e2cf66b40db1d138b7009010c
Content-Type: image/jpeg;
name="mylogo.jpg"
Content-transfer-encoding:base64
Content-ID: <mylogo>
Content-Disposition: inline;
filename="mylogo.jpg"; size=7579;
[base64 encoded string goes here.]
--b19e863e2cf66b40db1d138b7009010c--
Can anybody see an obvious reason why the image won't embed as expected?
EDIT
Note this behaviour isn't general to all email clients. So far only noted in Thunderbird.
I noticed two issues:
The MIME-boundary is inconsistent. For the first attachment it's d0f4ad49cc20d19bf96d4adf9322d567 and then b19e863e2cf66b40db1d138b7009010c is used. Thus, technically the second attachment is "part" of the first attachment.
If you replace all b19e863e2cf66b40db1d138b7009010c by d0f4ad49cc20d19bf96d4adf9322d567 Thunderbird correctly identifies the image attachment.
Use multipart/related instead of multipart/mixed. (see RFC2387)
A multipart/related is used to indicate that each message part is a component of an aggregate whole. It is for compound objects consisting of several inter-related components - proper display cannot be achieved by individually displaying the constituent parts. The message consists of a root part (by default, the first) which reference other parts inline, which may in turn reference other parts. Message parts are commonly referenced by the "Content-ID" part header. (see Wikipedia entry for MIME multipart/related)

Saving IMAP XLS Attchements with 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.

Apple Mail does not display PDF sent using Zend_Mail

My web application creates PDF documents using Zend_Pdf and sends them using Zend_Mail. It also attaches some user uploaded documents (also PDF). The attachments show up in all general used mail programs, except in Apple Mail. The created PDF is about 30 KB and the message is sent using an external mail server.
In Apple Mail the message list shows the message with a paper clip (indicating that it has attachments), but when the message is opened no attachments are visible. When I click 'Details' in the message head, it shows the attachments and the option to save them.
This is the (stripped down) code that sends the e-mail:
<?php
$mail = new Zend_Mail('utf-8');
$mail->setFrom('niels#example.com', 'Niels')
->setSubject('Subject')
->addTo('niels#example.com', 'Niels')
->setBodyHtml('Hi there', 'utf-8', Zend_Mime::ENCODING_8BIT);
$a = new Zend_Mime_Part($pdfContent);
$a->type = 'application/pdf';
$a->filename = 'my_pdf.pdf';
$a->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$a->encoding = Zend_Mime::ENCODING_BASE64;
$mail->addAttachment($a);
$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
$mail->send();
The mail I receive has the following header for the message
Content-Type: multipart/mixed; boundary="=_f6a669390c6713f60a851af814fe897f"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
The HTML mail content:
--=_f6a669390c6713f60a851af814fe897f
Content-Type: text/html; charset="utf-8"
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
The attachment content:
--=_f6a669390c6713f60a851af814fe897f
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="my_pdf.pdf"
Is there a way that the attachments show up in the message in Apple Mail? Now some people respond with 'there are no attachments'. Does Apple do some 'smart' things to hide/show the attachments? Or should I use another Content-Disposition etc? I've searched quite some time to find a solution, but am running out of clues.
This is not Zend issue but Apple Mail issue. Install Thunderbird ;-)
Here are few tips:
problem like this can occur if you are sending inline files and attachments at the same time
mail app settings are defaulted to inline attachments
Close Mail
Open Terminal
enter command defaults write com.apple.mail DisableInlineAttachmentViewing -bool yes,
Open Mail and try again
you should not be explicitly specifying encoding in setting body, your mail is initialized with default 'UTF-8' encoding by the way you
you should not be encoding header unless you are sending emails in languages that use not Roman letters-based character set
Try attaching the file inline and
$mail = new Zend_Mail('utf-8');
$mail->setFrom('niels#example.com', 'Niels')
->setSubject('Subject')
->addTo('niels#example.com', 'Niels')
->setBodyHtml('Hi there');
// add attachment
$mail->createAttachment(file_get_contents('my_pdf.pdf'), 'application/pdf', Zend_Mime::DISPOSITION_ATTACHMENT , Zend_Mime::ENCODING_BASE64);
// try sending attachment inline... maybe this will work (not sure if supported by all mail clients)
// $mail->createAttachment(file_get_contents('my_pdf.pdf'), 'application/pdf', Zend_Mime::DISPOSITION_INLINE , Zend_Mime::ENCODING_BASE64);
$mail->send();
The answer that Alex provided was indeed the solution: it is an Apple Mail issue. When the attachment size increases, it shows up in the e-mail. For example: I just sent a PDF document that is bigger (79KB, shows up as 112KB in the message list) and that is visible. If I send the same kind of PDF (but smaller) it is hidden.

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