Rails 2.3: SMTP settings for Postmarkapp: Connection refused - connect(2) - ruby-on-rails

Does anybody have experience with Postmarkapp?
I have a rails 2 app (radiant cms) and try to send emails through SMTP.
This is how my smtp settings looks like:
config.action_mailer.smtp_settings = {
:address => "smtp.postmarkapp.com",
:port => '25',
:authentication => :plain,
:user_name => 'postmark-ap-key',
:password => 'postmark-ap-key',
:domain => 'postmarkapp.com'
}
The Mailer class:
class RegistrationMailer < ActionMailer::Base
def send_email(email, sent_at = Time.now)
subject "Some text here"
recipients "#{email}"
from 'xxx#yxz.com'
sent_on sent_at
body :text => "Some text here"
end
end
and here is the code where I call the deliver method (in a controller action):
mail = RegistrationMailer.create_send_email(params[:email])
RegistrationMailer.deliver(mail)
I got an 'Connection refused - connect(2)' error whenever I call the deliver method. Anybody can help me out what am I doing wrong? I used the exact same code on heroku with other smtp settings (for sendgrid) and it worked without any problems.

I haven't used Postmark myself, but there appears to be a gem to help you send mail through their system, it's probably because you have to send through an API key.
https://github.com/wildbit/postmark-rails
http://rubygems.org/gems/postmark-rails
Relevant question for implementation: How can I customize Devise to send password reset emails using PostMark mailer

Related

Dot missing(on domain name) of password reset email link send by SendGrid

I am using SendGrid mailer on Ruby and Rails framework. In password reset email template we are sending a password reset link which looks like the following format (https://subdomain.domainname.com/password_reset/token/?some_other_params). Most of the time the password reset link is emailed to recipient in correct format but for some customer it is not sending the proper link. The issue we noticed is "the dot is missing between (subdomain and domainname) or (domainname and com) randomly and the resulting password reset link to customer looks like (https://subdomaindomainname.com/password_reset/token/?some_other_params) which is a wrong link. This issue is happening only on production and occur very random.
I verified our variable which have domain name in our source code and verified the code which is generating the url and also thoroughly tested this and their is no issue on our source code. On google i see this question. I did't understand how to programatically solve this on SendGrid email client.
mailer.rb
class Mailer < ActionMailer::Base
sendgrid_category Rails.env
default_url_options[:host] = APP_URL
end
config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => "subdomain.domainname.com",
:port => 587,
:authentication => "plain",
:enable_starttls_auto => true
}
APP_URL = "subdomain.domainname.com"
mailer_helper.rb
def link_to_with_ga(*args)
args[1] = append_ga args[1], 'html'
link_to *args
end
def append_ga(link, utm_content)
link << "#{link.include?('?') ? '&' : '?' }#{#ga_tag}&utm_content=#{utm_content}"
end
_reset_password_view.rb
<%= link_to_with_ga(t("users.reset_password"), reset_password_url(:token => #token, :locale=>#user_locale,:protocol => ( Rails.env.eql?('development') ? 'http' : 'https' )),:id => 'reset_link') %>
Please help me in solve this issue.
Thank you

Email - From address is the logged in address and not the address that specified in 'from' attribute

Below is the code for the mailer which sends the verification email:
ApplicationMailer
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
SendVerificationMailer
class SendVerificationMailer < ApplicationMailer
def send_verification(verification_id)
#verification = Verification.find(verification_id)
#email = #verification.email
#token = #verification.token
mail :from => "Support <support#domain.io>", :to => #email, :subject => "User verification code"
end
end
In production.rb I have configured the action_mailer as follows:
# Mailing preferences
config.action_mailer.default_url_options = { :host => ENV["HOST_NAME"] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => ENV["SMTP_ADDRESS"],
:port => ENV["SMTP_PORT"],
:domain => ENV["SMTP_DOMAIN"],
:authentication => :login,
:user_name => ENV["EMAIL"],
:password => ENV["PASSWORD"]
}
I used an account info#domain.io for smtp user_name and password. When I send Email from local machine it is working fine and the from address in the received mail is support#domain.io in both development and production mode.
But, when the mail is sent from production machine in AWS the from address displayed as info#domain.io and not support#domain.io what I expect to be.
I am using AWS Elastic Beanstalk for deployment in which I have a web server and a worker, where the worker takes the attributes from SQS queue and send the Email.
Can anyone help me in fixing this.
Thank you..
Some email providers such as GMAIL (which you are using) do not allow overriding the from email address. It must be the email address of the GMAIL account holder.
If you have a requirement to change the from email address you'll need to look at other providers.
You may want to look at MailGun www.mailgun.com which is an email service that's very developer-friendly (even has sample Ruby code in its documentation) and allows up to 10,000 emails per month for free.

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.

No email alerts in mailcatcher, but able to send emails to valid account?

I am running a Rails app and using Mailcather gem as an SMTP service. It was said that it can catch all outgoing emails, however I've done making correct settings in config/environments/development.rb, testing it to send email, but no email catched in either 127.0.0.1:1080 and localhost:1080. But the email was sent and received tho. I've tried all of the possible configurations. Here is my config/development.rb
config/development.rb
Ror::Application.configure do
# Mailer
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => '127.0.0.1:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "127.0.0.1", :port => 1025 }
end
Here is my user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "aaaaaaaaa#gmail.com"
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
end
I used the registration_confirmation() method to check whether user is registered or not via email. But none of of the email popped up in mailcatcher. I did install it under rvm. I did test install it both with wrapper and without wrapper but the result still the same. Bottom line is, it is able to send emails outside, but can't receive email inside. Or any notifications. Did I miss something? Any advice or corrections would be appreciated. Thanks
You call the mail method like this:
UserMailer.registration_confirmation(user).deliver

Can I send email from different addresses using same ActionMailer

I'm using ActionMailer for my Rails 2.3.9 application.
When I send an email using:
deliver_user_invite
config:
def user_invite(subject, content)
subject subject
from "User Invite <invite#mydomain.com>"
recipients "invites#mydomain.com"
sent_on Time.now
content_type "text/html"
body :content => content
end
with SMTP configuration
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'mydomain.com',
:authentication => :plain,
:user_name => 'user#mydomain.com',
:password => 'password'
}
However when the email is sent, the sender email shows as user#mydomain.com instead of invite#mydomain.com.
Can I have different SMTP configuration for different email addresses? or Is there a way to set the sender email address from ActionMailer config?
This is a Gmail SMTP limitation. It will always make the sender of the e-mail be the username/login you use for your smtp settings, and will ignore your from address.
A possible workaround might be to change the smtp settings dynamically when you need to send as someone else.
Edit: You might be able to go into the settings for your own Gmail account and use the "Add another email address you own" option to allow your account to send through those e-mail addresses. I haven't tested it, but it might work. (See http://www.mobileread.com/forums/showpost.php?p=21093&postcount=1).

Resources