set up SendGrid on Rails app - ruby-on-rails-4

I'm using sendgrid and action mailer to send email from my rails app which is deployed on Heroku. My emails are being delivered but I'm not seeing any email activity in Sendgrid. The open tracking app is enabled.
I'm wondering if I've set up the app properly? I'm using the sendgrid-ruby gem and I've set
config.action_mailer.default_url_options = { host: ENV["DOMAIN"] }
and
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
}
Is there anything else I need to do?

SendGrid was experiencing issues with their Email Activity yesterday (https://support.sendgrid.com/hc/en-us/articles/205074437-16-Mar-Email-Activity-Unavailable-)
I would contact their support team (support#sendgrid.com) and have them take a look.

Related

Custom SMTP setting from database in ruby on rails?

Usually in rails we can config the smtp settings like:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "anythingworks",
:user_name => "mygmailaddress#gmail.com",
:password => "somePW",
:authentication => "plain",
:enable_starttls_auto => true
}
But I need several connections from database value to be set as smtp and also if no value in database the default smtp settings should be used.
How can I achieve this ?
Just set it based on the database value, e.g.:
ActionMailer::Base.smtp_settings = {
:address => Setting.find_by(key: :smtp_address)&.value || 'smtp.gmail.com',
:port => 587,
:domain => "anythingworks",
:user_name => "mygmailaddress#gmail.com",
:password => "somePW",
:authentication => "plain",
:enable_starttls_auto => true
}
This assumes you have a database table called "settings" with key and value columns so you can set any generic config in it, one config item per record. And an ActiveRecord class Setting to access it.
It will fall back to the hard-coded value if the setting isn't present or the value is null.
The other thing is to make sure this config code is established at a point in the boot cycle when ActiveRecord is already present. You could put it in config/initializers/mail.rb for example.

Auto login in Power BI with REST API with cron task

I want auto login in Azure portal for access_token so that I can send data to Power BI dataset . From cron task in Rails App currently I am using
OAuth2::Client
and
OAuth2::AccessToken for generating token.
I add username, password , grant_type => 'password' and scope => 'openid' to OAuth2::Client but it is not working something is missing
I got this. Now I am using rest_client instead of Oauth2
request = RestClient.post( url,
{
:grant_type => 'password',
:scope => 'openid',
:resource => 'https://analysis.windows.net/powerbi/api',
:client_id => 'CLIENT_ID',
:client_secret => 'CLIENT_SECRET',
:username => 'USERNAME',
:password => 'PASSWORD'
})

Net::SMTPAuthenticationError, 535-5.7.8 Username and Password not accepted

I know this question as been answered a lot of times, but I still can't figure what's wrong with my parameters.
I've modified my gmail account so it allows less secured apps, unlocked it with a captcha, but it still considers that my username and password are not accepted, even though i'm sure about them for I checked them 3 times already
Here are my files :
config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "mymail#gmail.com",
:password => "mypassword",
:authentication => :login,
:enable_starttls_auto => true
}
config/initializers/smtp_settings.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mywebsite.fr",
:user_name => "myemail#gmail.com",
:password => 'myPassword',
:authentication => :login,
:enable_starttls_auto => true
}
I guess I've done something wrong, or put something somewhere it doesn't belong, but I've seen so many different ways to do it that I'm lost
Thank you in advance
I am a beginner and this is the first time I am posting on stackoverflow and I hope this helps you out. Please let me know if I can help you further! Also if you want to use sendgrid and heroku I can help with that also.
So when it comes to my gmail authentication settings I also had to allow less secure apps, and unlock the captcha. Along with these two changes in gmail settings, I also had to go to my gmail settings>Forwarding and POP/IMAp> then click ENABLE IMAP. Save your changes. These are all the changes I mad to my gmail.
Make the following changes:
config/initializers/setup_mail.rb
change authentication to :plain
if Rails.env.development? || Rails.env.production?
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
authentication: :plain,
user_name: 'yourEmail#gmail.com',
password: 'yourSecureGmailPassword',
domain: 'mail.google.com',
enable_starttls_auto: true
}
end
config/environments/test.rb host is a website because i am using cloud9
config.action_mailer.default_url_options = { host: 'https://blocipedia-sunnygoo.c9users.io', port: 3000 }
config/environments/development.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: 'same as in test.rb', port: 3000 }
app/models/user.rb
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: "sunnydgoswami##gmail.com"
def new_user(user)
#user = user
mail(to: user.email, subject: "Welcome to Blocipedia!")
end
end
app/views/layouts/mailer.html.erb
<html>
<body>
<%= yield %>
</body>
</html>
app/views/layouts/mailer.text.erb
<%= yield %>
app/views/user_mailer/new_user.html.erb
'<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-
equiv="Content-Type" />
</head>
<body>
<h1>Welcome, new user!</h1>
<p>
<%= #user.email %>, join us in creating a vibrant wiki
atmosphere!
</p>
<p>
You signed up using <%= #user.email %>. You will be
using this email next time to log in.
</p>
</body>
</html>'
"i have tried everything on stackoverflow but didnot work so i found this solution its the correct way i hope it helps "
To Solve smtplib.SMTPAuthenticationError: Username and Password not accepted Error You need to do Is Just Create App Password and then Use it and you'll be able to use SMTP. Just follow the below step to Create App Password. First of all Login into Your Gmail Account. And then Go To MyAccount Section By visiting https://myaccount.google.com Then Open Security Tab in the Sidebar As Shown in the Image. Then You can see There is Signing in to Google section and Make Sure you have turn on two steps verification if Not Then Turn On two steps verification. When You Turn On Your 2-Step Verification then you'll be able to see App Passwords option. And Now Click on App Passwords. Then select app as Mail and select your corresponding device(if you are on window select window mail).
Then Click on Generate to create App Password. copy the password.
replace your gmail password with this app password in your smtp
Now your app Password is been created. Now Use this password in Your SMTP.
Just Use this Password in SMTP And Now, Your error must be solved. Thank You.

Rails 4 Sendgrid integration giving error - Unauthenticated senders not allowed

I have integrated Sendgrid settings on a Rails 4 server. These settings work fine for development environment. But this is giving error on production environment.
Net::SMTPFatalError (550 Cannot receive from specified address <simmi#mydomain.com>: Unauthenticated senders not allowed)
config/initializers/email_setup.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:domain => DOMAIN,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true
}
config/initializers/devise.rb
config.mailer_sender = 'simmi#mydomain.com'
config/environments/production.rb
# Default URL
config.action_mailer.default_url_options = { host: 'mysite.mydomain.com' }
DOMAIN = 'mysite.mydomain.com'
According to sendgrid support team, this error comes when username or password are incorrect. I tried logging manually into the smtp server through telnet and it was working.
On my server commandline, I followed these steps:
telnet smtp.sendgrid.net 587
EHLO
AUTH LOGIN
Enter username in Base64
Enter password in Base64
Link to convert text into Base64 - http://www.opinionatedgeek.com/dotnet/tools/base64encode/
The ENV variables were somehow not working on my production environment. As a workaround, I tried adding the username and password directly and it worked.
I have also faced the same problem and fixed it by adding the following:
config/environment.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:domain => DOMAIN,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options = { host: 'mysite.mydomain.com' }
config/application.rb
ActionMailer::Base.delivery_method = :smtp
The letter_opener gem is very useful if you want to test sending emails in development mode.
If you want to overwrite the letter_opener, add the following configuration
config/environments/development.rb
ActionMailer::Base.delivery_method= :letter_opener
And also add the port under ActionMailer::Base.smtp_settings.
You are probably loading your environment variables after you are trying to initialize your mailer. You can do the initialization directly after loading your variables to be sure that they exist.
Set up a config file with your username and password variables:
# config/mailer.yml
production:
SENDGRID_USERNAME: 'username'
SENDGRID_PASSWORD: 'password'
Set up an initializer file:
# config/initializers/mailer.rb
if Rails.env.production?
config_path = File.expand_path(Rails.root.to_s + '/config/mailer.yml')
if File.exists? config_path
ENV.update YAML.load_file(config_path)[Rails.env]
end
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:domain => "yourdomain",
}
end
If your production environment is Heroku:
Login to your Heroku account and select the application. Under "Settings", click the "Reveal Config Vars" button. Enter in your sendgrid key and value pairs, then submit. Run: heroku restart.

553 5.7.1 <mydomain>: Sender address rejected: not owned by user admin#mydomain.com

In a rails4 application, I am trying to send emails by configuring smtp settings but I am getting the following error:
553 5.7.1 : Sender address rejected: not owned by user
admin#mydomain.com
I am using the following settings:
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.mydomain.com",
:port => 25,
:domain => "mydomain.com",
:user_name => "admin#mydomain.com",
:password => "CorrectPassword",
:authentication => :plain,
:enable_starttls_auto => true,
:openssl_verify_mode => 'none'
}
config.action_mailer.default_url_options = {:host => "mydomain.com"}
The same works when I use my gmail account settings. I got my domain from bigrock.com and all MX Records are verified. I am able to send emails manually (by logging in webmail.mydomain.com). This issue has delayed the release of my site by quite a few days. Please Help!
As #marc-b commented,
The mail server is rejecting your email because you're trying to send from a username which differs from the username you're logging in as, e.g. you've set the From: to be no-reply#mydomain.com, which is obviously not the same as admin#mydomain.com
And the solution was:
.. You are right. ... I changed the "from" field's value to same as user_name.