8

域名邮件转发

 2 years ago
source link: https://sskaje.me/2021/11/01/self-hosted-domain-mail-forwarding-with-webhook/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

域名邮件转发


最早 QQ 邮箱有域名邮箱的功能,2020年下架了,新域名无法添加,只能使用QQ企业邮箱。但是QQ企业邮箱免费版不支持多域名,而且用起来并不方便。

有很多域名注册商提供免费的邮件转发服务,可以设置单条或者catch-all 的域名邮件转发,但是我看到过的几个(dynadot ,porkbun),设置的前提是 NS 要留在这里,但是 cloudflare 的免费版要求托管 NS 。

再有,forwardmx,forwardemail 等都有免费版本,但是配置起来特别麻烦,甚至某一家还会要求客户把目标邮箱放在 TXT 记录里。

所以,推荐 mailway,可以自己搭建也可以使用官方提供的服务。

我这里用了self-host 的方式,把转发服务跑在自己的服务器上。软件功能比较简单,按官方文档要求,安装好程序之后,在 /etc/mailway/domain.d 里配置域名就可以了。参考示例如下。

root@localhost:/etc/mailway/domain.d# more sskaje.me.yaml 
rules:
  - match:
      - type: all
    action:
      - type: forward
        value:
            - [email protected]

剩下的就是配置 MX 记录。按提示操作,配置 MX 和 TXT。

写这个blog的原因除了 mailway 本身方便好用,可以替代那些免费或者收费的服务。还有一个原因是它支持 webhooks,可以把邮件调用接口入库。参考配置如下

root@localhost:/etc/mailway/domain.d# more mail.domain.com.yaml 
rules:
  - match:
      - type: all
    action:
      - type: webhook
        value:
           - http://mailapi.server.domain:81/index.php
           - SECRET

示例的PHP代码

<?php
/*
 * mailway webhook example
 *
 * @author sskaje
 */

$DEBUG = false;
$server_domain = 'fwd.sskaje.me';
$maildb_server = 'http://127.0.0.1:8081';

if ($DEBUG) {
    $content = '';
    foreach (getallheaders() as $name => $value) {
        $content .= "$name: $value\n";
    }

    $content .= "\n";
    $content .= file_get_contents('php://input');

    file_put_contents('/tmp/mailapi.debug.log', $content);
}


# TODO: HMAC-SHA256 verify signature, but mailway does not provide a valid signature right now.

$body = file_get_contents('php://input');
$json = json_decode($body, true);

# TODO: verify dkim

$from = $body['headers']['From'];
$to = $body['headers']['To'];
$subject = $body['headers']['Subject'];

#
# Replace with mail db service because it's hardcoded in `webhooks/server.go`
#
# ```
# bodyUrl := fmt.Sprintf("https://%s/db/email/%s?token=%s",
#                        config.CurrConfig.InstanceHostname, id, bodyToken)
# ```
#
$bodyURL = str_replace('https://' . $server_domain, $maildb_server, $body['bodyURL']);


# ....

https://docs.mailway.app/self-host/configure-local-routing-rules/

https://docs.mailway.app/webhooks/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK