sumeetjain / outcomes-tracker

0 stars 0 forks source link

Students should be able to bcc or fwd an email to a specified account and have their email appear in their Entry comment thread #138

Open halfghaninne opened 8 years ago

halfghaninne commented 8 years ago
halfghaninne commented 7 years ago

http://guides.rubyonrails.org/action_mailer_basics.html#receiving-emails

Receiving and parsing emails with Action Mailer can be a rather complex endeavor. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that. So, to receive emails in your Rails app you'll need to:

Implement a receive method in your mailer.

Configure your email server to forward emails from the address(es) you would like your app to receive to /path/to/app/bin/rails runner 'UserMailer.receive(STDIN.read)'.

Installed ngrok so that the application would have a public-facing point of entry, but unsure how to configure gmail to forward to something like this. Might have to be done on an admin-level?

Once a method called receive is defined in any mailer, Action Mailer will parse the raw incoming email into an email object, decode it, instantiate a new mailer, and pass the email object to the mailer receive instance method. Here's an example:

class UserMailer < ApplicationMailer
  def receive(email)
    page = Page.find_by(address: email.to.first)
    page.emails.create(
      subject: email.subject,
      body: email.body
    )

    if email.has_attachments?
      email.attachments.each do |attachment|
        page.attachments.create({
          file: attachment,
          description: email.subject
        })
      end
    end
  end
end
LeannaRM commented 7 years ago

Story 1: An OCS student is writing an email to a prospective employer. They include in bcc an email address (ot@outcomes.omahacodeschool.com?). This email will then appear as a new event in the student's entry for that employer.

Story 2: An OCS student receives an email from a prospective employer. So, they fwd the email to an email address (ot@outcomes.omahacodeschool.com?). This email will then appear as a new event in the student's entry for that employer.

LeannaRM commented 7 years ago

We plan to attempt to do this using SendGrid parsing API to send message as POST request. https://sendgrid.com/docs/API_Reference/Webhooks/inbound_email.html https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html

LeannaRM commented 7 years ago

In rails, we will make a new controller for the POST request. First, find the user (the corresponding student) by email address, this is the from email address. Then we check to see if it was a new outgoing message or a forwarded message. If it was a new outgoing message, check the to email address and match to employers for the user. If was a fwd message then can grab email from header content. The email content then becomes a new event in the appropriate entry.

LeannaRM commented 7 years ago

If can't match an entry, then needs to send autoreply back to student to manually add the email and check input email addresses.