module MailCatcher::Mail

Public Instance Methods

add_message(message) click to toggle source
# File lib/mail_catcher/mail.rb, line 52
def add_message(message)

  mail = Mail.new(message[:source])

  m = Message.create(
    sender: message[:sender],
    recipients: message[:recipients].to_json,
    subject: mail.subject,
    source: message[:source],
    type: mail.mime_type || 'text/plain',
    size: message[:source].length
  )

  #message_id = db.last_insert_row_id
  message_id = m.id
  parts = mail.all_parts
  parts = [mail] if parts.empty?
  parts.each do |part|
    body = part.body.to_s
    # Only parts have CIDs, not mail
    cid = part.cid if part.respond_to? :cid
    MessagePart.create(
      message_id: message_id,
      cid: cid,
      type: part.mime_type || 'text/plain',
      is_attachment: part.attachment? ? 1 : 0,
      filename: part.filename,
      charset: part.charset,
      body: body,
      size: body.length
    )
  end

  EventMachine.next_tick do
    message = MailCatcher::Mail.message message_id
    MailCatcher::Events::MessageAdded.push message
  end
end
delete!() click to toggle source
# File lib/mail_catcher/mail.rb, line 142
def delete!
  Message.destroy_all
end
delete_message!(message_id) click to toggle source
# File lib/mail_catcher/mail.rb, line 146
def delete_message!(message_id)
  Message.where(id: message_id).destroy_all
end
message(id) click to toggle source
# File lib/mail_catcher/mail.rb, line 95
def message(id)
  Message.find(id).attributes
end
message_attachments(id) click to toggle source
# File lib/mail_catcher/mail.rb, line 113
def message_attachments(id)
  MessagePart.where(message_id: id, is_attachment: 1).order('filename ASC').map(&:attributes)
end
message_has_html?(id) click to toggle source
# File lib/mail_catcher/mail.rb, line 99
def message_has_html?(id)
  part = MessagePart.where(message_id: id, is_attachment: 0).where("type IN ('application/xhtml+xml', 'text/html')").first
  part.present? || ['text/html', 'application/xhtml+xml'].include?(Message.find(id).type)
end
message_has_plain?(id) click to toggle source
# File lib/mail_catcher/mail.rb, line 104
def message_has_plain?(id)
  part = MessagePart.where(message_id: id, is_attachment: 0, type: 'text/plain').first
  part.present? || Message.find(id).type == 'text/plain'
end
message_part(message_id, part_id) click to toggle source
# File lib/mail_catcher/mail.rb, line 117
def message_part(message_id, part_id)
  MessagePart.where(message_id: message_id, id: part_id).first.try(:attributes)
end
message_part_cid(message_id, cid) click to toggle source
# File lib/mail_catcher/mail.rb, line 138
def message_part_cid(message_id, cid)
  MessagePart.where(message_id: message_id, cid: cid).first.try(:attributes)
end
message_part_html(message_id) click to toggle source
# File lib/mail_catcher/mail.rb, line 125
def message_part_html(message_id)
  part = message_part_type(message_id, "text/html")
  part ||= message_part_type(message_id, "application/xhtml+xml")
  part ||= begin
    message = message(message_id)
    message if message.present? and ['text/html', 'application/xhtml+xml'].include? message["type"]
  end
end
message_part_plain(message_id) click to toggle source
# File lib/mail_catcher/mail.rb, line 134
def message_part_plain(message_id)
  message_part_type message_id, "text/plain"
end
message_part_type(message_id, part_type) click to toggle source
# File lib/mail_catcher/mail.rb, line 121
def message_part_type(message_id, part_type)
  MessagePart.where(message_id: message_id, type: part_type, is_attachment: 0).first.try(:attributes)
end
message_parts(id) click to toggle source
# File lib/mail_catcher/mail.rb, line 109
def message_parts(id)
  MessagePart.where(:message_id => id).order('filename ASC').map(&:attributes)
end
messages() click to toggle source
# File lib/mail_catcher/mail.rb, line 91
def messages
  Message.all.map(&:attributes)
end