multipart/mixed MIME creation for amazon SES on iOS - 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.

Related

MS Graph API 4MB size limit with MIME

It seems the MS REST (EWS) services are being deprecated toward the end of 2022 and moving to graph api is recommended.
All good, graph works just fine...except for the 4MB size limit on the payload!
Ok so there is this: https://learn.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http
What a mare! But anyway I am posting MIME content as below:
https://learn.microsoft.com/en-us/graph/outlook-send-mime-message
ie. I'm posting plain text content like below to graph's sendMail endpoint
From: Some One <someone#example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="XXXXboundary text"
Subject: This is a test
This is a multipart message in MIME format.
--XXXXboundary text
Content-Type: text/plain
this is the body text
--XXXXboundary text
Content-Type: text/plain;
Content-Disposition: attachment;
filename="test.txt"
this is the attachment text
--XXXXboundary text--
I don't understand how this would work with the upload-session aproach - so is it possible, and if so, how, can I send MIME content > 4MB using the graph api?

mail decode using base64 in Ruby

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

Activesync: Sendmail command with MIME Type

<?xml version="1.0" encoding="utf-8"?>
<SendMail xmlns="ComposeMail:" xmlns:airsync="AirSync">
<ClientId>34234243</ClientId>
<SaveInSentItems />
<Mime>
From:xxx#.com
To:yyy#.com
Subject:342234 MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: base64
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 234234
This is body
</Mime>
</SendMail>
I am working with SendEmail command. I am looking for way to send Mime content to server. I have tried:
Convert the above xml in wbxml and setBOdy HTTP request but server return 103 error code.
Convert the Content betweent to Base64, and append to old string like this:
<?xml version="1.0" encoding="utf-8"?>
<SendMail xmlns="ComposeMail:" xmlns:airsync="AirSync"><ClientId>34234243</ClientId>
<SaveInSentItems/>
<Mime>
text encode base 64
</Mime>
</SendMail>
And convert to wbxml, send to server and receive error code 119 mean :MessageHasNoRecipient
The message being sent contains no recipient.
Anybody help? thanks in advance
I am sure you have a blank character before the "To" keyword in your code.
Let's remove it. Your data before you encode it to base64 encoding must to look like this:
From: xxx#xxx.com
To: xxx#xxx.com
Subject: Mail Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
Test body
Best regards,
From MS documentation Mime element must be opaque BLOB https://msdn.microsoft.com/en-us/library/gg663453(v=exchg.80).aspx.
So you must write Mime data as CDATA.
<Mime>
<![CDATA[From: xxx#xxx.com
To: xxx#xxx.com
Subject: Mail Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Test body]]>
</Mime>

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.

send mail with doc file attachment

i am sending an e-mail with a doc file attachment. I am receiving the mail but with no any attachment.
PHP
$file_resume = '';
if (!empty($_FILES['attachment_file_name']['tmp_name'])) {
$file = $_FILES['attachment_file_name']['name'];
$attachment= file_get_contents($file);
$attachment= chunk_split(base64_encode($attachment));
}
$uid = md5(uniqid(time()));
$headers= "From: no-reply#edu.in"."\r\n";
$headers.= "Reply-To: no-reply#edu.in"."\r\n";
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$uid."\"\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=utf-8"."\r\n Content-Disposition: attachment; filename=\"".$attachment."\"\r\n";
$message = $_POST['person_name'];
mail($to,$subject,$message,$headers);
Html
<form id="attachment" action='mailer.php' method='POST' name="attachment" enctype="multipart/form-data">
person name: <input type="text" name="person_name" >
<br />
Attachment : <input id="attachment_file" class="field" style="height: 25px;" type="file" name="attachment_file_name" />
<input id="submit_button" type="submit" value="Send" />
</form>
If you're going to try to manually send an email with attachment(s), you need to get to know the underlying mail text packet that actually represents what you're constructing with the mail() arguments. Unless you comprehend and understand what's going on here, you'll struggle to ever get your email with attachment to send. There's too many sharp edges.
Gmail has a nifty feature on emails to view the original message packet, in plaintext. It's called Show Original. When testing your email script, use a Gmail account if you can so you can inspect the actual email plaintext packet. To Show Original, go to the top, left dropdown triggered by the down arrow next to the reply arrow of an email.
So sending a test email to myself with an image attached, we have what's below. Your mail call, essentially, has to translate into something more or less like this example.
Especially note how the headers start off (the top five headers below you will not handle, except maybe MIME-Version). At the end of that block, you have:
Content-Type: multipart/mixed; boundary=089e0118416874703004d86a5106
The part following the = is a message part boundary, which allows you to add multipart message blocks, including attachment file contents that have been encoded and given appropriate sub-part headers.
So there's:
--089e0118416874703004d86a5106
... message block(s) ...
--089e0118416874703004d86a5106--
There's actually another boundary declared, with:
Content-Type: multipart/alternative; boundary=089e0118416874702b04d86a5104
Which involves the text/plain and text/html dual message formats. This is not required, but many mail clients do it by nature. You can choose either text/plain or text/html, it's up to you and how your comment is formatted.
Next we see:
--089e0118416874703004d86a5106
Content-Type: image/jpeg; name="opinion (2).jpg"
Content-Disposition: attachment; filename="opinion (2).jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hejlmnuz0
/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAIEBAYIBggICAgICAgICAgKCgoKCgoKCgoKCgoKCgoK
... lots of lines for the encoded file block ...
W5R3W8ajLwrgUJEDGKWN2kWvO5iB7qdKwB6MwwfqVrQKcObveZxZBtQNCp0vc//Z
--089e0118416874703004d86a5106--
Note that the boundary's have a trailing -- at the end of their blocks (not each block).
Take a look below and compare that to the code that luk3thomas has in his answer. Try sending a very simple email message to yourself, and compare that to an attachment-formatted email:
mail('your#email.com', 'Simple mail test', 'Test message body content.');`
You should start to get the idea what's required to send emails with attachments included. The thing is, formatting an email packet for sending is very particular and little things can cause it to fail. So you have to pay attention.
MIME-Version: 1.0
Received: by 10.50.40.164 with HTTP; Thu, 21 Mar 2013 00:30:51 -0700 (PDT)
Date: Thu, 21 Mar 2013 02:30:51 -0500
Delivered-To: [redacted]#gmail.com
Message-ID: <CAKJE7RAH3+ZgN+86xykJrrzVaHK3waPD-a-OXbSDe3FGgcQrMw#mail.gmail.com>
Subject: Test of email with attachment for plaintext
From: Jared Farrish <[redacted]#gmail.com>
To: Jared Farrish <[redacted]#gmail.com>
Content-Type: multipart/mixed; boundary=089e0118416874703004d86a5106
--089e0118416874703004d86a5106
Content-Type: multipart/alternative; boundary=089e0118416874702b04d86a5104
--089e0118416874702b04d86a5104
Content-Type: text/plain; charset=UTF-8
This is the body of the email message.
--089e0118416874702b04d86a5104
Content-Type: text/html; charset=UTF-8
<div dir="ltr">This is the body of the email message.<br></div>
--089e0118416874702b04d86a5104--
--089e0118416874703004d86a5106
Content-Type: image/jpeg; name="opinion (2).jpg"
Content-Disposition: attachment; filename="opinion (2).jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hejlmnuz0
/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAIEBAYIBggICAgICAgICAgKCgoKCgoKCgoKCgoKCgoK
CgoKCgwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/2wBDAQIICBAQEBAQEBAgICAgIEBAQEBA
QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQED/wgARCAEsASwDASIA
AhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABgcFCAIDBAkAAf/EABsBAAMBAQEBAQAAAAAAAAAA
[-- snip many lines of base64 file contents --]
yGrjmAV5jlw7j5RFebDQu8RG2rLa/up9x/xHOU5eppGo7lm3uDTz6w5XxBYgB7ruYJFwW/2PiD9q
XjDEturp55IeLGM36+Tlz8JY6v8AXGhta9/4Ih62UvuWHuP3DfOg/wAGFg/g0mNKnahdifSKKua+
h0likOlp+yFa2FgP0rtOReVitRTIiUuJSjw6JpN4thVyhRdZ8EE/aYzn523NZeJYu7judhllHo0g
W5R3W8ajLwrgUJEDGKWN2kWvO5iB7qdKwB6MwwfqVrQKcObveZxZBtQNCp0vc//Z
--089e0118416874703004d86a5106--
You'll need to add the attachment into the body of the email message. Try something like this:
$random_hash = md5(time());
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
$headers .= "\r\nMIME-Version 1.0";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$message =
"--PHP-alt-$random_hash
Content-Type: text/plain
Dear Same,
We would like to thank you for your registration to be held on Saturday August 25, 2012 at the....
--PHP-alt-$random_hash
Content-Type: application/pdf; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-alt-$random_hash--";
#mail($to, $subject, $message, $headers);

Resources