Contents
  1. 1. Requirements
  2. 2. Process Overview
  3. 3. Specific Implementation
    1. 3.1. Porkbun Email Hosting
    2. 3.2. Flask Mail
    3. 3.3. Mailgun
      1. 3.3.1. Sending Emails with Flask APP
      2. 3.3.2. Forwarding Emails to Gmail
      3. 3.3.3. Sending Emails with Gmail
    4. 3.4. Cloudflare
      1. 3.4.1. DNS Settings
      2. 3.4.2. Email Routing

This content was automatically generated by gpt-4o-mini (No human review). The original post is in Chinese.

This blog post discusses how the PI Review email service is set up. This post is more of a learning note rather than a technical tutorial. Therefore, some parts may not be clearly explained or may lack detail; please forgive any shortcomings.

Requirements

Currently, the PI Review website uses three email addresses for external communication, with the following three scenarios for each email:

  1. Automated emails ([email protected]). When a user registers an account, the system automatically sends an email to verify their registration email. When a user forgets their password, the system automatically sends an email to reset the password.

  2. Receiving user emails ([email protected]). Users contact us for various reasons, such as adding universities, issues activating accounts, suggestions, etc.

  3. Admin replies to user emails ([email protected]).

Process Overview

I created a diagram to summarize the current Email Service for PI Review.

User sends an email to PI Review (red line)

User registration/password recovery (blue line)

  • Step 1. User signs up for an account
  • Step 2. Flask app generates email content with a token
  • Step 3. Mailgun sends the email to the user using [email protected]

Admin replies to user emails (green line)

  • Step 1. Admin uses her personal Gmail account to reply to the user’s email
  • Step 2. Mailgun sends the email to the user using [email protected]

Specific Implementation

Porkbun Email Hosting

Let’s start with the simplest part. Initially, we used the email service (Email Hosting) provided by the domain site porkbun.com, which allows us to receive and manually send emails. Similar to Gmail, it has a web interface webmail.porkbun.com, where two administrators can share a single account and password to log in. The cost is $24 per year, as shown in the image below.

As shown in the image, Porkbun also offers a free Email Forwarding service, which can forward emails sent to [email protected] to your designated inbox, but it is not possible to send emails from [email protected], which feels a bit unprofessional.

Starting in May of this year, we no longer use Porkbun Email Hosting. The reason is that we found it inconvenient to log into the Porkbun webmail page to check emails; we couldn’t manage to check emails daily, leading to some emails not being replied to in a timely manner. Therefore, after May of this year, we stopped using Porkbun’s built-in email hosting service (see the Cloudflare section for more details).

Flask Mail

Regarding how to automatically send emails to users in a Flask App, I referred to this blog post, which I followed last year to implement it.

The Flask Mega-Tutorial Part X: Email Support

This blog discusses how to implement the password reset feature, specifically in four steps (the account activation feature is very similar):

  1. The user sends a password reset request.
  2. The Flask APP generates Password Reset Tokens.
  3. The Flask APP sends the password reset email (with a link containing the token).
  4. The user clicks the link in the email to reset their password.

When I was developing the Flask backend logic, I tested it using Gmail’s Email Server. After I completed this part of the functionality, I switched to using the email [email protected] (see the Mailgun section for details). I may have also referred to another blog post.

The Flask Mega-Tutorial Part VII: Error Handling

Mailgun

Currently, both @mail.pi-review.com email accounts are managed by mailgun.

Sending Emails with Flask APP

The configuration for sending emails using Mailgun involves the following steps. First, log in to Mailgun.

  1. Add domain, i.e. mail.pi-review.com

  2. Follow these steps to verify your domain (go to Cloudflare and add DNS records)

  3. Add new SMTP users [email protected] and [email protected]

  4. Add SMTP settings information on the PI-Review server (config.json)

Once these steps are completed, Mailgun can replace the Gmail Email Server in the Flask Mail process to automatically send emails to users (such as verification emails, password resets, etc.).

Forwarding Emails to Gmail

To forward emails received at [email protected] to the administrator’s personal Gmail account, you need to set it up in Mailgun.

  1. Enter Receiving page
  2. Create route: Match Recipient
  3. Recipient: [email protected]
  4. Forward: [email protected]
  5. Save route

This resolves the initial inconvenience of checking emails when using Porkbun. The administrator only needs to check their Gmail account daily to see if anyone has emailed PI Review.

Sending Emails with Gmail

The administrator’s Gmail should not only be able to receive emails but also ideally respond to emails as PI Review (instead of displaying a personal email address). Therefore, some settings need to be configured in the Gmail account.

  1. Gmail: see all settings
  2. Accounts and Import
  3. Send mail as: Add another email address
  4. Add SMTP credentials (this is in the part about sending emails in the Flask APP)

After completing the above steps, the administrator can use their Gmail to reply to PI Review emails and choose to reply using [email protected].

Cloudflare

DNS Settings

Here are the steps to migrate the DNS server from Porkbun to Cloudflare.

  1. Cloudflare: Add a site -> choose Free plan -> delete default records
  2. Porkbun: change authoritative nameservers
  3. Cloudflare: DNS setting add A record
  4. mailgun: add domain: mail.pi-review.com (see the Mailgun section)
  5. Cloudflare: DNS setting add more records

Email Routing

The last step is to forward emails sent to [email protected] to [email protected]. Why is this necessary? Because initially, we used Porkbun Email Hosting to send and receive emails with [email protected]. However, Porkbun cannot automatically send emails, so when we were using Mailgun, we opted for [email protected] instead of [email protected], because pi-review.com was occupied by Porkbun. So this is essentially a legacy issue; now that we are no longer using Porkbun email hosting, we still want users to be able to send emails to the [email protected] address.

The specific steps are in Cloudflare:

  1. Click on Email
  2. Set up Email Routing
  3. Create address: [email protected] send to [email protected]
Contents
  1. 1. Requirements
  2. 2. Process Overview
  3. 3. Specific Implementation
    1. 3.1. Porkbun Email Hosting
    2. 3.2. Flask Mail
    3. 3.3. Mailgun
      1. 3.3.1. Sending Emails with Flask APP
      2. 3.3.2. Forwarding Emails to Gmail
      3. 3.3.3. Sending Emails with Gmail
    4. 3.4. Cloudflare
      1. 3.4.1. DNS Settings
      2. 3.4.2. Email Routing