Rails 3 ActionMailer - Gmail Classifies Message As Spam - ruby-on-rails

My ActionMailer is set up to send an email once a user fills out an application. After upgrading to Rails 3 and ActionMailer, gmail now seems to be classifying the response email as spam.
I use google apps for the domain (hosted by dreamhost) and have it set up to send as smtp; and I am able to directly send emails to the same users from the google apps web account, and not have it classified as spam.
My question is: are there settings or values that I should have set in the ActionMailer (etc) that might circumvent this?
One recommendation I got was to set up and SPF, but I wasn't sure about this as I was using smtp via gmail.
Here is my configuration:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "email#domain.org",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
The email is rendered as html (have a plain text one too), with attachments of the files the user uploaded to the application.
<!DOCTYPE html>
<HTML>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<p>
Dear <%= #f.first_name + " " + #f.last_name %>,<br>
<br>
etc....
Here is how my mailer is formatted
class EmploymentMailer < ActionMailer::Base
default :from => "email#domain.org"
def employment_app_email(f, files)
#f = f
#files = files
mail(:to => ['email#domain.org', f.email], :subject => "Subject")
files.each do |file|
attachments[file[1].original_filename] = File.open(file[1].path, 'rb'){|a| a.read}
end
end
end

Related

ActionMailer SMTP working in production but not locally

Seems to be the opposite problem to a lot of the questions on here. I've not been able to get the mailers working locally but then I deployed my app to Heroku and everything is fine.
No apparent errors when sending locally. Strangely though, I can have incorrect credentials locally and it will still appear to have sent ok. Almost as if it's not trying to deliver through smtp. I get a message like this in the console after sending:
UploadMailer#complete: processed outbound mail in 32.1ms Delivered mail 63ded40c98545_b9cbb90-440#Chriss-MacBook-Pro.local.mail (1238.6ms)
There's nothing in the SendGrid dashboard showing that it has reached their servers.
config/application.rb
...
config.action_mailer.delivery_method :smtp
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:address => 'smtp.sendgrid.net',
:domain => ENV['SENDGRID_DOMAIN'],
:port => 587,
:authentication => 'plain',
:enable_starttls_auto => true
}
Mailer and mailer view
class UploadMailer < ApplicationMailer
def complete
mail(to: 'me#myemail.com', subject: 'foo')
end
end
# app/views/upload_mailer/complete.html.erb
<h2>
Hello
</h2>
Environment
Ruby: 3.0.2 (rbenv)
Rails: 6.1.7.2
Tried on Mac and Windows(WSL)
Looks like I was just missing
config.action_mailer.perform_deliveries = true
in config/environments/development.rb

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

An SMTP To address is required to send a message/Argument Errror

ArgumentError at /contacts
An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.
Good Day Guys,I am having trouble debugging this myself and have consumed so much time I needed your help and explanation as to how why it didn't work.
Things I did already:
1.Added gem 'binding_of_caller'
2.bundle install
goal:
Is when a visitor submit a contact form it supposed to send me a email message automatically.
My woes/confusion:
1.How do you set your email in secrets.yml or put your email wherein you configured the contact form request directly to your email
2.What I did is put
to:myemail.com > secrets.yml both in production and development
3.Am I right?
Please explain this to me as I am going in depth on ruby on rails.
You need to generate a mailer first.
rails g mailer example_mailer
app/mailers/example_mailer.rb This file will be generated by this line.
Then you need to create a method in your mailer where u can define whom to send mail. you can give direct email id or you can take it from database. As in my given example u need to pass user to this method and it will send email to that user's email.
def sample_email(user)
#user = user
mail(to: #user.email, subject: 'Sample Email')
end
Create a HTML template for how your mail will look
app/views/example_mailer/sample_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi <%= #user.name %></h1>
<p>
Sample mail sent using smtp.
</p>
</body>
</html>
Then you need to configure your smtp in /config/environments/production.rb or /config/environments/development.rb whichever environment you are using.
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'username#gmail.com',
:password => 'Gmail password',
:authentication => "plain",
:enable_starttls_auto => true
}
Then you need to trigger your mail action any any method from where you are sending mails.
ExampleMailer.sample_email(#user).deliver

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

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

ActionMailer emails "sent" in development.log, but not received

I'm having problems actually sending via ActionMailer in development, on my localhost, with Rails 2.3.2 and Ruby 1.8.6. The development.log shows that it has "sent" the email with no errors, but the email is not received. I have tried multiple email addresses for sending and receiving and have tried multiple configs and plugins, but cannot get the email to send. Any help would be much appreciated - I feel like I'm dancing around a bunch of versions of solutions for different versions of rails and ruby and can't nail it down. I would much appreciate any comments. Thanks!
Plugins:
action mailer optional tls
smtp_tls
Different email configs:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true, #works in ruby 1.8.7 and above
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'example.com',
:authentication => :plain,
:user_name => 'testacct',
:password => 'secret'
}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:tls => :true,
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => 'testacct#gmail.com',
:password => 'secret'
#:enable_starttls_auto => true # for rails >= 2.2 && ruby >= 1.8.7
}
config.action_mailer.perform_deliveries = :true #try to force sending in development
config.action_mailer.raise_delivery_errors = :true
config.action_mailer.default_charset = "utf-8"
Development.log:
Sent mail to sa23kdj#trash2009.com
Date: Fri, 18 Dec 2009 00:27:06 -0800
From: Test Email Acct <testacct#gmail.com>
To: sa23kdj#trash2009.com
Subject: Signup
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=mimepart_4b2b3cda9088_634334302a5b7
--mimepart_4b2b3cda9088_634334302a5b7
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww=
w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang=3D'en' xml:lang=3D'en' xmlns=3D'http://www.w3.org/1999/xhtml'>=
<head>
<meta content=3D'text/html;charset=3DUTF-8' http-equiv=3D'content-typ=
e' />
</head>
<body>
Welcome Email
<p>
user name:
lfglkdfgklsdf
activation link:
http://localhost:3000/login
</p>
</body>
</html>
--mimepart_4b2b3cda9088_634334302a5b7--
Put the following in config/environments/development.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
It will override the settings in config/environment.rb
Also for rails 2.X you'll need to setup:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.com",
:user_name => "username#domain.com",
:password => "secret_passsword",
:authentication => :plain
}
You need to use true and not :true.
:tls => true
...
config.action_mailer.perform_deliveries = true #try to force sending in development
config.action_mailer.raise_delivery_errors = true
In case anyone faces this problem, set "config.action_mailer.raise_delivery_errors = true" in development.rb in your environments folder and try sending mail again. This should raise whatever error is being encountered.
Sometimes in line 8 of smtp_tls.rb , the check_auth_args method accepts only 2 arguments : user and secret. Remove the 'authtype' argument if you see it and try again. Should work.

Resources