수신은 Cloudflare Email Routing 사용
답장은 Google SMTP 서버를 사용
https://community.cloudflare.com/t/solved-how-to-use-gmail-smtp-to-send-from-an-email-address-which-uses-cloudflare-email-routing/382769/3
https://support.google.com/mail/answer/22370?hl=en
https://www.youtube.com/watch?v=8I8PCM0YGKs
수신시 자동 답장은 Cloudflare Worker 사용
https://developers.cloudflare.com/email-routing/email-workers/reply-email-workers/
Cloudflare Worker에서 forward 메서드를 호출하여 자동 답장과 동시에 관리자들에게 포워딩도 할 수 있다.
https://community.cloudflare.com/t/email-worker-to-forward-to-multiple-emails/474377/4
이메일이 답장(reply)인지 여부를 판별하는 가장 일반적인 방법은 이메일 헤더를 확인하는 것입니다. 예를 들어, 이메일 헤더에 In-Reply-To나 References 항목이 포함되어 있으면 해당 메일이 이전에 보낸 메일에 대한 답장임을 알 수 있습니다.
구현 아이디어
헤더 검사
1.1. 이메일 수신 시, Cloudflare Worker가 이메일의 헤더 정보를 파싱하도록 합니다.
1.2. 만약 In-Reply-To 또는 References 헤더가 존재한다면 → 이 경우는 사용자가 답장한 것으로 판단하여 자동 회신(reply) 기능을 생략하고, 담당자에게 포워딩만 처리합니다.
1.3. 헤더가 없을 경우 → 일반 수신 메일로 판단하여 정해진 문구로 자동 회신을 보내고 동시에 담당자에게 포워딩합니다.
코드 예시 (의사코드)
3.1. 헤더 이외의 검증: 일부 경우 사용자가 수동으로 헤더 정보를 변조할 가능성도 있으므로, 필요시 제목(subject)에 “Re:” 등의 문자열도 추가로 확인할 수 있습니다.
3.2. 외부 서비스 연동: Cloudflare Worker는 직접 이메일을 송수신하지 않으므로, 실제 이메일 발송 및 포워딩은 SendGrid, Mailgun 등 외부 이메일 API와 연동하여 구현하는 것이 일반적입니다.
3.3. 에러 핸들링: 이메일 처리 중 발생할 수 있는 예외 상황에 대한 처리를 추가하는 것이 좋습니다.
이와 같이 이메일 헤더를 기반으로 답장 여부를 구분하여 처리하면, 사용자가 답장한 경우 자동 회신은 생략하고 포워딩만 하는 기능을 효과적으로 구현할 수 있습니다.
STEP 1. Make sure you have 2FA enabled…
For this method to work, you need to have two factor authentification enabled for your Google account. If it’s not, follow the link below and set it up → Enable 2FA in your Google account…
STEP 2. Also in your Google account, create an App Password for Mail → Create an App Password… (You will copy and use this password later along with your Gmail address in the Google SMTP server settings “Add another email address” form)…
Select “Mail” for the app. Select your computer as the device. Hit “Generate”. Copy and keep the generated password for later in this process.
STEP 3. Then, back in Gmail, add your Cloudflare-routed email address…
In Gmail go to Settings → Accounts → Send mail as. Under that section, click on Add another email address and fill in the first form with your name and your Cloudflare-routed email address. Untick “Treat as an alias” and click Next Step >>
STEP 4. Fill in the next form…
SMTP Server: smtp.gmail.com Port: 587
Username: your gmail address (incl. @gmail.com)
Password is the App Password you generated in Step 2.
Leave TLS enabled
Click Add Account >>
You will receive an email from Gmail asking you to confirm ownership with a code. Enter the code in the dialog (or click the link provided in the confirmation email), and you are all set!
Now you can select your new email address in the list when you compose a new message in Gmail. And, when reply to an email you received at your new address, the new address should be pre-filled in the From: field.
v=spf1 include:_spf.mx.cloudflare.net include:_spf.google.com ~all
The DNS records for your domain on Cloudflare (the one for which you have set up the Email Routing) – that domain – should have a TXT record with the SPF policy. It looks like this (where in this example I have Email Routing set up for my domain on Cloudflare, denverit.com):
In order to authorize the Gmail SMTP Servers, so that you can send emails from your Gmail email account using your Cloudflare-routed email addresses, you need to replace the the Content in this DNS TXT record entry with…
v=spf1 include:_spf.google.com include:_spf.mx.cloudflare.net ~all
Then click the Save button.