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.
Related
I want to send emails with sendgrids API from a google cloud platform instance. They have a short tutorial on how to do this but it has the email message, who its from, who to send to, and other info all in app.rb which isnt the normal way messages are sent in rails apps.
I looked at the sendgrid ruby docs and they also dont have very good information. All the info in in one place and they dont state what files to put them in or even mention any smtp settings to use.
Here is what I have so far
development.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 465,
:domain => "mydomain.com",
:user_name => ENV['sendgrid_username'],
:password => ENV['sendgrid_password'],
:authentication => :plain,
:ssl => true,
:enable_starttls_auto => true
}
gemfile
gem "sendgrid-ruby"
The email views are in app/views, the mailer methods are in app/mailer the same as normal rails 4 apps have it setup.
So I guess here are my main questions:
Where do I call the environment variables holding the sendgrid api key?
Where do I tell Action Mailer to use sendgrid or SendGrid::Mail as I've seen in a couple of places?
Do I even need the sendgrid gem?
Are the smtp settings correct for sending over SSL with sendgrid?
I'm new to sending emails like this in a rails app and would really appreciate the help.
Put apikey as the name and the actual apikey as password:
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "yourwebsite.com",
authentication: :plain,
user_name: 'apikey',
password: Rails.application.secrets.sendgrid_api_key
}
Source
Use Figaro gem: https://github.com/laserlemon/figaro
It will generate an application.yml file for you, where you can store your sendgrid_username and sendgrid_password.
Have you generated your mailer? For example, generate mailer user_notifier where you can define your a default-from email, and some methods like this:
r
# send a signup email to the user, pass in the user object that contains the
user email address
default :from => 'hello#yourdomain.com'
def send_signup_email(user)
#user = user
mail( :to => #user.email,
:subject => 'Thanks for signing up!'
)
end
No, you don't need it.
https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html
I just finished my ruby foundations coursework at Bloc and I'm starting to bury my head into rails development. Things were going smooth until I hit this snag with devise and confirmation emails. I tried googling and looking around at some other questions but couldn't really find any that gave me anything that I could pull from and apply to my situation.
I'm receiving the following error when signing up for an account.
Net::SMTPAuthenticationError in Devise::RegistrationsController#create
535 Authentication failed: Bad username / password
Error extracted from source around line #976
def check_auth_response(res)
unless res.success?
raise SMTPAuthenticationError, res.message
end
end
From other posts I know you'll probably want to see that I have a config/initializers/setup_mail.rb file that looks like this:
if Rails.env.development?
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
authentication: :plain,
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
domain: 'heroku.com',
enable_starttls_auto: true
}
end
And here's an application.yml file EXAMPLE:
SENDGRID_PASSWORD:
SENDGRID_USERNAME:
SECRET_KEY_BASE:
also I have the following in my config/environments/development.rb before anybody suggests it:
config.action_mailer.default_url_options = { host: 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
# Override Action Mailer's 'silent errors' in development
config.action_mailer.raise_delivery_errors = true
If there's any other files that you'd like to see let me know and I'll add them to this post.
Congrats and welcome to the world of hacking on cool things.
The error you are getting means that the SendGrid server received a bad username + password combo.
Chances are your environment variables are empty and your application.yml file isn't being loaded properly with your SendGrid username + password.
You can confirm this by printing them out somewhere in your code. In a controller works.
puts "SENDGRID_USERNAME: #{ENV['SENDGRID_USERNAME']}"
puts "SENDGRID_PASSWORD: #{ENV['SENDGRID_PASSWORD']}"
I'd suspect that they are nil.
I'd recommend reading https://quickleft.com/blog/simple-rails-app-configuration-settings/ about how to get them sourced into your app.
Please let me know if you need any more help!
Two-Factor Authentication is required as of Q4 2020, and all Twilio
SendGrid API endpoints will reject new API requests and SMTP
configurations made with a username and password via Basic
Authentication.
I received a similar issue from an app I've been running for the last couple years. From now on, basic auth doesn't work, and you'll need to use an alternative auth mechanism. Heroku sendgrid auto-configuration on installation has not yet been updated to reflect this.
Source: https://sendgrid.com/docs/for-developers/sending-email/upgrade-your-authentication-method-to-api-keys/
In my case the error was to use as username the id of my apikey, but this is wrong, the correct value user_name is 'apikey', (Literally 'apikey' string), as they say in their integration example,
https://sendgrid.com/docs/for-developers/sending-email/rubyonrails/
https://app.sendgrid.com/guide/integrate/langs/smtp
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey', # This is the string literal 'apikey', NOT the ID of your API key
:password => '<SENDGRID_API_KEY>', # This is the secret sendgrid API key which was issued during API key creation
:domain => 'yourdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
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.
I am developing a website using ROR on Windows 7 (64-bit). I am trying to setup my website so that a person who creates a new login on it gets a confirmation email. I am using ActionMailer for the email confirmation sending. I am trying to configure it to send confirmation email using Gmail SMTP server. (This is just for testing. Once it works, I can use something else on the deployment server.)
I am getting this error:
Application-specific password required
Here is the code snippet from development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:authentication => :login,
:user_name => 'my_login#gmail.com',
:password => 'my_application_specific_password'
}
Why I am getting this error even though I generated a new application-specific password for this purpose and am using it? How to fix this?
I think you may have enabled extra 2-step security on the Google Account you are using to send emails. This can require you to sign in using a special code sent to your mobile phone by text message; or for particular non-compatible applications, creating an 'Application-specific password'.
More details on Application-specific passwords here.
I'm trying to get sendgrid running on my Rails 3 App with Devise, so devise can send out registration emails etc..
I added the following, config/setup_mail.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '25',
:domain => "XXXXXXXXX.com",
:authentication => :plain,
:user_name => "XXXXXXXXXXX#gmail.com",
:password => "XXXXXXXXXX"
}
Shouldn't that be enough for Rails + Devise to send out registration emails? Or do I need something else or a gem of some kind?
The logs show the email being generated but I don't see anything in the log about MAIL being sent successfully or erroring. And my sendgrid account still says 0/200 emails sent.
Is there a better way in Rails to see what's going on when it trys to send the email?
Thanks
You can erase the setup that you have.
heroku addons:create sendgrid:free
That is the only pieces of code you need to get email configured with heroku.
Make sure you have your host link setup which I think you did because it will cause it to crash but if you haven't:
config.action_mailer.default_url_options = { :host => 'myapp.heroku.com' }
Actually this last lien is different on rails3 so watch out for that :)
The "config" line needs to be added to your "production.rb" file.
I'm searching for the same answer myself. In intializers/devise.rb I read:
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"
I wonder if Devise has to be told to use Actionmailer.