Class: Viewpoint::EWS::Message

Inherits:
Item
  • Object
show all
Defined in:
lib/model/message.rb

Direct Known Subclasses

CalendarItem

Constant Summary

Constants included from ItemFieldUriMap

ItemFieldUriMap::FIELD_URIS

Instance Attribute Summary

Attributes inherited from Item

#change_key, #item_id, #parent_folder_id

Attributes included from Model

#ews_methods, #ews_methods_undef

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Item

add_attachments, #attachments, #clear_updates!, #copy, #deepen!, #delete!, get_item, #mark_read!, #mark_unread!, #move!, #parent_folder, #recycle!, #save!, #text_only=, #text_only?, #update!, #update_attribs, #update_attribs!

Constructor Details

#initialize(ews_item, opts = {}) ⇒ Message

Initialize an Exchange Web Services item of type Message



66
67
68
# File 'lib/model/message.rb', line 66

def initialize(ews_item, opts={})
  super(ews_item, opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Viewpoint::EWS::Item

Class Method Details

.send(subject, body, to_recipients, cc_recipients = [], bcc_recipients = [], file_attachments = nil, draft = false) ⇒ Message, true

Send an E-mail message

Parameters:

  • subject (String)

    The message subject

  • body (String)

    The message body

  • to_recipients (Array)

    An array of e-mail addresses to send to

  • cc_recipients (Array) (defaults to: [])

    An array of e-mail addresses to send to

  • bcc_recipients (Array) (defaults to: [])

    An array of e-mail addresses to send to

  • file_attachments (Array<File>) (defaults to: nil)

    An array of File objects to read data from that will attach to this message.

Returns:

  • (Message, true)

    Returns true if the message is sent, if draft is true it will return the Message object or it raises an error with a message stating why the e-mail could not be sent.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/model/message.rb', line 34

def self.send(subject, body, to_recipients, cc_recipients=[], bcc_recipients=[], file_attachments=nil, draft=false)
  item = {}
  item[:subject] = {:text => subject}
  item[:body] = {:text => body, :body_type => 'Text'} unless body.nil?
  to_recipients.each do |a|
    item[:to_recipients] = [] unless item[:to_recipients].is_a?(Array)
    item[:to_recipients] << {:mailbox => {:email_address => {:text => a}}}
  end
  cc_recipients.each do |a|
    item[:cc_recipients] = [] unless item[:cc_recipients].is_a?(Array)
    item[:cc_recipients] << {:mailbox => {:email_address => {:text => a}}}
  end unless cc_recipients.nil?
  bcc_recipients.each do |a|
    item[:bcc_recipients] = [] unless item[:bcc_recipients].is_a?(Array)
    item[:bcc_recipients] << {:mailbox => {:email_address => {:text => a}}}
  end unless bcc_recipients.nil?
  
  conn = Viewpoint::EWS::EWS.instance
  resp = conn.ews.create_message_item(:drafts, item, 'SaveOnly')
  (resp.status == 'Success') || (raise EwsError, "Could not send message. #{resp.code}: #{resp.message}")
  msg_key = resp.items.first.keys.first
  msg_id = resp.items.first[msg_key][:item_id]
  msg_id = add_attachments(msg_id, file_attachments) unless (file_attachments.nil? || file_attachments.empty?)
  if !draft
    resp = conn.ews.send_item([msg_id])
    (resp.status == 'Success') || (raise EwsError, "Could not send message. #{resp.code}: #{resp.message}")
  else
    self.new({:item_id => msg_id})
  end
end

Instance Method Details

#headersObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/model/message.rb', line 70

def headers
  deepen! if @shallow
  return @headers if defined?(@headers) && !@headers.empty?
  return nil unless @ews_item.has_key?(:internet_message_headers)
  @headers = {}
  # @todo When ruby 1.9 becomes more pervasive the Enumerator#each_with_object
  #@headers ||= @ews_item[:internet_message_headers][:internet_message_header].each_with_object({}) do |h,obj|
  @ews_item[:internet_message_headers][:internet_message_header].each do |h|
    @headers[h[:header_name]] = h[:text]
  end
  @headers
end

#to_mailObject

This creates an object of type Mail (mail gem) and allows us to manipulate and output Message Items in standards compliant ways.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/model/message.rb', line 87

def to_mail
  mail = Mail.new
  unless(headers.nil?)
    mail.received  headers['Received']
    mail.content_type headers['Content-Type']
    mail.content_transfer_encoding headers['Content-Transfer-Encoding']
  end
  mail.date date_time_sent unless date_time_sent.nil?
  mail.message_id internet_message_id unless internet_message_id.nil?
  mail.in_reply_to in_reply_to unless in_reply_to.nil?
  mail.references references unless references.nil?
  mail.subject subject unless subject.nil?
  mail.return_path = sender.email_address unless(sender.nil? || ! sender.respond_to?(:email_address))
  mail.to to_recipients.map {|r| r.email_address if r.respond_to?(:email_address) } unless to_recipients.nil?
  mail.cc cc_recipients.map {|r| r.email_address if r.respond_to?(:email_address) } unless cc_recipients.nil?
  mail.from from.email_address unless(from.nil? || ! from.respond_to?(:email_address))
  # Because the mail gem does not pass an object to the block there are some issues with using self
  msg = self
  if(body_type == "HTML")
    mail.html_part do
      body msg.body
    end
    mail.text_part do
      body Nokogiri::HTML(msg.body).content
    end
  else
    mail.text_part do
      body msg.body
    end
  end

  # add attachments
  if(self.has_attachments?)
    self.attachments.each do |att|
      if(att.is_a?(FileAttachment))
        mail.attachments[att.file_name] = Base64.decode64(att.content)
      end
    end
  end
  mail
end