Sending email using sendgird - ruby-on-rails

I am trying to send email using sendgrid. The email is delivered, but when i check it, the content is missing.
I followed the instruction provided by sendgrid.
-config.action_mailer.delivery_method = :smtp
-edit config/environment.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '25',
:domain => "halo.dev",
:authentication => :plain,
:user_name => "account",
:password => "password"
}
The email can be successfully delievered. However,it only shows
from halo#halo.net via sendgrid.me
to user#user.com
date Wed, Nov 2, 2011 at 5:23 PM
mailed-by sendgrid.me
signed-by sendgrid.me
No content html is shown
What I have tried:
deliever the email as file (i.e config.action_mailer.delivery_method = :file) It works fine. I got the file in local
deliever the email using the program itself (i.e. config.action_mailer.delivery_method = :sendmail) It works fine too. When I check the email, everything is ok.

Try turning on the Email Template App inside of your Sendgrid Account.

Related

Emails send from rails with O365 being rejected as spam

I have a Rails 5 app deployed on Heroku. I'm using devise for email and have setup everything correctly to send password reset emails. This works perfectly in my dev environment. However, when I send from production I get an error in my 0365 inbox that says:
Generating server: CO2PR13MB0140.namprd13.prod.outlook.com
myemail#gmail.com
Remote Server returned '550 5.7.708 Service unavailable. Access denied, traffic
not accepted from this IP. For more information please go to
http://go.microsoft.com/fwlink/?LinkId=526653 AS(8561)
[CO2PR13MB0124.namprd13.prod.outlook.com]'
Original message headers:
Received: from CO2PR13MB0140.namprd13.prod.outlook.com
([fe80::c872:9c6:9d6a:8b3]) by CO2PR13MB0140.namprd13.prod.outlook.com
([fe80::c872:9c6:9d6a:8b3%4]) with mapi id 15.20.1339.019; Wed, 14 Nov 2018
02:42:20 +0000
MIME-Version: 1.0
Content-Type: text/plain
Date: Wed, 14 Nov 2018 02:42:20 +0000
Message-ID:
<5beb8b87e888a_42b06292aba70609bd#01184e4c-2116-478b-a773-fcc26ac206aa.mail>
Subject: Reset password instructions
In looking into this, and talking to support, it appears that the it's being rejected because a third party server is (my app on Heroku I'm assuming) is trying to send out the email with 0365 settings.
Here are my development.rb settings that work perfectly:
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.office365.com',
:port => '587',
:authentication => :login,
:user_name => ENV['365_USERNAME'],
:password => ENV['365_PASSWORD'],
:domain => 'mysite.com',
:enable_starttls_auto => true
}
Here are my production.rb settings that cause O365 to reject the email. I've tried playing with these quite a bit. And I've ensured that the "from" email matches the login email. i.e. I'm not trying to send from no-reply#mysite.com or similar.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { :host => 'www.mysite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.office365.com',
:port => '587',
:authentication => :login,
:user_name => ENV['365_USERNAME'],
:password => ENV['365_PASSWORD'],
:domain => 'mysite.com',
:enable_starttls_auto => true
}
From discussions with support. I may need to add an SPF TXT record to allow emails to be sent from a remote server. The problem is, I have no idea what to add to the SPF record. My current spf record is:
v=spf1 include:spf.protection.outlook.com -all
Their response just says they have blacklisted your IP, so it’s not down to your message content.
If you are sending from Heroku, you may need to list them in your SPF as well. Also ensure that your host name resolves backwards as well as forwards - that should be possible in Heroku’s control panel.
Once you’ve done that, check your SPF record gives a pass to your message source using kitterman.com or mxtoolbox testing services.
O365’s spam filter and blocking policy is pretty bad anyway, however I have had success in asking their support to remove blocks, though you need to persevere because they reject all such requests by default. This is poor service, but it’s still far better than you will get from gmail.

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.

Do I need to provide a "from" email address while using Heroku + SendGrid?

I have the standard setup in config/environments/production.rb
config.action_mailer.default_url_options = { host: "myapp.heroku.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:addresses => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
However, I encountered the following error while trying to send a mail:
ArgumentError: An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.
I then tried using the email I found on my heroku add-on page, i.e. app#######heroku.com, but now I got
Errno::ECONNREFUSED: Connection refused - connect(2)
So do I need to specify a from email or not? If yes, which one should I use?
Try this code in your production.rb file:
config.action_mailer.default_options = { from: "my_address#example.com" }
Yes you need to set a from address. All emails need an from address specified.
:from => "address_you_are_sending_from#your_domain.com"
This needs to be set in your _mailer.rb file. You can also set it gloablly in an initiallizer but it must always be referenced in your_mailer.rb files, and within each mail method itself.
You should change the :domain to a domain that you control. But if you were to use Heroku would your apps address not be yourapp.herokuapp.com instead of yourapp.heroku.com?
You can set a from address in your mailer file.
For example if you have an admin_mailer.rb for notifying your personal email of new activity on your site, it could look like this:
def new_customer_trial(customer_object)
#new_customer = customer_object
mail :to => "me#personal_email.com", :subject => "New Customer Trial", :from => "'My Site Notifications' <notifications#my_site.com>"
end
And you'd send that mail from a controller:
AdminMailer.new_customer_trial(customer_object)
or as a delayed job:
AdminMailer.delay.new_customer_trial(customer_object)

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

Net::SMTPAuthenticationError 502 5.5.2 in Rails ActionMailer

i'm trying to send confirmation email to the user.
But i get following error:
Net::SMTPAuthenticationError (502 5.5.2 Error: command not recognized
Configuration in production.rb is following:
# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = true
# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp
# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
:address => 'path_to_address_specified_by_my_hoster',
:port => 25,
:domain => 'my_domain.com',
:authentication => :plain,
:user_name => 'signup#my_domain.com',
:password => 'password'
}
I have created a mailbox in user profile at my hosting provider, named "signup#my_domain.com"
For created mailbox, they issued to me login and password:
login = verbose_login
password = verbose_password
I did't completely understood the exact format of :user_name.
Should i use
:user_name => "signup#my_domain.com"
or:
:user_name => "signup"
or:
:user_name => "verbose_login"
Or this field is specific to the mail server, and i must ask support of hosting provider ?
And what the difference between :authentication => :plain and :login ?
Thanks.
This works well for me:
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'my_nick#gmail.com',
:password => 'secret_password',
:authentication => 'login',
:enable_starttls_auto => true
}
more info here
Petr
I have got the same error recently, and the reason was incorrect format of recipients
recipient = IO.readlines(emails_filename).first
mail(:to => recipient, :subject => subject)
Don't forget to add strip to get clean email addresses.
I had the same problem, but it is just a google configuration issue.
For some reason Google was blocking access from unknown location (app in production), so to solve this you can go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue (this will grant access for 10 minutes for registering new apps).
On the other hand, you could login to your gmail account, then go to https://www.google.com/settings/security/lesssecureapps and enable the access to less secure applications, which was the solution for me!
Please try to login with browser once.
There might be some issue with login (it will ask captcha etc).
Once you successfully logged-in, then try with Rails Mailer.
It should work now.
This issue generally happens with test account as we do not login via browser usually.
Then mail providers ask for confirmation like captcha, dob or answer of security question etc.
You've to use ActionMaliler::Base instead of config
ActionMailer::Base.smtp_settings #try this
config.action_mailer.smtp_settings #instead of this
Change your smtp code to below. It should work now.
ActionMailer::Base.smtp_settings = {
:address => 'path_to_address_specified_by_my_hoster',
:port => 25,
:domain => 'my_domain.com',
:authentication => :plain,
:user_name => 'signup#my_domain.com',
:password => 'password'
}

Resources