Heroku actionmailer how to - ruby-on-rails

I am trying to create an email adress. I have added the sendgrid plugin to my app.
Here is my application.rb
module Konkurranceportalen
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# all .rb files in that directory are automatically loaded.
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.mydomain.com",
:port => 25,
:user_name => "mail#mydomain.com",
:password => "mypass",
:authentication => :login
}
end
end

You need to change your settings for Sendgrid:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => "25",
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN']

To create an regular email like mail#yourdomain.com you need an Mail Exhange host.
When you have choicen your mail exhange host you can create emails like mail#yourdomain.com
Now you need to setup the Mx record on you DNS server
And vola you have a email like mail#yourdomain.com

that doesn't look like the code you need to use SendGrid with on Heroku - the docs have all the details you need, here

Related

Ruby on Rails and Sendinblue via SMTP

I would like to use Sendinblue to send transactional email from my Ruby on Rails web application via SMTP.
I edited config/environments/production.rb as follows:
ActionMailer::Base.smtp_settings = {
:address => 'smtp-relay.sendinblue.com',
:port => '587',
:authentication => :plain,
:user_name => ???,
:password => ???,
:domain => 'fireworks.com',
:enable_starttls_auto => true
}
What am I expected to use as user_name and password? My account's username and password or my SMTP keys? Also, am I required to use any gem, like sib-api-v3-sdk, or this gem is useful only for sending email using the Sendinblue API?
This is all you need to use SendInBlue with ActionMailer. Other answers suggest to use gems that are not required for SMTP. You DO NOT need the sendinblue or sib-api-v3-sdk gems to use SIB with ActionMailer in a plain vanilla Rails app.
#config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => ENV.fetch('SMTP_HOST', 'smtp-relay.sendinblue.com'),
:port => ENV.fetch('SMTP_PORT', '587'),
:authentication => :plain,
:user_name => ENV['SMTP_USERNAME'], #See: https://account.sendinblue.com/advanced/api
:password => ENV['SMTP_PASSWORD'], #See: https://account.sendinblue.com/advanced/api
:enable_starttls_auto => true
}
Add this to your gemfile
gem 'sib-api-v3-sdk'
Add this to config/environments/production.rb
config.action_mailer.default_url_options = { host: "your_domain.com", port: 587 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp-relay.sendinblue.com",
:port => 587,
:user_name => ENV['SEND_IN_BLUE_USERNAME'],
:password => ENV['SEND_IN_BLUE_PASSWORD'],
:authentication => 'login',
:enable_starttls_auto => true
}
Add this in in config/initializers/send_in_blue.rb
SibApiV3Sdk.configure do |config|
config.api_key['api-key'] = ENV["SEND_IN_BLUE_API_KEY"]
end
Make sure your environment variables are correct. It works for me in production.
can add gem 'sendinblue'Official Sendinblue provided API V2 Ruby GEM
In order to send an email, you need to change the smtp settings in config/environments/*.rb(Whichever applicable)
Rails.application.configure do
#append this settings
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => ‘smtp-relay.sendinblue.com’,
:port => 587,
:user_name => ‘YOUR_SENDINBLUE_EMAIL’,
:password => ‘YOUR_SENDINBLUE_PASSWORD’,
:authentication => ‘login’,
:enable_starttls_auto => true
}
end
change STMP & API in Your setting account
STMP SERVER --> smtp-relay.sendinblue.com
port --> 587

Does Sendgrid require mail gem and how to set it up with action mailer?

Heroku docs says:
https://devcenter.heroku.com/articles/sendgrid#ruby-rails
To send out emails through SendGrid, you need to configure the Mail class to have the correct values:
require 'mail'
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.sendgrid.net',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
end
Neither the Mail gem docs or heroku specify clearly where the Mail class should be and how to configure it.
In the past I have just used Action Mailer something like this
class UserMailer < ActionMailer::Base
default from: 'careerswitch.me#gmail.com'
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.signup_confirmation.subject
def signup_confirmation(user,subject_param='Welcome!')
#user = user
mail to: user.email,
subject: subject_param do |format|
#format.text {render __method__}
format.html {render __method__}
end
end
end
Any good example on what has changed and how do I setup sendgrid on Herouku/Rails4.1?
Heroku provide add on for sendgrid.
https://elements.heroku.com/addons/sendgrid
By adding this it will automatically set environment variables for you i.e
SENDGRID_USERNAME
SENDGRID_PASSWORD
After provisioning the add-on you have to put this into production.rb
ActionMailer::Base.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'appname.herokuapp.com',
:address => "smtp.sendgrid.net",
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
Mailer class will remain same as normally used.
These are initializer settings. There should be a generator for that, but I think there isn't. At least last time I did it manually.
Go to yourappdirectory/config/initializers
Create a file named mail.rb
Paste the code above there
That's all. Once you configure it, you'll be good to go.

Failed to send mail on Rails but works on simple ruby script

The following code can send mails perfectly,
But not works on Rails.
content = "sample_content"
smtp = Net::SMTP.new("mail.#{#domain}", 25 )
smtp.enable_starttls
smtp.start( #domain, #user_name, #passwd, :login) do |smtp|
smtp.send_message content, #sender, [#receiver]
end
Here's my settings on development.rb
config.action_mailer.smtp_settings ={
:address => "mail.#{ENV['domain']}",
:domain => ENV['domain'],
:port => '25',
:authentication => :login,
:user_name => 'ENV["username"]',
:password => 'ENV["passwd"]',
:enable_starttls_auto => true
}
Error message on console
Net::SMTPFatalError: 550 5.7.1 Client does not have permissions to send as this sender
It looks like that you are using a different account in the from in your emails than the account that you are using in your smtp_settings
To have this clearer:
Imagine that your smtp_settings are
config.action_mailer.smtp_settings ={
:address => "mail.#{ENV['domain']}",
:domain => ENV['domain'],
:port => '25',
:authentication => :login,
:user_name => 'me#example.com', ######## this is the important part of the explanation
:password => 'ENV['passwd']',
:enable_starttls_auto => true
}
and then in your mail class:
def greet
mail(from: 'you#example.com', and: 'other params')
end
Then the mail server will answer with that.
It is possible to setup you email server so that one account can send emails as a different one.
If this is not possible for you, may be you can put all you mail config into an yml file and load it on the fly before sending the emails.
Having different mail classes subclassing ActionMailer::Base is another option too.

sendgrid addon on heroku and devise

Please help! I can't seem the get the sendgrid addon to work, and I have devise. Is there a way just to use a devise as a sender of e-mails, if so, then how? Or what am I doing wrong:
Here is my significant config/initializers/devise.rb code:
Devise.setup do |config|
# ==> Mailer Configuration
#config.mailer_sender = "myapp.herokuapp.com"
# Configure the class responsible to send e-mails.
#config.mailer = "Devise::Mailer"
Do I have to uncomment this if i want to use sendgrid?
In enviroment/production.rb I have:
config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com' }
Have you done the basic Sendgrid configuration?
https://devcenter.heroku.com/articles/sendgrid
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
}

Getting Devise 1.3.4 to send emails with Gmail in development

I'm trying to setup devise 1.3.4 to send emails via gmail while in development mode. I should mention that I'm using Rails 3.0.4 and Ruby 1.9.2p136.
I've tried the following in config/environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mydomain.com",
:user_name => "info",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
And in config/initializers/devise.rb I changed
config.mailer_sender = "please-change-me-at-config-initializers-devise#example.com"
To
config.mailer_sender = "info#mydomain.com"
Then I tried
http://yekmer.posterous.com/devise-gmail-smtp-configuration
It's still not working.
Is there a wiki page on how to get the mailer working? I see the email in my log and it looks great! The links work, etc ... I just want to see them in my email account.
Edit
I found the answer -
I used http://yekmer.posterous.com/devise-gmail-smtp-configuration - I had been putting that code in config/intializers/devise.rb when I should have been putting it in config/environments/development.rb.
Have you tried this?
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "myinfo#gmail.com",
:password => "secret",
:authentication => "plain"
# :enable_starttls_auto => true # I don't have this, but it should work anyway
}
--------- EDIT
it it's sent maybe you don't receive it because of the spam filter, first thing to check:
class UserMailer < ActionMailer::Base
default :from => "myinfo#gmail.com"
# ...
end
you should put that in devise initializer :
# Configure the class responsible to send e-mails.
config.mailer = "YourAppDeviseMailer"
Then create a class that extends Devise::Mailer :
class YourAppDeviseMailer < Devise::Mailer
default :from => 'your_email'
def self.mailer_name
"devise/mailer"
end
end
I think you can change it inside config/initializers/devise.rb. No need for a new class I think?
#config/initializers/devise.rb
config.mailer_sender = 'youremail#gmail.com'
Check if the value of ActionMailer::Base.delivery_method is :smtp

Resources