Sending email through Rails 3 with gmail problems - ruby-on-rails

I am getting this error whilst trying to send emails through gmail -
Net::SMTPAuthenticationError (530 5.7.0 Must issue a STARTTLS command first. pw17sm4922458lab.5
):
app/controllers/contact_controller.rb:11:in `create'
I have tried loads of different things but to no avail, below is my settings from production.rb
# Change mail delvery to either :smtp, :sendmail, :file, :test
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "bizmodev.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: '******.*****#gmail.com',
password: '********'
}
# Specify what domain to use for mailer URLs
config.action_mailer.default_url_options = {host: "bizmo.co.uk"}
Any help on this matter would really be appreciated.

OK fixed it, basically I installed sendmail on my VPS and then restarted Apache and it now works -
Ubuntu Sendmail command line install
apt-get install sendmail
Hope this may help someone in the future...

you can enable starttls like this:
config.action_mailer.smtp_settings = {
....
:enable_starttls_auto => true}

Related

how to send mail through the localhost?

please help solve the problem.
i use webrick server on localhost. in development.rb i registered:
config/enviroments/development.rb:
Rails.application.configure do
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
i use gem devise. and i user 'forgot youre password'-function (devise-recoverable module). the result did not get to the email message. but where can I find a mark(log) that was an attempt to send a letter? whether there is a directory where they are stored?
You will need to edit your config/environments/development.rb file.
Here's something to get you started, add this to that file:
# port 3000 or whatever port you are using for your localhost
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
# this line is what you want to be true, else you won't get messages!
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'gmail.com' # or to whatever domain your email is
authentication: "plain",
enable_starttls_auto: true,
user_name: 'my_gmail_or_other_username',
password: 'my_gmail_or_other_password'
}
Now, when you are moving towards production or even just pushing it to a public repository you obviously don't want to hardcode the username and password (for security purposes). For this I suggest you look up how to use environmental variables. The Figaro gem might also come in handy here and is another option.
This is the default 'smtp' way, there are other methods, such as the use of a gem as specified by the earlier answer.
try gem mailcatcher for development purpose.
http://mailcatcher.me/
github: https://github.com/sj26/mailcatcher
This gem will provide you a tab in browser where all your emails will be there unless you delete them.

ArgumentError in Devise::RegistrationsController#create SMTP-AUTH requested but missing user name

I have problem with sending emails from localhost.
it was working on linux and now I'm trying to run my app on mac and it doesn't work already.
I have an error:
ArgumentError in Devise::RegistrationsController#create
SMTP-AUTH requested but missing user name
in config/environments/development.rb:
#Action Mailer config
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
# Send email in development mode.
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: "587",
domain: "mail.google.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USER_ID"],
password: ENV["GMAIL_PASSWORD"]
}
My env variables are good, I tried put my credential into code as well and it still doesn't work :/
I checked similiar topics but didn't found the solution.
What can I try?
I read:
I tried put my credential into code as well and it still doesn't work :/
But have you tried replacing your var env with your gmail id/password like this ?
#Action Mailer config
**********
user_name: "my_username",
password: "my_password"
}
Just to be sure.
Ok, solved.
credentials didn't work because I havn't restarted my server (stupid laziness)
my env vars didn't work because I've puted them into ~.profile but it work in ~.bash_profile, that was helpful (I use OS X Yosemite).

rails, heroku devise action mailer not working - how to debug?

i am trying to set up confirmable with heroku devise for my app. here is my code: (followed a few tutorials)
config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com' }
Rails.application.routes.default_url_options[:host] = 'myapp.herokuapp.com'
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
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: ENV["GMAIL_DOMAIN"],
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
i've added the heroku variables for the password/domain/username. but when i try to sign-up on my live site, i get an error.
does anybody see any errors? or can someone point me on how I can debug this? thanks!
Heroku & Gmail have some authentication token problem while sending e-mail.
It is better to use sendgrid or Mandrill by MailChimp
They both are free & configuration is very easy

Redmine email configuration with environment variables

I've configured email on redmine according to the instructions in the redmine wiki, and it all works fine. Here are the contents of my config/configuration.yml file:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: my_email#gmail.com
password: my_password
However I am trying to use environment variables in place of my_email#gmail.com and my_password like so:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: <%= ENV['SENDGRID_USERNAME'] %>
password: <%= ENV['SENDGRID_PASSWORD'] %>
When I try to send a test email, with the environment variables in the config file, redmine give this error:
'An error occurred while sending mail (535 Authentication failed: Bad username / password )'.
So I guess the erb snippet is not being evaluated (I have double checked the values of the environment variables).
I've searched for a solution and come up empty, does anyone have any suggestions as to how I should configure email in redmine so that I don't expose my sendgrid credentials in the config file?
Alternatively if someone can tell me that it's not a security risk to use the credentials directly, without environment variables, that would also solve my problem.
I had answered this question 2 weeks ago, and it was deleted by someone else right away.
So I'm not sure if someone will delete this answer again to prevent other guys knowing how to solve this problem. Good luck!
I got the same issue and this article
Set up mailing in redmine on heroku solved my problem.
The idea is moving the settings from config/configuration.yml to config/environments/production.rb and using Ruby code to set it up. Since ENV['key'] is handled by erb, I guess configuration.yml is not handled that way. Maybe there is some security issue?
So in your case, you should add these codes to config/environments/production.rb as follows,
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
And remove codes from config/configuration.yml and make it looks like this,
default:
# Outgoing emails configuration (see examples above)
email_delivery:
delivery_method: :smtp

Rails mailer Net::SMTPAuthenticationError (530-5.5.1 Authentication Required) with Gmail using ENV[USERNAME] and ENV[PASSWORD]

I'm getting an error when sending out mail from the Production environment, specifically:
Net::SMTPAuthenticationError (530-5.5.1 Authentication Required)
On my local machine and development environment I'm declaring the passwords using ENV[GMAIL_USERNAME] and ENV[GMAIL_PASSWORD] and it's working fine, where the declared ENV is stored in my .bash_profile
# Development Environment for Action Mailer config
config.action_mailer.default_url_options = { host: '0.0.0.0:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mydomain.com',
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
# Production Environment for Action Mailer config
config.action_mailer.default_url_options = { host: 'mydomain.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "mydomain.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
Unfortunately, in Production I get an error when sending mail. However, if I hardcode the username and password in environments/production.rb it works. The ENV["GMAIL_USERNAME"] and `ENV["GMAIL_PASSWORD"] is set in the .bash_profile:
export GMAIL_USERNAME="hello#mydomain.com"
export GMAIL_PASSWORD="mypassword"
I thought it would it was the app unable to call ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"] but when I jump into the production rails console and puts ENV["GMAIL_USERNAME"] it outputs the correct credentials.
I have restarted Apache and restarted my app multiple times but I'm puzzled what to do next.
Any help is much appreciated.
Thank you in advanced.
Whose bash profile? Your users? Apache won't read those profiles on startup as it doesn't run bash to start the server.. Your profile gets sourced when you login to the server. Which is why it works when you test.
You need to make sure that environment variables are defined in the script or the environment that apache runs under. Depending upon your server this could be in /etc/apache2/envvars or added to the startup script for apache.

Resources