# Help with IMAP and mime

**URL:** <https://www.ruby-forum.com/t/help-with-imap-and-mime/131991>\
**Category:** Ruby\
**Created:** [March 14, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/help-with-imap-and-mime/131991 "2008-03-14T21:56:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [March 14, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/help-with-imap-and-mime/131991/1 "2008-03-14T21:56:24Z")

</div>

I’m trying to create a new mime message via imap but I’m having some  
problems getting the formatting down.

I’m using net/imap and rmail to get and put data onto the imap  
server. I’d like to set the header correctly so that I can write the  
data to the server such that it understand what it is. In this case a  
calendar item. The idea being that I could then write a calendar item  
directly to my calendar on my exchange server and exchange would treat  
it as such.

—Begin Code-----  
require ‘net/imap’

require ‘rubygems’  
require ‘rmail’

imap = Net::IMAP.new(‘myserver’)

imap.login(‘username’, ‘password’)

imap.select(‘Calendar-folder’)

myAppt = RMail::Message.new  
myAppt.header.date = Time.now  
myAppt.header.from = ‘removed\_email\_address@domain.invalid’  
myAppt.header.to = ‘removed\_email\_address@domain.invalid’  
myAppt.header.subject = ‘Ruby test appointment’  
myAppt.header.add(‘Content-Type’, ‘MULTIPART/ALTERNATIVE’)  
part = RMail::Message.new  
part.header.add(‘Content-Type’, ‘TEXT/CALENDAR’, nil, ‘param’ =\>  
‘{“METHOD” =\> “REQUEST”}’)  
part.header.add(‘Content-class’, ‘urn:content-classes:appointment’)  
part.body = vCard  
myAppt.add\_part(part)

imap.append(“Calendar-folder”, RMail::Serialize.write(’’, myAppt),  
[:Seen])  
—End Code-----

## I’m particularly concerned with how to get the param set properly.

I pulled some data from the server using imap.fetch(message\_id,  
“BODY”)

This is what I would like to get the header to look like

#\<struct Net::IMAP::FetchData seqno=1, attr={“BODY”=\>#\<struct  
Net::IMAP::BodyTypeMultipart media\_type=“MULTIPART”,  
subtype=“ALTERNATIVE”, parts=[#\<struct Net::IMAP::BodyTypeText  
media\_type=“TEXT”, subtype=“CALENDAR”, param={“METHOD”=\>“REQUEST”},  
content\_id=nil, description=nil, encoding=“8BIT”, size=1279, lines=22,  
md5=nil, disposition=nil, language=nil, extension=nil\>], param=nil,  
disposition=nil, language=nil, extension=nil\>}\>

* * *

This is what I’m managing to create

#\<struct Net::IMAP::FetchData seqno=4, attr={“FLAGS”=\>[:Seen],  
“BODY”=\>#\<struct Net::IMAP::BodyTypeMultipart media\_type=“MULTIPART”,  
subtype=“ALTERNATIVE”, parts=[#\<struct Net::IMAP::BodyTypeText  
media\_type=“TEXT”, subtype=“CALENDAR”, param={“PARAM”=\>"{"},  
content\_id=nil, description=nil, encoding=“7BIT”, size=1761, lines=30,  
md5=nil, disposition=nil, language=nil, extension=nil\>], param=nil,  
disposition=nil, language=nil, extension=nil\>}\>

Thanks for any help you can give.  
CParticle

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [March 14, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/help-with-imap-and-mime/131991/2 "2008-03-14T23:15:50Z")

</div>

On Sat, Mar 15, 2008 at 7:54 AM, CParticle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using net/imap and rmail to get and put data onto the imap  
> server. I’d like to set the header correctly so that I can write the  
> data to the server such that it understand what it is. In this case a  
> calendar item. The idea being that I could then write a calendar item  
> directly to my calendar on my exchange server and exchange would treat  
> it as such.

I don’t know if this is exactly what you need, but you can do that  
with TMail with:

irb(main):001:0\> require ‘rubygems’  
irb(main):002:0\> require ‘tmail’

irb(main):003:0\> mail = TMail::Mail.new

irb(main):004:0\> mail[‘header’] = ‘Content-Type=text/calendar,  
param={“METHOD” =\> “REQUEST”}’  
=\> “Content-Type=text/calendar, param={"METHOD" =\> "REQUEST"}”  
irb(main):005:0\> puts mail.encoded  
Header: Content-Type=text/calendar, param={“METHOD” =\> “REQUEST”}

Would mean using TMail, it is heavier than RMail, both are good, YMMV.

You might also want to get onto the TMail Talk mailing list or have a  
look through the recently updated RDocs at the website;  
[http://tmail.rubyforge.org/](http://tmail.rubyforge.org/)

Mikel L.

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar
