Reputation: 41929
I'm using gmail to send out emails from my application. Anticipating very low traffic but maybe more than enough that I might hit gmail mail limits so I'm setting it up (in my application controller) to use two different accounts depending on the time of day.
I've used this set up before successfully but now that I've introduced the "greater than" or "less than" symbols I'm getting an error message about the "when." In another application I did
when Time.now == 1
....
when Time.now == 2
...etc
and it worked fine.
Can anyone tell me what's wrong with this?
case
when Time.now.hour > 12
ActionMailer::Base.smtp_settings = {
:user_name => "[email protected]",
:password => ENV['GMAIL_PASS'],
:address => "smtp.gmail.com",
:port => 587,
:tls => true
}
when Time.now.hour < 12
ActionMailer::Base.smtp_settings = {
:user_name => "[email protected]",
:password => ENV['GMAIL_PASS'],
:address => "smtp.gmail.com",
:port => 587,
:tls => true
}
end
Upvotes: 0
Views: 155
Reputation: 6856
Why use a case statement with only 2 options? A very simple and more elegant way of accomplishing what you want to do is:
username = ["[email protected]", "[email protected]"].sample
Then you will get a random distribution that over time will be 50/50. I think using gmail in general though for bulk mailing is bad. Any decent host can give you a SMTP server.
Upvotes: 2
Reputation: 4926
I can't answer why the error is occurring. I've tested it and like @summea said, it seems to work without else
(although using else
is better - your example would do nothing when Time.now.hour == 12
)
However, I think dividing accounts on hours is a bad idea. I doubt that usage will be evenly spread; because different parts of the world will sleep at different times. So you might find 80% of mails are sent via one account.
If you split by seconds, you would get a more even distribution.
To make subsequent modification simpler, you might also want to set a variable for user_name, and avoid repeating the other server settings:
case
when Time.now.sec > 29
user_name = "[email protected]"
else
user_name = "[email protected]"
end
ActionMailer::Base.smtp_settings = {
:user_name => user_name,
:password => ENV['GMAIL_PASS'],
:address => "smtp.gmail.com",
:port => 587,
:tls => true
}
Upvotes: 1