Devise, admin_mailer & config - ruby-on-rails

I'm using devise with my rails 4 app.
I'm trying to setup figaro before I let the site go live, but for now, I'm managing my test email account with my passwords in production.rb. It won't be this way for long, but I have hit a problem with my setup.
I have two admin_mailer emails that get sent when someone registers. One is an email to me, using one gmail account to let me know that they have registered. The second is an email to the user, welcoming them to the site. It is supposed to be sent from a second email account that I set up.
In my production.rb, I have:
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: ###,
domain: 'gmail.com',
user_name: 'firstemailaddress#gmail.com',
password: '###',
authentication: 'plain',
enable_starttls_auto: true }
{
address: 'smtp.gmail.com',
port: ###,
domain: 'gmail.com',
user_name: 'secondemailaddress#gmail.com',
password: '####',
authentication: 'plain',
enable_starttls_auto: true }
In my admin_mailer, I set the sender for the emails as one or the other of those user names.
However, when I run the code, both emails are sent from the first email address.
Does anyone know how to set up to mailers so that emails send from different addresses?
Thank you

I think each mailer is supposed to be associated with one email account. You could create two separate mailers, and define the smtp settings within each individual mailer rather than in production.rb.
For example, app/mailers/first_mailer.rb :
class FirstMailer < ActionMailer::Base
default from: 'firstemailaddress#gmail.com'
self.delivery_method = :smtp
self.smtp_settings = {
# your gmail smtp settings here
}
def some_email(user)
mail(to: user.email, subject: 'Subject')
end
end
You can send this by calling FirstMailer.some_email(user).deliver.
Similarly, you then create another file app/mailers/second_mailer.rb that uses a different account (and so different settings).
There may be a more DRY way to do this, but I know this works.

Related

Rails ActionMailer sends email but it does not show up as a sent email in the mailbox used

I am using namecheap to send emails and it uses privateemail.
My setup in ActionMailer is:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'mail.privateemail.com',
port: 587,
domain: 'privateemail.com',
user_name: 'very#cool.com',
password: "very_secret",
authentication: 'plain',
enable_starttls_auto: true
}
As they say here:
https://www.namecheap.com/support/knowledgebase/article.aspx/1179/2175/general-private-email-configuration-for-mail-clients-and-mobile-devices/
Now it sends the emails, but when I login in the web client, it does not show any mails sent in the sent folder.
Why is that ?
SMTP delivery means sending an email and not send an email and store it in sent folder. This has to be done by the client additionally!

Can't get user confirmation to work in production with devise

So I've been trying to debug this all day but getting nowhere. Basically I have the confirmable module in my user model. It sends the confirmation link just fine but when I click the link in the email, it takes me to my home page and does nothing. It works perfectly fine in dev. Here are my settings in my prod configs:
config.action_mailer.default_url_options = { host: 'mysite.com' }
config.action_mailer.smtp_settings = {
address: 'email-smtp.us-east-1.amazonaws.com',
port: 587,
user_name: ENV['SES_USERNAME'],
password: ENV['SES_PASSWORD'],
authentication: :login,
enable_starttls_auto: true
}
The confirmation token param in the link does in fact match the token saved to the user object. Not sure what to look for in my logs at this point. There aren't any errors coming up. What am I doing wrong here?

Seding emails with Devise gem and Mailgun Api

I want to send automated emails via Mailgun either SMTP or API. The problem is that in tutorials I find they explain how to do that manually e.i creating mailer class etc. For example like that:
def send_simple_message
RestClient.post "https://api:YOUR_API_KEY"\
"#api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
:from => "Excited User <mailgun#YOUR_DOMAIN_NAME>",
:to => "bar#example.com, YOU#YOUR_DOMAIN_NAME",
:subject => "Hello",
:text => "Testing some Mailgun awesomness!"
end
This is from official Mailgun documentation.
But I am using Devise gem which has email sending implemented.
For example I want to send password reset email. When I click forgot password and submit my email from logs I see that my email is tried to be sent, but not sent of course, I need to set up email server.
So the question is where is this code for sending recovery email is written in devise, how to override it? I want it to oveeride so it will use Mailgun API for example.
I have already generated registrations_controller.rb using
rails generate devise:controllers registrations
command. So I suppose I am overriding it here?
Any suggestions?
have you read this tutorial? Looks like you need to setup it in config/environments/development.rb
Something like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: ENV['GMAIL_DOMAIN'],
authentication: 'plain',
user_name: ENV['GMAIL_USERNAME'],
password: ENV['GMAIL_PASSWORD']
}
Also, you can try to use mail gun gem. Looks like It's really easy to setup it
config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
api_key: '<mailgun api key>',
domain: '<mailgun domain>'
}
Hope it helps you.

Running into SMTP error when trying to send email in RoR app

I've been desperately trying to send an email using the Action Mailer class in RoR, and even though the terminal shows that a message is being sent, it keeps returning with this error:
et::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:968:in `check_auth_response'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:739:in `auth_plain'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:731:in `authenticate'...
Here's what my mailer class looks like within app/mailers/my_mailer.rb:
class MyMailer < ActionMailer::Base
default from: "from#example.com"
def welcome_email(user)
#user = user
mail(to: user.email,
from: 'example_email#gmail.com',
subject: 'Welcome!')
end
end
And here's what my config/application.rb file looks like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
#domain: 'gmail.com',
user_name: 'example_email#gmail.com',
password: 'my_password',
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.default_url_options = {
:host => "localhost",
:port => 3000
}
in my config/environments/development.rb file I also have these two lines:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
My problem is that whenever I start the rails console (working in development, have no idea how to run in production), assign a user's email to the user variable then enter the command: MyMailer.welcome_email(user).deliver
the terminal says:
Rendered my_mailer/welcome_email.html.erb (11.7ms)
Rendered my_mailer/welcome_email.text.erb (0.5ms)
Sent mail to abhas.arya#yahoo.com (923.1ms)
Date: Fri, 08 Aug 2014 13:54:38 -0400
From: abhas.aryaa#gmail.com
To: abhas.arya#yahoo.com
Message-ID: <53e50ededebb2_2b0b8082dbf8507c5#Abhass-MacBook-Pro.local.mail>
Subject: Welcome!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_53e50eded7355_2b0b8082dbf85068f";
charset=UTF-8
Content-Transfer-Encoding: 7bit
but then renders the error. My views are simple enough that I didn't think I needed to include them here. I've desperately tried everything, including unlocking google captcha but still nothing works. I'd appreciate any help, all I want is for an email to get sent to the proper inbox. I'd love you forever if you can solve this issue!!!
The reason that you are seeing this error is that Google has blocked your IP. You will need to enable authorization for this IP.
Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.
Then again try to send email from the application, it should work. You will need to send email from your app within 10 min of visiting the above link. By visiting above link, Google will grant access to register new apps for 10 min.
I solved by doing the following:
1) Log into the Google Apps Admin portal and enable Less Secure Apps by checking "Allow users to manage their access to less secure apps" under Security preferences.
2) Login into the account that will serve as the mailer and turn "Allow less secure apps" to ON.
Most of the answers available on-line refer to step 2, but you cannot configure at a user level without the Admin turning the feature on.
This one solved the issue for me after some investigation:
Click https://www.google.com/settings/u/0/security/lesssecureapps
Enable access for less secure apps here by logging in with the email address you have provided in smtp configuration.
I resolved my issue by doing this.
config.action_mailer.default :charset => "utf-8"
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mysite.com',
user_name: myemail#gmail.com,
password: mypassword,
authentication: 'plain',
enable_starttls_auto: true
}
As google will try to block your sign-in if you have turned off access for less secure apps in your accounts settings. Therefore, follow this link and "Turn on" access for less secure apps.

Sending an email from my ROR4 app

I know the question has been asked before, but I still can't get it to work, even though the console is saying it has.
emailer.rb
class Emailer < ActionMailer::Base
default from: "from#example.com"
def welcome_email()
#url = 'http://example.com/login'
email_with_name = "#{My Name} <#{MyEmail#gmail.com}>"
mail(to: email_with_name, subject: 'Test')
end
end
environment.rb #(in config)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: 'myemail#gmail.com',
password: 'mypassword',
authentication: 'plain',
enable_starttls_auto: true }
To test, I'm putting Emailer.welcome_email().deliver into the console
I'm told that if my setting are incorrect, it's possible that no error comes up.
Do these settings look ok to everyone? If so, where else might I check to find the problem?
Thanks
I was having the same issue as you. All the data appeared to be passing through but no email was getting sent. I was actually seeing a Connection error in my server log which to me indicated an inability of my app to connect to Google.
I ended up finding this instruction set http://www.leemunroe.com/send-automated-email-ruby-rails-mailgun/ and creating a free MailGun account. It was really straight-forward. Had it up and running within about 30 minutes, as opposed to the hours I spent failing with Google + SMTP.
Hope this helps. Good luck.

Resources