SendGrid on Heroku fails - ruby-on-rails

I setup send grid starter for my heroku app.
I put this in my config/environment.rb:
ActionMailer::Base.smtp_settings = {
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:domain => "my-sites-domain.com",
:address => "smtp.sendgrid.net",
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I create this class models/notifier.rb:
class Notifier < ActionMailer::Base
def notify()
mail(
:to => "my-email#gmail.com",
:subject => "New Website Contact",
:body => "body",
:message => "message",
:from => "website#my-sites-domain.com"
)
end
end
I put this in another controller to send the email:
def contact
Notifier.notify().deliver
redirect_to("/", :notice => "We Have Received Your Request And Will Contact You Soon.")
end
When I deploy and try to send an email I get this error:
Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password
I have setup send grid completely and it says I am ready to send emails.
I also ran heroku config --long to get the actual password and user name and hard coded them and that still gave me the same error.

RoR + SendGrid + Heroku:
I came across similar problem where I kept receiving this error
Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password when I was trying to send email.
I tried
username with which I signed up with sendgrid, and
also the one heroku generated (when I commissioned the addon).
Answer is very simple.
DO not use any username. It is simple text 'apikey' and for password use the generated API key that you got from Dashboard.
More here #
https://support.sendgrid.com/hc/en-us
What is my SMTP Username and Password?
We recommend using "apikey" for your SMTP username and creating an API Key to use for your password. For more information, click here.
https://sendgrid.com/docs/for-developers/sending-email/integrating-with-the-smtp-api/

I had tried to set my password with the command:
heroku config:add SENDGRID_PASSWORD=my-new-password
But it turns out this only changes what heroku has stored as your password, it doesn't actually change your password. You also cannot retrieve your password after doing this.
What I had to do was remove and re-add the sendgrid add on.

Related

How do I send emails using Sendgrids API to send emails with rails 4? I can seem to put all the pieces together

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

Rails EOFError (end of file reached) when saving a devise user

I'm getting this error in production when trying to create a user (i'm using the devise gem).
EOFError (end of file reached):
I hit this problem before and it was due to my smtp settings using zoho mail.
I believe my configuration below is what fixed the problem:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.zoho.com",
:port => 465,
:domain => 'example.com',
:user_name => 'user#example.com',
:password => 'password',
:authentication => :login,
:ssl => true,
:tls => true,
:enable_starttls_auto => true
}
Now we've added SSL to the site and I believe that is what is causing this error to occur now.
Does anyone have any insight into this error or zoho mail smtp settings with SSL?
This error was caused by not having my config/initializers/devise.rb specifying the correct email address for config.mailer_sender.
Also! I made this additional mistake and had the same issue: I used my own domain instead of the mail server domain for the "domain" variable.
Your environment variable should be:
GMAIL_DOMAIN=gmail.com
Or for the example above:
:domain => 'gmail.com',
I found one cause for the error here => https://stackoverflow.com/a/40354121/6264112
But this didn't solve my issue. While I wasn't getting any errors, my emails were still not working through Zoho so I found another solution that works perfectly for my needs...
1) Connect Zoho to gmail using SMTP. I setup my zoho email as an alias for my personal gmail account so zoho emails are forwarded to gmail and I can reply to them IN gmail FROM my zoho email address. This should be done anyways so you never have to login to zoho. Just do all emailing from gmail.
2) Connect ActionMailer to gmail account NOT zoho.
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:user_name => ENV["gmail_username"],
:password => ENV["gmail_password"],
:authentication => :plain,
:enable_starttls_auto => true
}
Now, I just need to specify the to and from values in the mailer like so:
def notify_admin (message_details)
#message_details = message_details
mail(to: "jesse#mydomain.com", subject: "Contact form filled out by: " + message_details[:name], from: message_details[:email])
end
This works when I want to send emails to myself as is the example above when someone submits the contact form.
It ALSO works when I want to send an email from my domain such as when they fill out the lead magnet. All I did was switch the to: and from: addresses.
Here's a working pony gem call.
Pony.mail({
:to => 'apotonick#gmail.com',
subject: "Pony ride",
body: "Awesome!",
from: "nick#trb.to", # this MUST be the sending Zoho email.
:via => :smtp,
:via_options => {
:address => 'smtp.zoho.com',
:port => '465',
:enable_starttls_auto => true,
ssl: true,
:user_name => 'nick#trb.to', # MUST be identical to :from.
:password => 'yourStrongPw',
:authentication => :login,
}
})
I had this issue, and I tried everything and still couldn't figure out what the issue was.
Let's face it, it's a SH!t message. What I did find though I was running my rails app locally with POW and its actually a POW error.
When I run rails server and do the same thing that caused the error, I actually got the real error message and was able to find I hadn't setup my controller correctly

Rails 2.3: SMTP settings for Postmarkapp: Connection refused - connect(2)

Does anybody have experience with Postmarkapp?
I have a rails 2 app (radiant cms) and try to send emails through SMTP.
This is how my smtp settings looks like:
config.action_mailer.smtp_settings = {
:address => "smtp.postmarkapp.com",
:port => '25',
:authentication => :plain,
:user_name => 'postmark-ap-key',
:password => 'postmark-ap-key',
:domain => 'postmarkapp.com'
}
The Mailer class:
class RegistrationMailer < ActionMailer::Base
def send_email(email, sent_at = Time.now)
subject "Some text here"
recipients "#{email}"
from 'xxx#yxz.com'
sent_on sent_at
body :text => "Some text here"
end
end
and here is the code where I call the deliver method (in a controller action):
mail = RegistrationMailer.create_send_email(params[:email])
RegistrationMailer.deliver(mail)
I got an 'Connection refused - connect(2)' error whenever I call the deliver method. Anybody can help me out what am I doing wrong? I used the exact same code on heroku with other smtp settings (for sendgrid) and it worked without any problems.
I haven't used Postmark myself, but there appears to be a gem to help you send mail through their system, it's probably because you have to send through an API key.
https://github.com/wildbit/postmark-rails
http://rubygems.org/gems/postmark-rails
Relevant question for implementation: How can I customize Devise to send password reset emails using PostMark mailer

Net::SMTPAuthenticationError in rails 3.1.0.rc5 when connecting to gmail

When ever time i try sending notifications in my rails app i get the following error
Net::SMTPAuthenticationError (535-5.7.1 Username and Password not accepted. Learn more at
):
app/models/friendship.rb:6:in `send_request'
app/controllers/friends_controller.rb:21:in `make_friendship'
my development.rb mail config settings is
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
# Gmail SMTP server setup
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:enable_starttls_auto => true,
:port => 587,
:domain => '#example.com',
:authentication => :plain,
:user_name => 'user#gmail.com',
:password => 'secret'
}
I have this and it works for me:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "name#example.com",
:password => 'password',
:authentication => "plain",
:enable_starttls_auto => true
}
Login to the account you're using in your browser then visit this page:
http://www.google.com/accounts/DisplayUnlockCaptcha
This gives you a 10 minute window to login with the app you want to let access your account. Go back to your Rails app and make it send an email, after that everything should work.
I have a similar configuration that works fine but once in a while I get this error and I suspect that it is because Google mark the account as potentially abusive for some reason, too fast logins etc (each time a mail is sent).
You can make it work again by manually login via web interface and type the CAPTCHA. If this happens often I would probably think about using some other solution, like using an own MTA or at least an local MTA between Rails and gmail capable of sending multiple mails without relogin. In that case you may even deliver the mail yourself without going thru gmail, just make sure to setup proper SPF records etc.
you are missing the link in the error message! :)
Net::SMTPAuthenticationError (535-5.7.1 Username and Password not accepted. Learn more at https://support.google.com/mail/bin/answer.py?hl=en&answer=14257
Thus for details see: https://support.google.com/mail/bin/answer.py?hl=en&answer=14257
Make sure that you've entered your full email address (e.g. username#gmail.com)
Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
I had the same problem: it worked from my desktop (in development environment), but it failed from production environment (a server in Sweden...).
You have to login into your gmail account and check the emails if Google has prevented the sign-in attempt.

Setting up sendgrid for rails..returning Authorization error

The emails now send from my local, but do not send from my box. I am returned this error. Anyone know what this might be?
Net::SMTPAuthenticationError (535 5.7.8 Error: authentication failed: authentication failure
):
My environments/production.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '25',
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "email#gmail.com",
:password => "password1234"
}
/etc/ssmtp/ssmtp.conf :
root=postmaster
mailhub=smtp.sendgrid.net
AuthUser=email#gmail.com
AuthPass=password1234
AuthMethod=LOGIN
rewriteDomain=mydomain.com
FromLineOverride=YES
UseSTARTTLS=NO
This usually happens because your account hasn't been provisioned.
By the way, I find that it's more favourable to use PostFix with SendGrid so there is less delay for the user when they are using your rails app. When I was using SMTP (like you have above), I was getting long delays when loading an action that sent mail.
See this SendGrid wiki page for more info:
http://wiki.sendgrid.com/doku.php?id=postfix
When registering to SendGrid it will be moderated by admin before you can send mail from new account.
See:
https://support.sendgrid.com/hc/en-us/articles/200181628
I had a similar issue with my test server and due to inactivity, the account was disabled. I created a Support ticket with SendGrid and it was reactivated.

Resources