使用 Mailgun 发送电子邮件


Google Cloud 与 Mailgun 合作,提供具有程序化 API、日志保留、电子邮件个性化、分析和电子邮件验证的电子邮件服务。

以下说明介绍如何使用 Postfix 将 Mailgun 配置为电子邮件中继。

准备工作

  1. Google Cloud Marketplace 上注册并创建新的 Mailgun 账号。

  2. 获取您的凭据。根据本说明的要求,您必须知道自己的 Mailgun SMTP 用户名、密码和主机名。请于 Mailgun 控制面板的网域部分下获取您的用户名和密码。

    SMTP 主机名是 smtp.mailgun.orgsmtp.eu.mailgun.org,具体取决于 Mailgun 中网域的配置方式。

  3. 配置防火墙规则以允许 TCP 端口 2525 上传出的流量。

使用 Postfix 将 Mailgun 配置为邮件中继

将 Mailgun 配置为邮件中继可让 Postfix 邮件传输代理转发远程传送的电子邮件。

  1. 使用 SSH 连接到您的实例。

    gcloud compute ssh [INSTANCE_NAME]
    

    其中,[INSTANCE_NAME] 是您要从中发送电子邮件的虚拟机实例的名称。

  2. 成为超级用户并设置一个安全的 umask。

    user@test-instance:~$ sudo su -
    
    root@test-instance:~# umask 077
    
  3. 安装 Postfix 邮件传输代理。

    Debian

    root@test-instance:~# apt update && apt -y install postfix libsasl2-modules
    

    CentOS

    root@test-instance:~# yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y
    

  4. 出现提示时,选择 Local Only 配置并接受默认的域名选项。

  5. 修改 Postfix 配置选项。Postfix 配置选项在 main.cf 文件中设置。使用您所选的文本编辑器打开该文件。

    root@test-instance:~# vi /etc/postfix/main.cf
    
  6. 如果文件中存在以下几行内容,请将其注释掉。

    # default_transport = error
    # relay_transport = error
    
  7. 要添加 Mailgun SMTP 服务,请将以下这行代码添加到文件末尾。

    relayhost = [smtp.mailgun.org]:2525
    
  8. 如需强制支持 SSL/TLS 并为这些请求配置 SMTP 身份验证,请将下面几行代码添加到文件末尾。由一个简单的访问和安全层 (SASL) 模块负责处理 Postfix 配置中的身份验证。

    smtp_tls_security_level = encrypt
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    
  9. 保存更改并关闭该文件。

  10. 生成 SASL 密码映射。

    1. 创建一个可用于标准输入的新密码文件。

      root@test-instance:~# cat > /etc/postfix/sasl_passwd << EOF
      
    2. 在提示符处,输入服务详细信息,并将 YOUR_SMTP_LOGINYOUR_SMTP_PASSWORD 替换为您的凭据。请参阅 Mailgun 帮助,以获取有关如何查看或更改每个网域的凭据的说明。

      > [smtp.mailgun.org]:2525 YOUR_SMTP_LOGIN:YOUR_SMTP_PASSWORD
      
    3. 通过输入分隔符 EOF 保存并关闭文件。

      > EOF
      
  11. 使用 postmap 实用程序生成一个 .db 文件。

    root@test-instance:~# postmap /etc/postfix/sasl_passwd
    
    root@test-instance:~# ls -l /etc/postfix/sasl_passwd*
    
    -rw------- 1 root root    68 Jun  1 10:50 /etc/postfix/sasl_passwd
    -rw------- 1 root root 12288 Jun  1 10:51 /etc/postfix/sasl_passwd.db
    
  12. 接着,移除包含您凭据的文件,因为将不再需要该文件。

    root@test-instance:~# rm /etc/postfix/sasl_passwd
    
  13. 设置您 .db 文件的权限。

    root@test-instance:~# chmod 600 /etc/postfix/sasl_passwd.db
    
    root@test-instance:~# ls -la /etc/postfix/sasl_passwd.db
    
    -rw------- 1 root root 12288 Aug 31 18:51 /etc/postfix/sasl_passwd.db
    
  14. 最后,重新加载配置以加载修改后的参数。

    Debian

    root@test-wheezy:~# /etc/init.d/postfix restart
    

    CentOS

    [root@test-centos ~]# postfix reload
    

  15. 测试您的配置。安装 mailxmailutils 软件包并测试您的配置。

    Debian

    root@test-wheezy:~# apt -y install mailutils
    

    CentOS

    [root@test-centos ~]# yum install mailx -y
    

    发送一条测试消息。

    root@test-instance:~# echo 'Test passed.' | mail -s 'Test-Email' EMAIL@EXAMPLE.COM
    

    在系统日志中查找包含 status 和表示成功的服务器响应代码 (250) 的状态行。

    Debian

    root@test-wheezy:~# tail -n 5 /var/log/syslog
    

    CentOS

    [root@test-centos ~]# tail -n 5 /var/log/maillog
    

如需查看其他主题的详细示例和信息(包括如何跟踪和路由电子邮件),请参阅 Mailgun 文档

探索有关 Google Cloud 的参考架构、图表和最佳实践。查看我们的 Cloud Architecture Center