PI Review Email Service
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:
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.Receiving user emails (
[email protected]
). Users contact us for various reasons, such as adding universities, issues activating accounts, suggestions, etc.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)
- Step 1. User sends an email to
[email protected]
- Step 2. Cloudflare routes this email to
[email protected]
- Step 3. Mailgun forwards this email to the admin’s personal Gmail
[email protected]
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):
- The user sends a password reset request.
- The Flask APP generates Password Reset Tokens.
- The Flask APP sends the password reset email (with a link containing the token).
- 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.
Add domain, i.e.
mail.pi-review.com
Follow these steps to verify your domain (go to Cloudflare and add DNS records)
Add new SMTP users
[email protected]
and[email protected]
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.
- Enter Receiving page
- Create route: Match Recipient
- Recipient:
[email protected]
- Forward:
[email protected]
- 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.
- Gmail: see all settings
- Accounts and Import
- Send mail as: Add another email address
- 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.
- Cloudflare: Add a site -> choose Free plan -> delete default records
- Porkbun: change authoritative nameservers
- Cloudflare: DNS setting add A record
- mailgun: add domain: mail.pi-review.com (see the Mailgun section)
- 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:
- Click on Email
- Set up Email Routing
- Create address:
[email protected]
send to[email protected]