如何使用/围绕Gmail的SMTP出站发送限制?

时间:2020-12-04 18:14:06

I'm using my Gmail Apps for Domain account to send email within my rails application for standard automated emails (user signup, forgot password, notify admin of new comment, etc), but I'm worried about the 500 emails per day limit set by Google.

我正在使用我的Gmail Apps for Domain帐户在我的rails应用程序中发送标准自动电子邮件(用户注册,忘记密码,通知新评论管理员等)的电子邮件,但我担心每天限制设置500封电子邮件由谷歌。

Google suggests one way to overcome the limit is to use multiple user accounts.

谷歌建议克服限制的一种方法是使用多个用户帐户。

So, I've setup 10 additional gmail user accounts (noreply1, noreply2, noreply3, etc) - I'd like to track when any of these accounts has sent 500 emails in a 24 hour period and use the idle account accordingly.

所以,我已经设置了10个额外的gmail用户帐户(noreply1,noreply2,noreply3等) - 我想跟踪这些帐户中的任何一个在24小时内发送了500封电子邮件并相应地使用空闲帐户。

How do I dynamically set the :user_name value in ActionMailer::Base.smtp_settings?

如何在ActionMailer :: Base.smtp_settings中动态设置:user_name值?

Here's my current setup - NOTE: this sends from "noreply1" every time, even though i'm explicitly setting :user_name and :from to "noreply2":

这是我当前的设置 - 注意:每次都从“noreply1”发送,即使我明确设置:user_name和:from from“noreply2”:

--- development.rb --- 
    ActionMailer::Base.delivery_method = :smtp
    ActionMailer::Base.smtp_settings = {
        :address => "smtp.gmail.com",
        :port => "587",
        :domain => "mydomain.com",   
        :authentication => :plain,
            :user_name => "noreply1@mydomain.com",
        :password => "password"
    }

--- account.rb --- (MODEL, called via a callback)
after_create :send_welcome_email
...
def send_welcome_email
  #ActionMailer::Base.smtp_settings[:user_name] = 'noreply2@mydomain.com'
  ActionMailer::Base.smtp_settings.merge!({:user_name => "noreply2@mydomain.com"})  
  SubscriptionNotifier.deliver_welcome(self)   
end

--- subscription_notifier.rb --- (MODEL) 
class SubscriptionNotifier < ActionMailer::Base
  def welcome(account)    
    @sent_on = Time.now
    @subject = "Welcome to the App"
    @recipients = account.email
    @from = "noreply2@mydomain.com" 
    @body = { :account => account }
  end
end

4 个解决方案

#1


You could also set up an MTA on your server and use that for sending the mail.

您还可以在服务器上设置MTA并使用它来发送邮件。

That's what we do.

这就是我们的工作。

You have to add your server's IP as a valid one for sending email in your domain's SPF record to avoid getting marked as spam.

您必须将服务器的IP添加为有效的IP,以便在您的域的SPF记录中发送电子邮件,以避免被标记为垃圾邮件。

Another benefit of this is that if you do this, you can set the From: address of the email to be one of your users, which you cannot do with GMail.

这样做的另一个好处是,如果您这样做,您可以将电子邮件的发件人:地址设置为您的用户之一,而您无法使用GMail。

#2


Store the available usernames in a table in the database, along with a 'last-modified', 'last-reset' and a sent count. You can then query this when sending an email to find the least used email address currently. Then increment the sent count and last-modified account. The 'last-reset' value can be used for your cleanup code so that you reset the counts each 24 hour period.

将可用的用户名存储在数据库的表中,以及“last-modified”,“last-reset”和发送计数。然后,您可以在发送电子邮件时查询此信息,以查找当前使用最少的电子邮件地址然后递增发送的计数和最后修改的帐户。 'last-reset'值可用于清理代码,以便每24小时重置一次计数。

This also makes it easy to add new email accounts, retire accounts you aren't using anymore, implement in a different app, etc. as it's all just in a database table that you can change when required.

这也可以轻松添加新的电子邮件帐户,退出您不再使用的帐户,在不同的应用程序中实施等等,因为它只是在数据库表中,您可以在需要时进行更改。

#3


You should be able to set the :user_name element in the hash in the mailer in the same fashion as in the configuration, namely by doing:

您应该能够以与配置中相同的方式在邮件程序中的哈希中设置:user_name元素,即通过执行以下操作:

ActionMailer::Base.smtp_settings[:user_name] = 'new_user_name'

Although this may require some extra code to force a reload of any internal action mailer config (not tested this myself)

虽然这可能需要一些额外的代码来强制重新加载任何内部动作邮件程序配置(我自己不测试)

#4


The comment box was becoming too restrictive for my questions. Changing the ActionMailer::Base.smtp_settings hash dynamically works as expected for me, so I suspect there is some other factor at play here. Some things to try:

评论框对我的问题变得过于严格。动态更改ActionMailer :: Base.smtp_settings哈希对我来说是预期的,所以我怀疑还有一些其他因素在起作用。有些事要尝试:

  1. Are you using a TLS plugin? I used action_mailer_optional_tls with Rails 2.3.2 and Ruby 1.8.6.
  2. 你在使用TLS插件吗?我在Rails 2.3.2和Ruby 1.8.6中使用了action_mailer_optional_tls。

  3. What is being writing to the log/console?
  4. 正在写入日志/控制台的是什么?

  5. You're changing the username but not the password: do all the noreply accounts have the same password?
  6. 您正在更改用户名但不更改密码:所有noreply帐户都使用相同的密码吗?

Edit: more things to try

编辑:更多的事情要尝试

I'd have a good look at that smtp_tls.rb file mentioned in the comments to make sure nothing's hard-coded. Or remove it and try the plugin I linked above. To use it, you just need to add :tls => true to the smtp_settings hash.

我会好好看看评论中提到的smtp_tls.rb文件,以确保没有任何硬编码。或者删除它并尝试我上面链接的插件。要使用它,您只需要在smtp_settings哈希中添加:tls => true。

#1


You could also set up an MTA on your server and use that for sending the mail.

您还可以在服务器上设置MTA并使用它来发送邮件。

That's what we do.

这就是我们的工作。

You have to add your server's IP as a valid one for sending email in your domain's SPF record to avoid getting marked as spam.

您必须将服务器的IP添加为有效的IP,以便在您的域的SPF记录中发送电子邮件,以避免被标记为垃圾邮件。

Another benefit of this is that if you do this, you can set the From: address of the email to be one of your users, which you cannot do with GMail.

这样做的另一个好处是,如果您这样做,您可以将电子邮件的发件人:地址设置为您的用户之一,而您无法使用GMail。

#2


Store the available usernames in a table in the database, along with a 'last-modified', 'last-reset' and a sent count. You can then query this when sending an email to find the least used email address currently. Then increment the sent count and last-modified account. The 'last-reset' value can be used for your cleanup code so that you reset the counts each 24 hour period.

将可用的用户名存储在数据库的表中,以及“last-modified”,“last-reset”和发送计数。然后,您可以在发送电子邮件时查询此信息,以查找当前使用最少的电子邮件地址然后递增发送的计数和最后修改的帐户。 'last-reset'值可用于清理代码,以便每24小时重置一次计数。

This also makes it easy to add new email accounts, retire accounts you aren't using anymore, implement in a different app, etc. as it's all just in a database table that you can change when required.

这也可以轻松添加新的电子邮件帐户,退出您不再使用的帐户,在不同的应用程序中实施等等,因为它只是在数据库表中,您可以在需要时进行更改。

#3


You should be able to set the :user_name element in the hash in the mailer in the same fashion as in the configuration, namely by doing:

您应该能够以与配置中相同的方式在邮件程序中的哈希中设置:user_name元素,即通过执行以下操作:

ActionMailer::Base.smtp_settings[:user_name] = 'new_user_name'

Although this may require some extra code to force a reload of any internal action mailer config (not tested this myself)

虽然这可能需要一些额外的代码来强制重新加载任何内部动作邮件程序配置(我自己不测试)

#4


The comment box was becoming too restrictive for my questions. Changing the ActionMailer::Base.smtp_settings hash dynamically works as expected for me, so I suspect there is some other factor at play here. Some things to try:

评论框对我的问题变得过于严格。动态更改ActionMailer :: Base.smtp_settings哈希对我来说是预期的,所以我怀疑还有一些其他因素在起作用。有些事要尝试:

  1. Are you using a TLS plugin? I used action_mailer_optional_tls with Rails 2.3.2 and Ruby 1.8.6.
  2. 你在使用TLS插件吗?我在Rails 2.3.2和Ruby 1.8.6中使用了action_mailer_optional_tls。

  3. What is being writing to the log/console?
  4. 正在写入日志/控制台的是什么?

  5. You're changing the username but not the password: do all the noreply accounts have the same password?
  6. 您正在更改用户名但不更改密码:所有noreply帐户都使用相同的密码吗?

Edit: more things to try

编辑:更多的事情要尝试

I'd have a good look at that smtp_tls.rb file mentioned in the comments to make sure nothing's hard-coded. Or remove it and try the plugin I linked above. To use it, you just need to add :tls => true to the smtp_settings hash.

我会好好看看评论中提到的smtp_tls.rb文件,以确保没有任何硬编码。或者删除它并尝试我上面链接的插件。要使用它,您只需要在smtp_settings哈希中添加:tls => true。