mail decode using base64 in Ruby - ruby-on-rails

I am trying to implement decode mail
--001a11419e22edc789053c4ccb1b
Content-Type: text/plain; charset=UTF-8
This is known to all
--001a11419e22edc789053c4ccb1b
Content-Type: text/html; charset=UTF-8
This is known to all
--001a11419e22edc789053c4ccb1b--
how can i decode the html formate in UTF-8
i am also trying decode
require 'base64'
def decode(x)
y = Base64.decode64(x)
d = y.force_encoding('UTF-8')
return d
but error is
invalid byte sequence in UTF-8
when i use only Base64.decode64(x)
then it showing the output..
�����uӟ6۽�ٽ9��xݯ= ��z{Sʗ�{�V���Z�ǭQ1|~��j
what is the reason behind it?

You can't parse an email only with Base64.decode64. Only a small part of some emails is base64 encoded. You have to extract this parts and after this you can use Base64.decode64 to decode the extracted parts.
If you want to parse an email you can do it "by hand" or use existing gem's like for instance https://github.com/mikel/mail

Related

Attaching a File while sending mail in COBOL

I have a COBOL batch program where I am able to send mail notification to an ID once my job is complete however, I also want to add an attachment in the mail of the processed file.
The following code attaches another mail as an attachment.
HELO SANTAANA
MAIL FROM:<abc#somting.com>
RCPT TO:<abc#something.com>
DATA
From: LandT P2P - LO <abc#something.com>
To: abc#something.com
Subject: File processed - Price_Change_10-27-15 07-08-44
MIME-VERSION: 1.0
CONTENT-TYPE: MULTIPART/MIXED;name="Price_Change_10-27-15.csv"
CONTENT-DISPOSITION: ATTACHMENT;
FILENAME="Price_Change_10-27-15 07-08-44.csv"
Note: I have also tried using SMTP and still does not work
Here is the sample of the mail i receive on running this code.
If you are generating the text of the email from within your Cobol program, which it sounds like, you would need to append another section, specify the Content-Type and Content-Disposition, filename and encoding, and then follow it with the properly encoded data, similar to this:
Content-Type: application/xml; name="Price_Change_10-27-15 07-08-44"
Content-Disposition: attachment; filename="Price_Change_10-27-15 07-08-44"
Content-Transfer-Encoding: base64
UEsDBBQABgAIAAAAIQDfrfoCnAEAAEcGAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
... and so on ...
I did notice that you had the content of those tags in upper case, that might be a problem. RFC1341 specifies those as "multipart/mixed" and "attachment" and so on. It is possible that your lack of mixed case is messing you up.
CONTENT-TYPE: MULTIPART/MIXED;name="Price_Change_10-27-15.csv"
CONTENT-DISPOSITION: ATTACHMENT;
FILENAME
Even easier than generating your own, have you looked into the excellent XMITIP package by Lyonel B. Dyck, it manages all that for you and you write a few config arms to control it, and you can easily call it from a Cobol program just like any other Rexx. Or you could add it to the end of your job stream as a separate step and make the task really easy.

Mailing attachments using the ruby mail gem

I am using the Mail gem to send out emails with attachments from my application. Specifically, I have identical code living on my devbox and a server machine which is used to report a pdf file. However, pdfs sent as attachments from my devbox show up in Thunderbird and other clients fine, whereas attachments from the server show up only in SOME (not thunderbird) mail clients fine. I've opened the email source to compare the two and try to get some insight into the issue:
Devbox:
--_av-F3fhmdkLZDU59RCz4nm2gw--
--_av-ycGAp26idM_6kl-0lMcYmw
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="report.pdf"
JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9MZW5ndGggNjc4L0ZpbHRlci9G
bGF0ZURlY29kZT4+c3RyZWFtCnic7Zhdb9owFIZ7S37FuZm0SZA53wkSF1Bg
...
MzBhN2RlZDE0ZmJkZDMwNzU1MzU2Y2ZhPjxlYTM0OTMzOTEwN2RmYzI1NTJh
NDE5NTJmNDhmN2MwMD5dL0luZm8gMzMgMCBSL1NpemUgMzQ+PgpzdGFydHhy
ZWYKNDE2NjQKJSVFT0YK
--_av-ycGAp26idM_6kl-0lMcYmw--
Server:
----==_mimepart_561d49a469c4d_33ee3fea57ad63b8624ae
Content-Type: application/pdf;
charset=UTF-8;
filename=report.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=report.pdf
Content-ID: <561d49a46bc9f_33ee3fea57ad63b862596#autopilot-demo-east-web-1.mail>
JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9MZW5ndGggNjgwL0ZpbHRlci9G
bGF0ZURlY29kZT4+c3RyZWFtCnic7Zhdb9owFIZ7S37FuZm0SSVznJAPJC4o
oWs7qrVdql500xTAgNt8QGKg3a+fnQ5RbR3ttnYziQkR9jk+Pu95bEcKM+0V
nPjWcQ8WJMtpmjTBdI3RjQEnWcp9R/77tcfQbR19MW1u//Bxbe7RZH4Dpo51
..
N2I0MjdlNTE4ZTNmZGNmOGE5YzY1NTRjMzc+PGE1N2MzNmNkNTE0ZTQ1Mzlk
ZjY3OTU2ODZkY2M2ZDEzPl0vSW5mbyAzMyAwIFIvU2l6ZSAzND4+CnN0YXJ0
eHJlZgo0MTY2NgolJUVPRgo=
----==_mimepart_561d49a469c4d_33ee3fea57ad63b8624ae--
The simple line adding the attachment (Rails):
email.add_file filename: "sample.pdf", content: File.read("/public/sample.pdf")
Off the bat, the section identifiers are different (_av vs mimepart) and the way each attachment is encoded is also different after a few lines. My question is, what could be causing certain clients not to respect the "mimepart" sections as attachments? Beyond that, the next question would be why the same code would generate different output, but if I know the underlying issue I may be able to solve that myself. Thanks!

multipart/mixed MIME creation for amazon SES on iOS

Im constructing a MIME essentially from scratch to send emails with attachments using Amazon's SES SDK for iOS. By producing the following MIME and encoding it into a NSData object I am able to receive an email with an attached email:
From: me <from#example.com>
To: to#example.com
Subject: "example subject"
MIME-Version: 1.0
Content-Type: image/png
Content-Disposition: attachment; filename="img.png"
Content-Transfer-Encoding: base64
[giant string of base64 encoded png file omitted for brevity]
However I want to also have a plain text message in the body of the email, but I haven't been able to get my multipart/mixed message with the following format to be parsed correctly. It sends as an email with a "noname" attachment containing all the text after the first boundary.
From: me <from#example.com>
To: to#example.com
Subject: "example subject"
MIME-Version: 1.0
Content-Type: multitype/mixed; boundary="boundary--boundary--boundary"
--boundary--boundary--boundary
Content-Type: text/plain
example plain text
--boundary--boundary--boundary
Content-Type: image/png
Content-Disposition: attachment; filename="img.png"
Content-Transfer-Encoding: base64
[giant string of base64 encoded png file omitted for brevity]
--boundary--boundary--boundary--
Does anyone see something wrong with how I'm formatting the second MIME?
Thanks for your help.
You're using the wrong Content-Type. The correct MIME type for a message with this structure is multipart/mixed, not multitype/mixed.

MIME mail is not showing some pdf and .doc attachments

Hello this is my first post on this site.
Now i encountered a strange problem with MIME. I am working on writing emlx mail files.
(objective c, cocoa)
now i have no problem with showing images in mails. the problems is that half of my .doc and pdf attachments don't show in the message.
An formatting example.
MessageLength in Bytes(i.e 8556)
From: some#email
To: some#email2
Subject: mailsubject
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="somerandomgeneratedstring"
--somerandomgeneratedstring
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Body Text(not html body)
--somerandomgeneratedstring
Content-Type: application/msword; name="somefile.doc"
Content-Disposition: attachment; filename="somefile.doc"
Content-Transfer-Encoding: base64
[base64 encoded data]
--somerandomgeneratedstring--
PlistStructure
The text body is always displayed properly. The problem is that some text file attachments like .doc and pdf are not displayed, i run the same code for multiple text files and some work and some do not i have no clue why. all pictures work.
The thing i don't get is if i use the same data that wont work for MIME and write is as a file to my desktop the file is displayed properly.
Is there something i am missing ? Is there something i have to take care of when encoding such files in base64 ? if so why do some files work and others dont ?(source is in binary format, i encode is using nsdata base64encoding in Xcode)
I have gone trough the mime documentation multiple times and i cant seem so find a solution.
Can anyone please give me some tips or suggestions ?
EDIT: omg i forgot about this, The problem was that i had a problem with the first line, length in bytes, after i fixed that it worked. Sorry for wasting time :(
You need to get rid of the blank line between the top-level MIME-Version and Content-Type headers. MIME headers and body are separated by a blank line, so your Content-Type is effectively ignored as a header and treated as body content instead.
From: some#email
To: some#email2
Subject: mailsubject
MIME-Version: 1.0
<-- get rid of this blank line
Content-Type: multipart/mixed; boundary="somerandomgeneratedstring"
Update: since this problem is not a factor anymore, the only remaining factor you have not shown yet is the actual base64 data. It is likely malformed in some way that is preventing proper decoding. Everything else you have shown is accurate.

English Email Displays Chinese-like Characters

I'm using Postal to send emails with an HTML and Text portion.
When the email is sent to Gmail, it is displayed correctly. However, when it is displayed in at least two other email systems (Mail Enable's webmail interface, and an unknown system at a client), the text is rendered as something similar to Chinese. When the client forwards the email back to a Gmail account, the "Chinese" rendering is also visible.
Example email generated:
X-Sender: no-reply#thecompany.com
X-Receiver: therecipient#thecompany.com
MIME-Version: 1.0
From: no-reply#thecompany.com
To: therecipient#thecompany.com
Date: 17 Apr 2013 22:11:25 -0700
Subject: Some Subject
Content-Type: multipart/alternative;
boundary=--boundary_0_83808b99-ef32-4f47-8835-ba4a435a2141
----boundary_0_83808b99-ef32-4f47-8835-ba4a435a2141
Content-Type: text/plain; charset=utf-16
Content-Transfer-Encoding: base64
MIME ENCODED CONTENTS HERE==
----boundary_0_83808b99-ef32-4f47-8835-ba4a435a2141
Content-Type: text/html; charset=utf-16
Content-Transfer-Encoding: base64
MIME ENCODED CONTENTS HERE=
----boundary_0_83808b99-ef32-4f47-8835-ba4a435a2141--
Clearly there is an encoding issue that Gmail somehow sorts out but other email servers do not, but what exactly is the issue?
The charset is specified as utf-16. Is does Postal (or the MVC engine) in fact generate utf-8 output? How can I control the encoding of the output and/or the charset specified in the email header?
The character encoding can be explicitly set to utf-8 by adding the headers
Content-Type: text/plain; charset=utf-8
and
Content-Type: text/html; charset=utf-8
See this article for more information.
NOTE: There is a typo in the article. The text/plain line is missing a semicolon. That is corrected in the example above.

Resources