How can I work with Gmail SMTP outbound restrictions?

I use my Gmail Apps for Domain account to send emails in my rails application for standard automatic emails (user registration, forgot password, notification notification for new comment, etc.), but I'm worried about 500 messages for each daily limit, installed by Google.

Google offers one way to break the limit - use multiple user accounts.

So, I set up 10 additional gmail user accounts (noreply1, noreply2, noreply3, etc.). I would like to track when any of these accounts sent 500 emails within 24 hours, and use the account,

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

Here my current setup is NOTE: this is sent with "noreply1" every time, although I explicitly set: user_name and: from to "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 
+6
email ruby-on-rails smtp
source share
4 answers

You can also configure MTA on your server and use this to send mail.

What are we doing.

You must add your server IP address as a valid one to send email to your SPF domain account to avoid getting marked as spam.

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

+9
source share

Save the available usernames in a table in the database along with the “last modified”, “last reset” and the sent invoice. You can then request this when sending an email to find the least used email address currently. Then increase the number of submitted and latest modifications. The value "last-reset" can be used for your cleanup code so that you reset count every 24-hour period.

It also makes it easy to add new email accounts, delete accounts that you no longer use, implement in another application, etc., like everything in the database table, which you can change if necessary.

+3
source share

You should be able to set the: user_name element in the hash in the mail program in the same way as in the configuration, namely:

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

Although this may require some additional code to force a reboot of any internal configuration of the mail program (I did not check it myself)

+2
source share

The comment box was getting too restrictive for my questions. Changing the hash of ActionMailer::Base.smtp_settings dynamically works as expected for me, so I suspect there are other factors as well. Some things to try:

  • Do you use the TLS plugin? I used action_mailer_optional_tls with Rails 2.3.2 and Ruby 1.8.6.
  • What is written to the log / console?
  • You change the username but not the password: do all noreply accounts have the same password?

Edit: more things to try

I would take a good look at this smtp_tls.rb file mentioned in the comments to make sure that nothing is hardcoded. Or delete it and try the plugin connected to it. To use it, simply add :tls => true to the smtp_settings hash table.

0
source share

All Articles