Rails, transactional email and sender validation - ruby-on-rails

I would like to use Sendinblue to send transactional email from my Rails web application.
To configure my application to use Sendinblue I edited config/environments/production.rb as follows (NOTE: fireworks.com is just an example):
Rails.application.configure do
...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'fireworks.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp-relay.sendinblue.com',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDINBLUE_USERNAME'],
:password => ENV['SENDINBLUE_PASSWORD'],
:domain => 'fireworks.com',
:enable_starttls_auto => true
}
...
end
Do I need any gem, like sib-api-v3-sdk?
I suppose this gem is useful only for sending email using the Sendinblue API.
At Sendinblue I authenticated my domain 'fireworks.com' and created a sender, noreply#fireworks.com, which I would use for account activation.
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "noreply#fireworks.com"
layout 'mailer'
end
In order for me to use this sender, Sendinblue wants me to verify (or validate) it.
Now the point is that noreply#fireworks.com does not exist. Michael Hartl in his tutorial uses noreply#example.com, I would use noreply#fireworks.com because I own the fireworks.com domain.
Since I would not be able to validate the sender, I thought that the only solution would be creating in my server the noreply user account, configuring Postfix to receive email from Internet and adding an MX record to my DNS zone.
I would maintain this configuration until I receive the validation email.
Is this solution necessary? Do I have any other choice?

Now the point is that noreply#fireworks.com does not exist.
Before continuing, I want to point out that this is ultimately not a good practice. You'll want some address to receive and recognize bounces.
Having said that, I just checked their documentation and it says here that instead of validating individual email addresses, you can just validate the whole domain via DNS entry, file upload or by sending them an email.

Related

Configuring production.rb file to send emails with Rails 4

I am stuck with sending emails in Rails. I need to send password reset or activation account emails using Devise gem. Thanks Devise has email sending functionality builtin. I only need to configure sender. I have googled and found many tutorials on how to do that. Here are the things I still don't understand as many tutorials do not talk about them much:
Any tutorial comes with something like that:
config.action_mailer.default_url_options = { :host => 'gmail.com' }
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
# SMTP settings
ActionMailer::Base.smtp_settings = {
:port => 587,
:address => 'smtp.gmail.com',
:domain => 'gmail.com',
:user_name => ENV['username'],
:password => ENV['password'],
:authentication => :plain,
}
So unclear things for me are:
1) what is :host, can I use localhost?, in example it is gmail.com. Do I need to set up some gmail server or whatever.
2) what is :domain, again is it my site domain? or using just gmail.com is fine?
3) What is user_name and password?
So the general question is do I need to install some server for mail on my production server and so on, people on this tutorials skip this part. Who sends my email? Rails app server? or separate smtp server?
1) host is a variable used to generate links to your site in the email (see comment).
2) domain is the sending domain of the email, if you have your own domain you could put your domain there.
3) user name and password are the credentials of your gmail account (or the account that you send mails from). If you have your own smtp relay server set up you can use information for that.
This will likely only work for a minimal number of emails sent, you need to look into a professional service that delivers your emails if you intend on sending more than about 100 per month.

Error when sending email confirmation using ActionMailer on ROR

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.

Using SMTP through gmail to email via sendgrid using ruby on rails

Im doing a ruby on rails project for work and they would like to use sendgrid, but they also like gmail. With gmail it allows you to send an email from the web browser under a different alias but now also supports sending that through another smtp server instead of their own.
I was wondering if then, it would be possible to send an email from the RoR project through to gmail (so management gets to keep their nice interface and sent box), but then it would forward it through to the sendgrid SMTP servers. Just to clarify I know how to and currently can send an email through gmail as a different alias, but this is specifically to forward it through to sendgrid after it gets to gmail.
I currently have a standard setup of:
Myapp::Application.configure do
config.action_mailer.default_url_options = { :host => 'www.mygenericwebsite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:authentication => :plain,
:domain => 'mygenericwebsite.com',
:user_name => "user#mygenericwebsite.com",
:password => "pA55w0RD"
}
class UserMailer < ActionMailer::Base
default :from => "HappyAdmin <user#mygenericwebsite.com>"
You could send via Sendgrid and BCC the Gmail address in your emails, and then apply a label to emails from the app based on the From address. Not sure if you can apply the Sent label, but another label would probably be all right. I think this would be simpler and more robust than sending each email twice.
Just wanted to point out that our product, PostageApp, will allow you to send via the Google SMTP if you are so inclined. All you have to do is add the SMTP details to your project and you're good to go.
I just checked with the personal project I have hooked up with Postage and all the emails sent out appear in the Sent Mail folder.
Let me know if that's what you're looking for, or if you have any other questions!

Rails 3 - sendgrid setup to support devise

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.

send email from localhost

I'm try learn about email in rails. I'm developing something on localhost. Is it possible to send an email from localhost to say a normal mail account like gmail? Do I have a install a mail server? I've just got a standard rails installation at the moment for development.
Update for rails 4.0
Now you need these code to make it work:
# I recommend using this line to show error
config.action_mailer.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:domain => 'mail.google.com',
:port => 587,
:user_name => 'foo#gmail.com',
:password => '******',
:authentication => :plain,
:enable_starttls_auto => true
}
You can set up ActionMailer to use Gmail's SMTP server using something like this in config/environment.rb:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => 'smtp.gmail.com',
:domain => '<your domain>',
:port => 587,
:user_name => '<your gmail>',
:password => '<your password>',
:authentication => :plain
}
Edit: If you experience any difficulties, set your config to display errors:
ActionMailer::Base.raise_delivery_errors = true
Have a look at ActionMailer. In RAILS_ROOT/config/environment/ , there is a file for different environments (development, test, production) the configurable settings go in these files
You specify the delivery_method like this,
ActionMailer::Base.delivery_method = :sendmail
or if you want
ActionMailer::Base.delivery_method = :smtp
A detailed example of the settings has been posted by Mikael S
HTH
If I understand your situation correctly, you want to send an email from your local computer using a custom email address such as john#mycompany.com. If you already registered the domain name for your email account ( mycompany.com ) is very likely that the company that is hosting your website, also has a POP/SMTP server. If so, you can use Mikael S's sample and change the address parameter to your Hosting company's smtp address and use your hosting company's username/password.
If you have not register your custom domain or don't have a hosting provider, you can install a free email server in your local computer. If you use WindowsXP, you can add the IIS email server by going to add/remove programs->windows features. If you are using Linux, you can use any of the email servers available in the repositories. Once you install your local email server you will use Mikael S's sample code and use 127.0.0.1 or localhost in the address field. If you are using WindowsXP's email server, I think you don't have to enter username/password.
Hope it helps you.
You can send it from localhost, you can even set the sender as a 'real' mailbox e.g. you#gmail.com.
However, some (or say most) servers will not accept this mail as part of their spam blocking strategy (inability to verify the sender identity).
However, In the past, I have had something similar with python which worked on gmail.
so good luck ;-)

Resources