Postfix + Dovecot 发送电子邮件时间过长

Postfix + Dovecot 发送电子邮件时间过长

当我尝试使用 dovecot 和 postfix 在 Rails 4.2 网站上发送电子邮件时,我收到 504 错误。我还使用 Redis 和 Sidekiq 设置了一个异步电子邮件服务器,当我发送异步电子邮件时,它们需要 5 到 30 分钟才能发送。

我的邮件服务器与我的网站在同一个机器上运行,但有趣的是,当我在我的家用电脑上运行我的开发服务器时,它会连接到我的网站的邮件服务器并快速、无问题地发送邮件。

我尝试查看我的mail.log日志以及/var/log目录中的所有其他日志,但是在等待邮件发送期间我没有看到任何内容,但是有一些垃圾邮件攻击(我正在运行 SpamAssassin 来关闭它们)。

我的 rails 服务器日志也没有显示任何有用的内容,只是记录电子邮件已呈现(未发送),然后在一分钟后重新启动服务器。

在我的开发服务器和生产服务器上我使用以下方式进行连接:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    :address => 'mail.mysite.com',
    :domain => 'mysite.com',
    :port => 587,
    :authentication => :plain,
    :enable_starttls_auto => true,
    :openssl_verify_mode  => 'none',
    :user_name => '[email protected]',
    :password => 'test'
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

结果如下dovecot -n

# 2.2.9: /etc/dovecot/dovecot.conf
# OS: Linux 4.2.0-23-generic x86_64 Ubuntu 14.04.4 LTS
auth_mechanisms = plain login
mail_location = maildir:~/Maildir
mail_privileged_group = mail
managesieve_notify_capability = mailto
managesieve_sieve_capability = fileinto reject envelope encoded-character vacation subaddress comparator-i;ascii-numeric relational regex imap4flags copy include variables body enotify environment mailbox date ihave
namespace inbox {
  location =
  mailbox Drafts {
    special_use = \Drafts
  }
  mailbox Junk {
    special_use = \Junk
  }
  mailbox Sent {
    special_use = \Sent
  }
  mailbox "Sent Messages" {
    special_use = \Sent
  }
  mailbox Trash {
    special_use = \Trash
  }
  prefix =
}
passdb {
  args = /etc/dovecot/dovecot-sql.conf.ext
  driver = sql
}
plugin {
  sieve = ~/.dovecot.sieve
  sieve_dir = ~/sieve
}
protocols = imap pop3 sieve
service auth-worker {
  user = vmail
}
service auth {
  unix_listener /var/spool/postfix/private/auth {
    group = postfix
    mode = 0666
    user = postfix
  }
  unix_listener /var/spool/postfix/private/dovecot-auth {
    group = postfix
    mode = 0660
    user = postfix
  }
  unix_listener auth-userdb {
    mode = 0600
    user = vmail
  }
  user = dovecot
}
service imap-login {
  inet_listener imap {
    port = 0
  }
}
service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    group = postfix
    mode = 0600
    user = postfix
  }
}
ssl_cert = </etc/dovecot/dovecot.pem
ssl_cipher_list = ALL:!LOW:!SSLv2:ALL:!aNULL:!ADH:!eNULL:!EXP:RC4+RSA:+HIGH:+MEDIUM
ssl_key = </etc/dovecot/private/dovecot.pem
userdb {
  args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
  driver = static
}
protocol pop3 {
  mail_max_userip_connections = 10
  pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
}
protocol imap {
  imap_client_workarounds = delay-newmail
  mail_max_userip_connections = 10
}
protocol lda {
  deliver_log_format = msgid=%m: %$
  mail_plugins = sieve
  postmaster_address = postmaster
  quota_full_tempfail = yes
  rejection_reason = Your message to <%t> was automatically rejected:%n%r
}

postconf -n

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
inet_interfaces = all
mailbox_command = /usr/lib/dovecot/deliver -c /etc/dovecot/dovecot.conf -m "${EXTENSION}"
mailbox_size_limit = 0
mydestination = localhost
myhostname = mysite.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
queue_directory = /var/spool/postfix
readme_directory = no
recipient_delimiter = +
relayhost =
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_recipient_restrictions = permit_sasl_authenticated
smtpd_relay_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = private/auth
smtpd_sasl_type = dovecot
smtpd_tls_auth_only = yes
smtpd_tls_cert_file = /etc/nginx/ssl/ssl-bundle.crt
smtpd_tls_key_file = /etc/nginx/ssl/server.key
smtpd_use_tls = yes
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_transport = dovecot

答案1

我的问题原来是双重问题:

  1. 我需要permit_mynetworks补充smtpd_recipient_restrictions

    postfix/main.cf的要求是

    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks
    


  1. 由于我有两个 Sidekiq 服务器在运行,因此我需要使用单独的队列

    我在同一台机器上运行了临时服务器和生产服务器,结果发现电子邮件经常进入错误的队列,从而导致异常,并且电子邮件随后重新排队并进一步延迟。为了解决这个问题,我需要通过修改我的代码来分离队列,config/sidekiq.yml包括:

    :queues:
      - staging
    

    并按以下方式发送电子邮件:

    UserMailer.delay(queue: Rails.env).my_email(args)
    

完成此操作后,我确保清除了 Redis 和 Sidekiq 队列,因为一些坏电子邮件仍然堵塞了系统,现在即使有延迟,电子邮件也能发送得非常快。

相关内容