Skip to content

Instantly share code, notes, and snippets.

@jordangraft
Created April 24, 2016 19:40
Show Gist options
  • Save jordangraft/058250eddf8c5f6a635e43e97f534a5d to your computer and use it in GitHub Desktop.
Save jordangraft/058250eddf8c5f6a635e43e97f534a5d to your computer and use it in GitHub Desktop.
class Email < ActiveRecord::Base
def self.process_incoming_email(message_id)
obj = AWS::S3.new.buckets['my-email-bucket'].objects[message_id]
contents = obj.read
from = contents.match(/(?<=From: )(.*?)(?=\n)/).try(:to_s)
to = contents.match(/(?<=To: )(.*?)(?=\n)/).try(:to_s)
subject = contents.match(/(?<=Subject: )(.*?)(?=\n)/).try(:to_s)
body = contents.match(/(?<=Content-Type: text\/html; charset\=UTF-8)(.*?)(?=--)/m).try(:to_s)
self.create(
from: from,
to: to,
subject: subject,
body: body
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment