Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send outlook calendar invite from Rails

I am trying to send a calendar invite from my Rails app. It seems to work fine in Gmail, but not in outlook, where it is sent as an attachment.

I have tried every suggestion out there on the Internet, but just cant get it to work.

class Notifications < ActionMailer::Base
  def add_interaction
    @i = i
    ical = Icalendar::Calendar.new
    e = Icalendar::Event.new
    e.start = DateTime.now.utc
    e.start.icalendar_tzid="UTC" # set timezone as "UTC"
    e.end = (DateTime.now + 1.hour).utc
    e.end.icalendar_tzid="UTC"
    e.organizer @i.interviewer_email
    e.created = DateTime.now
    e.uid = e.url = "#{HOSTNAME.to_s+'/interaction/'[email protected]_s}"
    e.summary "#{INTERACTION_TYPE[@i.itype][0]} with #{@i.company}"
    e.description <<-EOF
    #{INTERACTION_TYPE[@i.itype][0]} with #{@i.company}
    Date: #{@i.start_time.strftime('%e %b %Y')}
    Time: #{@i.start_time.strftime('%I:%M %p')}
    EOF
    ical.add_event(e)
    ical.custom_property("METHOD", "REQUEST")

    email=mail(to: "[email protected]", subject: "Interview",mime_version: "1.0", content_type:"text/calendar",body:ical.to_ical,content_disposition:"inline; filename=calendar.ics", filename:'calendar.ics')
    email.header=email.header.to_s+'Content-Class:urn: content-classes:calendarmessage'
    return email
  end
end

and it's called as

Notifications.add_interaction(Interaction.last).deliver

I also tried to construct the mail as

    email=mail(to: "[email protected],[email protected]", subject: "#{@i.bid.company.name} has suggested a time for #{INTERACTION_TYPE[@i.itype][0]}",mime_version: "1.0", content_type:"multipart/alternative",body:'')
    html_body = render_to_string("/notifications/add_interaction")
    email.add_part(
        Mail::Part.new do
            content_type "text/html"
            body html_body
        end
    )
    p = Mail::Part.new do
        content_type 'text/calendar; method=REQUEST name="calendar.ics"'
        content_disposition "inline; filename=calendar.ics"
        content_transfer_encoding "8bit"
        body ical.to_ical
    end
    p.header=p.header.to_s+'Content-Class:urn: content-classes:calendarmessage'
    email.add_part(p)

But in all cases calendar invite is delivered as an attachment in the outlook.

I noticed something strange, which could be the cause of this. When I viewed the source of email, in both cases the topmost mimetype was multipart/mixed, (instead of text/calendar the first time and multipart/alternative the second time)

Whereas when sending the email from console, it says this

Message-ID: <[email protected]>
Subject: Talent Auction has suggested a time for Introduction Call
Mime-Version: 1.0
Content-Type: text/calendar;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename=calendar.ics
filename: calendar.ics
Content-Class: urn: content-classes:calendarmessage

BEGIN:VCALENDAR
VERSION:2.0
METHOD:REQUEST
CALSCALE:GREGORIAN
PRODID:iCalendar-Ruby
BEGIN:VEVENT
CREATED:20140326T162000
DESCRIPTION:        Introduction Call with Talent Auction\n     Date: 28 Mar 2014\n 
    Time: 04:00 PM\n
DTEND:20140327T105000Z
DTSTAMP:20140326T105000Z
DTSTART:20140326T105000Z
CLASS:PUBLIC
ORGANIZER:[email protected]
SEQUENCE:0
SUMMARY:Introduction Call with Talent Auction
UID:localhost:300019
URL:localhost:300019
END:VEVENT
END:VCALENDAR

EDIT: I am using mandrill for sending emails, if that would be of any help

like image 362
nik-v Avatar asked Mar 26 '14 11:03

nik-v


1 Answers

afair outlook happily accepts events if attached in the 'right' way. cant simulate your setup right now, so all i can try to help is bit of guessing.. please try:

email = mail(to: "[email protected]", subject: "Interview", mime_version: "1.0", content_type: "text/plain", body: ical.to_ical, content_disposition: "attachment; filename='calendar.ics'")
return email

and if that fails too please try:

... content_type: "text/calendar" ...

for your other approach that would hopefully be:

p = Mail::Part.new do
    content_type "text/plain; name='calendar.ics'"
    content_disposition "attachment; filename='calendar.ics'"
    content_transfer_encoding "7bit"
    body ical.to_ical
end

and if that fails too please try

... content_type "text/calendar" ...

instead.

in addition, please keep in mind that the sender needs to be in the receiver's address book to have events added without confirmation. furthermore, there is an ical validator at http://severinghaus.org/projects/icv/. curious to get your feedback..

like image 119
jitter Avatar answered Oct 31 '22 15:10

jitter