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
Related
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)
I've read several topic on images and mailing, but I simple can't figure this one out.
I'm sending mail with PHP to a user with an image, it delivers the mail, shows that there is a images, but that the images is broken.
Could someone look into my composed mail as it is received for any errors?
Before loading in this images, I checked its type by getimagesize and it says img/png
Any help or suggestions are appreciated.
Subject: Screenshot compare of project: xxx
Content-Type: multipart/mixed;
boundary="multipart_related_boundary"
To: xxx#gmail.com
From: xxx#gmail.com
Subject: Screenshot compare of project: xxx
X-Mailer: PHP/5.4.20
This is a multi-part message in MIME format.
--multipart_related_boundary
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<html>
<head>
<title>Screenshot compare of project: static</title>
</head>
<body>
<p>We have found: 1 differances</p>
<p>chrometestBodyText.jpg</p>
<img alt="diffchrometestBodyText" src="cid:multipart_related_boundarydiffchrometestBodyText" />
</body>
</html>
--multipart_related_boundary
Content-ID: <multipart_related_boundarydiffchrometestBodyText>
Content-Type: image/png;
name=diffchrometestBodyText.png
Content-Transfer-Encoding: BASE64
I decided to use PHPMailer library https://github.com/PHPMailer/PHPMailer
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"
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.
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
....