Read MIME data using PHP - php

I have a third party program which basically allows users to send email and then it displays it in the system. But the problem is that it is generating an output like this: I want to just take this data and format it to something presentable. I would like to avoid REGEX. Are there any options or standard ways of displaying the content below in a more presentable fashion. Basically I will associate everything below as $text and then call a function clean($text) of sorts.
> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--B_3331365494_4098727
Content-type: text/plain;
charset="US-ASCII"
Content-transfer-encoding: 7bit
test
--B_3331365494_4098727
Content-type: text/html;
charset="US-ASCII"
Content-transfer-encoding: quoted-printable
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>test</SPAN></FONT>
</BODY>
</HTML>
--B_3331365494_4098727--

PEAR::Mail_mimeDecode is a great class to decode MIME messages. Once installed, you can use it as such:
$message = new Mail_mimeDecode($text);
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$messageStruct = $message->decode($params);
//messageStruct is now an array representing the message
// with all the parts properly included.

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

Parse multi-part emails encoded in base64 utf8 using PHP

I have a multi-part message that I would like to decode (see below).
I'm able to use PHP's base64_decode function to decode normal emails, but it wont work for multi-part emails.
Is anyone aware of how to do this or know of any scripts available?
--_000_FBA91459E616EF4B8C1CCF54B389A283030E5EMX105CL01corpemcc_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
SGVsbG8sIEkgY2Fu4oCZdCBzZWVtIHRvIGdldCB0aGUgbGluayB0byB3b3JrIGFmZmVjdGl2ZWx5
IHRvIGVudGVyIGluIHRoZSBuYW1lcyBvZiBwZW9wbGUgdG8gc2VuZCB0aGUgc3VydmV5IHRvb+KA
pml0IHdvbuKAmXQgbGV0IG1lIGVudGVyIHRoZSBuYW1lcy4gQmUgZ3JlYXQgaWYgeW91IGNvdWxk
IGhlbHAgb3Igc2hvdWxkIEkganVzdCB3YWl0IGEgbGl0dGxlIHdoaWxlIHRvIGdhaW4gdGhlIGFj
Y2Vzcz8NCg0KUmVnYXJkcw0... etc
--_000_FBA91459E616EF4B8C1CCF54B389A283030E5EMX105CL01corpemcc_
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64
PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiIHhtbG5zOm89InVy
bjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9mZmljZSIgeG1sbnM6dz0idXJuOnNjaGVt
YXMtbWljcm9zb2Z0LWNvbTpvZmZpY2U6d29yZCIgeG1sbnM6bT0iaHR0cDovL3NjaGVtYXMubWlj
cm9zb2Z0LmNvbS9vZmZpY2UvMjAwNC8xMi9vbW1sIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv
VFIvUkVDLWh0bWw0MCI+DQo8aGVhZD4NCjxtZXRhIGh... etc
--_000_FBA91459E616EF4B8C1CCF54B389A283030E5EMX105CL01corpemcc_--
There is a PHP module called mailparse which can do the heavy lifting for you.
On top of that, check out this wrapper which will make it more easy to access the functions of the library.
We use the php module in our stack at mailparser.io and it works very reliable.
I found this library which does most of what I want (I made a few small customisations).
https://github.com/CaTzil/emailParser

Images in mail can't open image

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

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.

Plaintext/HTML Email doesn't work in mac mail client

The code below is the code i am using. It works fine in thunderbird but not in mac mail client (and i assume anything made by microsoft. I currently do not have access to this to test it in). Much as i am aware of the idiosyncrasies of the various mail clients, I am flummoxed by this! It's fairly self explanatory but i am trying to send plain text and html emails to increase the readership. Any help would be much appreciated.
EDIT
I should have clarified that the contents get sent regardless but in thunderbird it displays the message correctly, but in mac mail client you get the entire thing from the first PHP-alt to the last PHP
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Rather than try and roll your own mailer, try e.g. PHPMailer. It has very good support for multipart/alternative. It's much easier to integrate this than to roll your own solution. I've been there - after working endlessly around strange MIME problems, I've dropped my hand-made mailer, switched to this, and focused on other things in the time I've spared.
In other words, don't reinvent the wheel. Although doing it yourself can be a good challenge and you'll learn a lot during the process, if you just want it to work, these guys have dealt with the complexity for you.
You're not using output buffering correctly - see man page for ob_end_clean to see that it doesn't return the captured output, you need ob_get_contents for that:
$message =ob_get_contents();
ob_end_clean();

Categories