Ruby on Rails ActionMailer SMTP Settings - ruby-on-rails

trying to set up my own ActionMailer::Base in RubyRails -> This works:
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
}
fine.. but it would be nice if I could use my mail server. but when i am trying this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.myowndomain.de",
:port => 25, (tried 587) also
:domain => "triedanything",
:user_name => "mycorrectuser",
:password => "yesitisthecorretpw",
:authentication => "plain",
:enable_starttls_auto => true
}
Tried lot of different settings / changes. But no E-Mail is sent. Is there any logs avaiable to this ? Think its something because authentication... "login" instead of "plain" doesnt help...
Had some tests with telnet sending some E-mails from command line.
gmail doesnt work from command line (saying STARTTTLS or something). My own domain mails server (which is hosted from some provider) says "relay access denied" (from commandline).
Webmail both working..
Any ideads ? Thanks!

The answer is the "relay access denied" message. Your smtp server needs to be configured to relay emails for this to work. I suspect that your smtp server is configured to accept connections for delivery TO your domain, not to relay emails elsewhere.
The same error message will probably be found in the smtp server's log files. Try sending an email to yourself (which would just be an accept, rather than a relay, for your smtp server). If emails to yourself are delivered, but emails to other domains are not, you will have confirmation that that is the problem.
Your ISP probably has an smtp server you can use for outgoing emails. If you want to use your own email server, it will need to be configured to relay emails.

Related

Sendgrid set up on Rails 4

I have a rails 4 app. I set up ActionMailer and I can send order confirmation emails via localhost and gmail.
I installed Sendgrid on Heroku and followed the set up instructions. I get a Net::SMTPSyntaxError (501 Syntax error
my environment.rb (i have sendgrid user/pwd in application.yml)
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
}
in production.rb - the only actionamailer setting i have is this. I have this as a placeholder to put the real domain in later. I'm currently using herokuapp.com.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
in my orders_controller within the order create method, I call the below.
AutoNotifier.orderconf_email(current_user, #order).deliver
auto_notifier.rb
class AutoNotifier < ActionMailer::Base
default from: "Test Email"
def orderconf_email(current_user, order)
#buyer = current_user
#order = order
mail(to: #buyer.email, subject: 'Thank you for your order.')
end
end
What am I missing? It works on localhost with gmail so I'm missing something in the sendgrid settings or in the default_url in production.rb file.
For posterity, here's a working setup for external SMTP in Rails on Heroku:
#config/environments/production.rb
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
:authentication => 'login',
:domain => 'yourdomain.com', # your domain to identify your server when connecting
}
Change default from: "Test Email" to valid email address, even example#example.com.
I would just like to point out, this is for sending emails via SMTP. While this method is totally ok, you should also consider sending via the API.
To do this, you need to specify an interceptor. Luckily, there's a Gem that helps with that. Here's a good article showing how to use it.
https://rubyplus.com/articles/561-Sending-Emails-using-SendGrid-API-in-Rails-4-1
It took us a long time to resolve the issue when we tried to deploy the SMTP relay on heroku. It worked perfectly fine on local but when pushed we received socket errors and time out issues. Eventually got it working.
Important note: Make sure not to use starttls_auto and SSL/or TLS this causes open SSL issue.

Rails EOFError (end of file reached) when saving a devise user

I'm getting this error in production when trying to create a user (i'm using the devise gem).
EOFError (end of file reached):
I hit this problem before and it was due to my smtp settings using zoho mail.
I believe my configuration below is what fixed the problem:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.zoho.com",
:port => 465,
:domain => 'example.com',
:user_name => 'user#example.com',
:password => 'password',
:authentication => :login,
:ssl => true,
:tls => true,
:enable_starttls_auto => true
}
Now we've added SSL to the site and I believe that is what is causing this error to occur now.
Does anyone have any insight into this error or zoho mail smtp settings with SSL?
This error was caused by not having my config/initializers/devise.rb specifying the correct email address for config.mailer_sender.
Also! I made this additional mistake and had the same issue: I used my own domain instead of the mail server domain for the "domain" variable.
Your environment variable should be:
GMAIL_DOMAIN=gmail.com
Or for the example above:
:domain => 'gmail.com',
I found one cause for the error here => https://stackoverflow.com/a/40354121/6264112
But this didn't solve my issue. While I wasn't getting any errors, my emails were still not working through Zoho so I found another solution that works perfectly for my needs...
1) Connect Zoho to gmail using SMTP. I setup my zoho email as an alias for my personal gmail account so zoho emails are forwarded to gmail and I can reply to them IN gmail FROM my zoho email address. This should be done anyways so you never have to login to zoho. Just do all emailing from gmail.
2) Connect ActionMailer to gmail account NOT zoho.
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
}
Now, I just need to specify the to and from values in the mailer like so:
def notify_admin (message_details)
#message_details = message_details
mail(to: "jesse#mydomain.com", subject: "Contact form filled out by: " + message_details[:name], from: message_details[:email])
end
This works when I want to send emails to myself as is the example above when someone submits the contact form.
It ALSO works when I want to send an email from my domain such as when they fill out the lead magnet. All I did was switch the to: and from: addresses.
Here's a working pony gem call.
Pony.mail({
:to => 'apotonick#gmail.com',
subject: "Pony ride",
body: "Awesome!",
from: "nick#trb.to", # this MUST be the sending Zoho email.
:via => :smtp,
:via_options => {
:address => 'smtp.zoho.com',
:port => '465',
:enable_starttls_auto => true,
ssl: true,
:user_name => 'nick#trb.to', # MUST be identical to :from.
:password => 'yourStrongPw',
:authentication => :login,
}
})
I had this issue, and I tried everything and still couldn't figure out what the issue was.
Let's face it, it's a SH!t message. What I did find though I was running my rails app locally with POW and its actually a POW error.
When I run rails server and do the same thing that caused the error, I actually got the real error message and was able to find I hadn't setup my controller correctly

Sending Mail in Rails App Heroku

I have Users that are able to invite others by email to a project they are working on but I keep getting the following error-
Net::SMTPAuthenticationError
Here is how I have mail settings configured for my production environment
config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com' }
#Sending email on the production side
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => :plain,
:user_name => "example#gmail.com",
:password => "password"
}
It works in my local environment using sqlite3 as I expect but when I push to heroku it throws the above error after running heroku logs. I believe my config settings to be correct and obviously heroku uses postgresql, so what am I missing here?
Thanks for you r attention.
I found the reason here How to send confirmation e-mails using gmail smtp service for registration using devise under development mode locally?
Google had detected some "suspicious" activity on the newly created account and was blocking me from sending emails. After verifying the account I was able to send emails with the above config and switching :domain => "heroku.com"

Using ActionMailer with a company Gmail account

I'm not sure if this belongs in server fault or here feel free to move it if it makes more sense somewhere else. I've seen the examples for setting up the smtp settings and using ActionMailer with Gmail and confirmed that they work for me.
Basically it looks like this for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
What I need to do now is to send an email to an address that isn't a plain Gmail account and yet, at it's core, is Gmail. My company uses whatever google's email service that allows you to use gmail but for the addresses to be listed as username#my.company.com rather than #gmail.com. I know for a fact that you can't simply log into our mail at the main gmail site so I assume our domain is different. Or something.
At the moment, when I simply use my own company user/pass I get an error message telling me that the user/pass was wrong. But I'm guessing the issue is that I'm trying to mail from the gmail variant of my username.
I have confirmed that our smtp server, as far as Thunderbird is concerned, is the normal gmail smtp, that our port is still 587, and that we are using TLS. What do I need to change here so that I can send an email to one of these addresses? Thanks.
I have my own domain setup at google for mail, the url to log into that directly is
http://mail.google.com/a/my.company.com
My rails app that sends mail through that account, has
:domain => "my.company.com"
as well as all the other fields you have.
"<user_name>" needs to be the whole email address, not just the user name.
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'busiess.com',
:user_name => 'username#company.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true

Delivery issues with Rails + Gmail SMTP in Dreamhost

I just released a Rails app in Dreamhost and I'm using Google Apps for my domain to handle Emai. I created the admin#domain.com account to serve as the sender authentication.
I installed smtp-tls plugin and my smtp conf is:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "www.domain.com",
:authentication => :plain,
:user_name => "admin#domain.com",
:password => 'secret'
}
The problem: Emails sometimes arrives, and sometimes don't. The recipient addresses are not exclusively Yahoo or GMail accounts, so I think it's not a spam issue. Any ideas? Thanks!
If you want better tracking of whether or not your emails are getting where you're trying to send them, checkout PostageApp:
http://postageapp.com/

Resources