send email from localhost - ruby-on-rails

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 ;-)

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.

Rails Devise Mailer: No Received Messages in Inbox

I'm using Rails 4 and Devise 3. I need to send confirmation e-mails for production. These are the SMTP configs for my config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'smtp.gmail.com' }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:tls => true,
:port => '587',
:user_name => 'my_email#gmail.com',
:password => 'my_password',
:authentication => 'plain',
:enable_starttls_auto => true
}
Logs say that the e-mail's been sent. However, I don't see anything in the inbox. (yes, mailcatcher is off)
Another question, do the configs of the development file affect the production's environment's in any ways? They shouldn't, correct?
Another important question: Using the way above, how many e-mails can be handled? For an example, if I used a third party say, Mandrill, would be better because up for tens of thousands of e-mails can be handled. What about this way?
P.S I've already tried Mandrill and it worked just fine. I am requested not to use a third party though so I won't be able to use Mandrill.
Lastly, is there any other way of sending the confirmation e-mails from the Rails Devise that I'm unaware of yet? Or are there any other configurations that I need to do OUTSIDE OF RAILS to make this work since I won't be using a third party?
Please make sure you have enter correct host name .
config.action_mailer.default_url_options = { :host => 'your domain name' }
As Rails configuration standard if your application running on local machine it loads development env. file settings, if it is production then it load production env. file settings.
I prefer to use sendgrid or Mandrill by MailChimp, if you ave large application then it is better to use 3rd party addons, for small application you can use Gmail .
hope this will help you :)

Net::SMTPAuthenticationError when sending email from Rails app (on staging environment)

I am sending email from my Rails application. It works well on development environment, but fails on staging. I get the following error:
Net::SMTPAuthenticationError (534-5.7.14 <https://accounts.google.com/ContinueSignIn?plt=AKgnsbtdF0yjrQccTO2D_6)
Note, that my I don't have a domain name for my staging.
Here are my settings in staging.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "my.ip.addr.here:80" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'my.ip.addr.here:80'
:user_name => "my_email_name#gmail.com",
:password => "my_email_password",
:authentication => 'login'
}
Please, help.
Edit.
After adding :tls => true option I get
OpenSSL::SSL::SSLError (Unrecognized SSL message, plaintext connection?)
And then I changed port to 25 and now I get this (with 30 seconds delay):
Timeout::Error (execution expired)
I had the same problem: emails were sent from development, but not from production (where I was getting Net::SMTPAuthenticationError).
This drove me to conclusion that the problem was not with my app's configuration, but with Google.
Reason: Google was blocking access from unknown location (app in production)
Solution: Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue (this will grant access for 10 minutes for registering new apps).
After this my app in production started sending emails ;)
Solved!
I simply changed password for my gmail account and somehow errors disappeared.
After dozen of changes, the final settings I ended up with are:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "my.ip.addr.here" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'my.ip.addr.here:80',
:user_name => "my_email_name#gmail.com",
:password => "my_email_password",
:authentication => :plain,
:enable_starttls_auto => true
}
This solution is working for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'localhost:3000',
:user_name => "xyz#gmail.com",
:password => "password",
:authentication => :plain,
:enable_starttls_auto => true
}
It's true that Google will block your sign-in attempt but
You can change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.
go to following link and turn on
https://www.google.com/settings/security/lesssecureapps
The above solution provided the correct settings (which I already had) but did not solve the issue. After continued attempts, I kept getting the same error. Turns out, I had to "clear the CAPTCHA" from the web. See the gmail documentation for details.
You can also jump right to the "clear the CAPTCHA" page here.
I had the same problem.
Solution:
You can turn on less secure apps option (at here).
https://myaccount.google.com/lesssecureapps
And unlock Captcha (link):
https://accounts.google.com/DisplayUnlockCaptcha
A lot later but just in case it helps anyone.. Just called Google Apps Help Center and they instructed to change the lesssecureapps setting (as everyone else) but also to change the port to 465.
In my case, that did the trick!
To resolve this issue:
If you see: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.), then you need to allow less secure apps to login your google account.
To enable less secure apps login follow: https://myaccount.google.com/lesssecureapps?. But will allow all apps to login.
If you want to customize it refer : https://support.google.com/a/answer/6260879?hl=en
Then may be possible you will get Net::SMTPAuthenticationError (534-5.7.14), so to resolve this refer: pli=1http://www.google.com/accounts/DisplayUnlockCaptcha. After that click the continue from the page you get redirected. It will verify your Captcha and your app will get verified to use your google account to send emails.
NOTE: Please make sure you'r using correct credentials of your gmail account.
If you'r not willing to allow all the apps please refer: https://support.google.com/a/answer/6260879?hl=en. From the link go to Use alternatives to less secure apps, this will guide you to an alternative way to Allow Less Secure apps access to your google account.
The accepted answer seems very old, I don't know if at that time the followin (better) solution was existing:
Go to https://myaccount.google.com/u/0/apppasswords
Generate a new password for you app
Replace the personal password with the generated password in config.action_mailer.smtp_settings
Now, sending emails works perfectly!
Hello this also worked for me and so if someone is still having a problem try this out.
Make sure you have figaro in your gemfile.
To save sensitive information such as username and password as environment variables
gem 'figaro'
And in your config/environments/development.rb , paste the codes below
using smtp as method delivery
config.action_mailer.delivery_method = :smtp
SMTP settings for gmail
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
}
config.action_mailer.default_url_options = { host: "locahost:3000" }
In your config directory create a file called application.yml
and add the codes below.
gmail_username: 'example#gmail.com'
gmail_password: "your_example_email_password_here"
You must use your email and password for authentication in the file.
I also faced the problem, and after some research in Gmail setting, I found the solution:
In gmail, go to settings.
Select "Forwarding and POP/IMAP" tab.
In the IMAP access section, select "Enable IMAP".
I had the same problem and after some trial and errors, have come to this resolution which is an option to be enabled in google:
Click https://www.google.com/settings/u/0/security/lesssecureapps
Enable 'Access for less secure apps' here by logging in with the email address you have provided in smtp configuration.

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.

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!

Resources