Вы находитесь на странице: 1из 5

Welcome to Q&A for professional and enthusiast programmers check out the FAQ!

! Stack Exchange log in careers chat meta about faq Stack Overflow Questions Tags Users Badges Unanswered Ask Question How can I work with/around Gmail's SMTP outbound sending limits? up vote 5 down vote favorite 6 I'm using my Gmail Apps for Domain account to send email within my rails applica tion for standard automated emails (user signup, forgot password, notify admin o f new comment, etc), but I'm worried about the 500 emails per day limit set by G oogle. 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. How do I dynamically set the :user_name value in ActionMailer::Base.smtp_setting s? Here's my current setup - NOTE: this sends from "noreply1" every time, even thou gh i'm explicitly setting :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 ruby-on-rails email smtp link edit flag edited Apr 1 '09 at 22:06 asked Mar 9 '09 at 14:19 Jim Jones 212213 74% accept rate i think workmad3's idea of changing the ActionMailer::Base.smtp_settings hash is on the right track, I just can't figure out how to get rails to reload that has h's values with my dynamically set :user_name key. Jim Jones Mar 26 '09 at 19:50 What version of Rails are you using? Changing ActionMailer::Base.smtp_settings[: user_name] dynamically in a controller action works for me in 2.3.2. (At least, the hash changes. I didn't try sending mail.) Andrew Watt Apr 1 '09 at 8:54 that's the strange thing - the hash key value changes, but the email is sent usi ng the original :user_name that was in my development.rb file Jim Jones Apr 1 '0 9 at 14:07 Even sending works for me. Can you post some code showing exactly how your origi nal configuration and changes in the controller are being done? Andrew Watt Apr 1 '09 at 20:31 sure thing - i'll add it to the question above...thanks Jim Jones Apr 1 '09 at 2 1:26 4 Answersactiveoldestvotes up vote 8 down vote accepted +50 You could also set up an MTA on your server and use that for sending the mail. 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. 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. link edit flag answered Mar 9 '09 at 17:36 Luke Francl 3,96611242 When somebody replies to an email sent out by you with a "from" address that's r egistered in GMail, you're not experiencing any spam issues from Google? pbz Apr 2 '09 at 5:59 Hmm...I haven't seen any problems with it. We registered Google Apps for Your Do main as our mail server in SPF and then also registered another IP address (our server) as a valid sender. Luke Francl Apr 2 '09 at 21:34 up vote 3 down vote Store the available usernames in a table in the database, along with a 'last-mod

ified', 'last-reset' and a sent count. You can then query this when sending an e mail to find the least used email address currently. Then increment the sent cou nt and last-modified account. The 'last-reset' value can be used for your cleanu p code so that you reset the counts each 24 hour period. This also makes it easy to add new email accounts, retire accounts you aren't us ing anymore, implement in a different app, etc. as it's all just in a database t able that you can change when required. link edit flag answered Mar 9 '09 at 14:34 workmad3 8,1721030 Sounds like a good rails plugin if you write that! Kyle Boon Mar 9 '09 at 14:43 thanks, very helpful technique for tracking the email accounts, but I was specif ically looking for code to set the :user_name element in a typical rails smtp co nfig hash. Jim Jones Mar 9 '09 at 15:11 Oops, didn't read the question properly. I'll add a second response with what I think will work for the actual problem :) @kyle: I may do that then... it seems pretty intuitive to me so surprised there isn't already something available alon g these lines workmad3 Mar 9 '09 at 15:26 up vote 2 down vote You should be able to set the :user_name element in the hash in the mailer in th e same fashion as in the configuration, namely by doing: ActionMailer::Base.smtp_settings[:user_name] = 'new_user_name' Although this may require some extra code to force a reload of any internal acti on mailer config (not tested this myself) link edit flag answered Mar 9 '09 at 15:26 workmad3 8,1721030 i finally got around to testing this, but I can't get it to work. setting Action Mailer::Base.smtp_settings[:user_name] = 'new_user_name' in a controller method doesn't affect the hash at all. any idea how to reload the hash? Jim Jones Mar 2 6 '09 at 18:02 up vote 0 down vote The comment box was becoming too restrictive for my questions. Changing the Acti onMailer::Base.smtp_settings hash dynamically works as expected for me, so I sus pect there is some other factor at play here. Some things to try: Are you using a TLS plugin? I used action_mailer_optional_tls with Rails 2.3.2 a nd Ruby 1.8.6. What is being writing to the log/console? You're changing the username but not the password: do all the noreply accounts h ave the same password? 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 us e it, you just need to add :tls => true to the smtp_settings hash. link edit flag edited Apr 2 '09 at 0:27

answered Apr 1 '09 at 23:18 Andrew Watt 58838 1. I'm using an smtp_tls.rb in my lib directory, but I honestly don't remember w here I got if from, but it sends mail with gmail fine. i'll try action_mailer_op tional_tls - yes, Rails 2.3.2, Ruby 1.86 patch lvl 111 2.console is logging emai l sent from noreply1 3.yes, all the passwords are the same Jim Jones Apr 1 '09 a t 23:39 are you using similar code to me? someone suggested "reloading" the hash, but I don't know how to do that. are you setting the value directly or using merge! Ji m Jones Apr 1 '09 at 23:40 Almost identical code. I tried setting the value as well as merge!, and both wor ked. You shouldn't need to "reload" the hash: ActionMailer uses the same smtp_se ttings hash you provide--there's no internal cache. Andrew Watt Apr 2 '09 at 0:0 8 Your Answer log in or Name Email Home Page Not the answer you're looking for? Browse other questions tagged ruby-on-rails e mail smtp or ask your own question. Hello World! This is a collaboratively edited question and answer site for professional and e nthusiast programmers. It's 100% free, no registration required. about faq tagged ruby-on-rails 36707 email 5746 smtp 1170 asked 2 years ago viewed 3,122 times latest activity 2 years ago Software Engineer/UI Developer Swiftpage Englewood, CO Software Engineer (JavaScript) Nokia Berlin, Germany view more jobs Related Rails and Gmail SMTP, how to use a custom from address How do I send email to my Gmail account using SMTP and Perl? SMTP email errors in Rails Send email using GMail SMTP server from PHP page Debugging an SMTP Timeout Error How to change from-address when using gmail smtp server

Need Smtp Server for Windows that wont deliver email sending emails with ruby where the address has a gmail style '+' filter Sending email via GMail in .NET Delivery issues with Rails + Gmail SMTP in Dreamhost Sending emails through SMTP and testing how to send email from rails app with gmail address Mail relays or SMTP services for use in code Sending mail with SMTP to multiple addresses: relaying the message to different servers ? reaching Gmail SMTP daily limit Sending Rails e-mails through Register.com's smtp Sending mail with devise and Gmail smtp server 555 5.5.2 Syntax error. gmail's smtp Rails: Controlling the logging and sending of emails in different environments Send out emails from Rails app using GoDaddy email What could cause a message sent from Gmail SMTP using C# not to arrive - No exce ption is thrown Using gmail for SMTP Send mail ruby (gmail with net/smtp) Grails: How to buffer outbound emails when SMTP server is temporarely down? Problem in Sending Email using Gmail Settings question feed about faq blog chat data podcast legal advertising info contact us feedback always welcome stackoverflow.com api/apps careers serverfault.com superuser.com meta a 51 webapps gaming ubuntu webmasters cooking game development math graphy stats tex english theoretical cs programmers unix apple word physics home improvement gis electronics android rev 2011.4.24.1 site design / logo 2011 stack exchange inc; user contributions licensed under cc -wiki with attribution required

Вам также может понравиться