How to create a record in the database base on email received to my site?

Viewed 106

I have Laravel web application for my portfolio. https://www.bunlongheng.com

Under the portfolio section, you will see this

enter image description here

Right now, I have a CRUD for admin only to create them, but a thought come to my mind ...

I would like to create this automated email to just email myself to create a new record to the database.

Example

Anyone email to : portfolio@bunlongheng.com

Subject: Title of portfolio Body:

  1. Task 1
  2. Task 2
  3. Task 3

With attachments : 2 images (example)

Goal:

I want to accept and parse that email, and create a new portfolio record

  • Subject = Title
  • Body = Description
  • Attachments = Portfolio Images

enter image description here

How do I start ?

1 Answers

This is a too-large topic for a Stack Overflow answer.

Some starting places:

  1. You need a mailbox accessible via IMAP or POP3. (SMTP is for outbound messages. You could implement an SMTP server to handle incoming messages, but doing that correctly is a really vast programming and network engineering job and not worth the trouble.)

    EDIT You need to create that emailbox on some email server; it doesn't matter which one. You can use gmail or outlook or your own domain's email. You could make myappinbox@code-8.com (if code-8.com is your domain obvs) or someRandomThingOrOther@gmail.com. Then you rig your software to talk to your service's IMAP or POP3 service. If it's a commercial service you can ask them to provide the IMAP or POP3 connection details. (It's a normal thing; they probably have a help page on it.)

    One little trick that may help you. If the mailbox is inbox@code-8.com you can send messages to inbox+tag@code-8.com and inbox+anotherTag@code-8.com, putting whatever you want after the +, and they all go to the same mailbox. So you can set up categories of incoming simply by telling different senders to send to mailboxes with different tags.

  2. You need a php program that will poll (visit) that mailbox looking for new mail every so often. The way incoming email works, you need to pull it from a mailbox.

  3. Your php program should use the php IMAP / POP3 APIs. The API is called IMAP, but it handles both protocols.

  4. It will use those APIs to fetch and decode the messages.

  5. Then you can write php code to parse the format you expect inside the messages and handle it the way you want.

There are also some free open-source Laravel packages, like this one, to do this task. It may be best to use one of those. Getting IMAP or POP3 code exactly right is tricky, especially in the area of deleting incoming messages when you're done with them. You'll take on a messy testing burden if you do it yourself. (Ask me how I know that sometime.)

Related