=0A in the subject of an email sent with Rails? - ruby-on-rails

I have this in my user_mailer.rb
class UserMailer < ActionMailer::Base
default from: ["no-reply##{CONFIG[:domain]}"]
def password_reset(user, portal_name)
#user = user
mail to: #user.email, subject: t('emails.password_reset.subject')
end
end
I have this in my yml translation file:
emails:
password_reset:
subject: You've requested to reset your password
There are no characters at the end of the translation string, however when the email is sent the subject appears like this in the email: "You've requested to reset your password=0A"
I've tried searching for an answer and I found Rails used to have an ActionMailer::Quoting.quoted_printable method, but it seems this no longer exists in rails 4.
Where is the "=0A" coming from? Any built-in solution to this in rails?

I managed to solve the problem by adding a chomp at the end:
mail to: user.email, subject: t('emails.password_reset.subject').chomp
It seems a newline was being introduced somewhere!

I also encountered this problem when using the 'X-SMTPAPI' header(SendCloud Mail Service).This is because Mail gem will handle the headers:
def encode_crlf(value)
value.gsub!(CR, CR_ENCODED)
value.gsub!(LF, LF_ENCODED)
value
end
When I tried using Mail gem only, it worked. I think this is because the charset is different with Rails' default setting. I solved the issue like this:
headers["X-SMTPAPI"] = Base64.encode64(JSON.dump({"to" => emails, "sub" => {"%name%" => names}})).gsub!(/\n/,'')

Related

How to use Sorcery Gem with Sidekiq

I am using Sorcery Gem to Authenticate user in my Rails application. Everything is working fine. When user register then I am able to send user activation email. However sending email is taking long time so I was thinking to delay the email or send email in backgroud using Sidekiq. I read Sorcery documentation but couldn't find the way to delay the email using Sidekiq.
Please guide me how to delay the email send by Sorcery gem using Sidekiq.
There could be two approach to this:
Call Sidekiq worker inside Mailer
what I understand from the docs is that you can configure it to call any
Mailer. What you could do inside the method is call
MailerJob.perform_late(*arg) instead of calling mail(*args) right away
Assuming you have a code something like this (ref: https://github.com/Sorcery/sorcery/wiki/User-Activation)
# app/mailers/user_mailer.rb
def activation_needed_email(user)
#user = user
#url = activate_user_url(#user.activation_token)
mail(to: user.email, subject: 'Welcome to My Awesome Site')
end
You can change the code to this
# app/mailers/user_mailer.rb
def activation_needed_email(user)
#user = user
#url = activate_user_url(#user.activation_token)
# mail(to: user.email, subject: 'Welcome to My Awesome Site')
MyMailerWorker.perfor_later(to: user.email, subject: 'Welcome to My Awesome Site')
end
Your worker would be defined as per the sidekiq docs(ref: https://github.com/mperham/sidekiq/wiki/Getting-Started)
class MyMailerWorker
include Sidekiq::Worker
def perform(to: nil, subject: nil)
MailerIWantToCall.send_activation_email(to, subject)
end
end
P.S: Option 1 is not a clean approach but works if you are stuck with Sorcery legacy configs
Don't set mail settings for srcoery(A cleaner approach)
Do not setup mail through sorcery at all.You can revert the changes
mentioned for UserMailer on this page
(github.com/Sorcery/sorcery/wiki/User-Activation) and add a callback on User
model based on when do you want to trigger an email and that way you have
control over what to call and with sidekiq or not.
I found the solution. It's quite easy. We just need to add following line in sorcery.rb file and all sorcery mail will be handled by ActiveJob
user.email_delivery_method = :deliver_later

how to change the sender email credentials according to the user of application in rails

I want to give different sender email credentials according to the user of application in rails which will be fetched from database.
If you're using ActionMailer, you can to set 'from':
fetch_sender = Object.select(:email, :name).find(id)
mail(from: "#{fetch_sender.name} <#{fetch_sender.email}>", to: 'Receiver <example#receiver.com>', subject: 'Something')
Example for ActionMailer class:
app/mailers/custom_mailer.rb
class CustomMailer < ApplicationMailer
def custom(sender, receiver)
mail(from: "#{sender.name} <#{sender.email}>", to: "#{receiver.name} <#{receiver.email}>", subject: 'Something')
end
end
For sending
CustomMailer.custom(sender, receiver).deliver
P.s. If you have sidekiq or something else, yo may use deliver_now or deliver_later

Rails 4 ActionMailer headers

I'm on Rails 4 and I've got one mailer. This was created following the tutorial in the docs.
class UserMailer < ActionMailer::Base
#delivery_options = {
user_name: 'user_name',
password: 'password',
address: 'smtp.sendgrid.net'
}
default from: "Auto <auto#mysite.me>"
def welcome_email (user, password)
#user = user
#password = password
#url = "http://url.com"
mail(to: #user.email, subject: "Welcome to my site", delivery_method_options: #delivery_options)
end
def project_invite_email (email, project)
#project = project
mail(to: email, subject: "#{#project.user.first_name} #{#project.user.last_name} requests a video from you", delivery_method_options: #delivery_options)
end
end
During the course of troubleshooting some deliverability issues, I found that some emails included different headers than others. It turned out that a combination of SPF, DKIM, and adjustments to the email copy were able to resolve the deliverability problems (many were previously caught by spam filters), but I'd still like to know more about how Rails is creating the headers for these emails.
For example, the second one includes this in the header: Content-Transfer-Encoding: 7bit but the first has this: Content-Transfer-Encoding: quoted-printable
As you can see, they both use the exact same configurations. The only difference is the content of the views (both have an HTML & text version).
Is rails adjusting the headers based on content?
Ok. I'll post it as a question next time. Thanks.
I've got this resolved now with the help of the other post.
How to change the mailer Content-Transfer-Encoding settings in Rails?
m = mail(...)
m.transport_encoding = "quoted-printable"
m.deliver
Yes, rails automatically adjusts Content-Transfer-Encoding. If you have non-standard characters in your mailer view, the header may randomly switch between 7bit and quoted-printable (or just stick to quoted-printable).
If required, you can force the mailer to use the default encoding (7bit).
class Mailer < ActionMailer::Base
default from: 'from#example.com',
content_transfer_encoding: '7bit'
...
end
However it is most likely caused by an invalid character, which should be visible (such as Â) once you force the header to 7bit.

How can I make my mailer deliver a message to an address containing umlauts under Rails 3?

This is my mailer:
class MyUserMailer < ActionMailer::Base
default from: "me#mail.com",
:content_transfer_encoding => "7bit"
def build_email(user)
mail(:to => user.email,:subject => "Welcome")
end
end
I'm testing with an address containing umlauts: äardvark#mail.com, but I keep receiving the following error, when I try to deliver the email:
'to' parameter is not a valid address. please check documentation
Documentation says that the mailer will use UTF-8 to encode all the fields. How can I make this work?
EDIT: I'm using Rails 3.2.5
Problem was caused by my usage of the mailgun Rails gem. After I switched to using smtp directly, problem went away.

Rails - How do I use full email addresses without triggering Net::SMTPFatalError?

I am new to rails and using rails-2.3.5 and ruby-1.8.7. Here is my notifier.rb model:
# app/models/notifier.rb
class Notifier < ActionMailer::Base
default_url_options[:host] = "foo.com"
#This method sends an email with token to users who request a new password
def password_reset_instructions(user)
subject "Password Reset Instructions"
from "Support Team<support#foo.com>"
recipients user.email
sent_on Time.now
body :edit_password_reset_url =>
edit_password_reset_url(user.perishable_token)
end
end
When I call this method I get the following error:
Net::SMTPFatalError in Password resetsController#create
555 5.5.2 Syntax error. 36sm970138yxh.13
I found an article that said the problem was a bug in ruby-1.8.4 and that the fix is to remove the angle brackets from the :from field. Sure enough, if I just use "support#foo.com" instead of "Support Team<support#foo.com>" everything works fine.
However, there is no reference to this issue in either the rails-2.3.5 API or ActionMailer Basics rails guide, and in fact both show "name<mail address>" in their actionmailer setup examples. Anyone know what I am doing wrong?
From the ticket that Travis referenced, it looks as though you can possibly avoid the problem with:
def password_reset_instructions(user)
subject "Password Reset Instructions"
from "Support Team<support#foo.com>"
+ headers "return-path" => 'support#foo.com'
recipients user.email
sent_on Time.now
body :edit_password_reset_url =>
edit_password_reset_url(user.perishable_token)
end
Otherwise, you can grab one of the patches noted in the ticket or wait for 2.3.6 or 3.x
Rails/ActionMailer broke this:
https://rails.lighthouseapp.com/projects/8994/tickets/2340
And since critical bugs like this don't get high priority or interim releases to fix them in the Rails project, you either have to patch it up yourself or wait a looong time to get it fixed. Just like this insanely bad bug that came out in Rails 2.3.4 that made Rails completely unusable for Ruby 1.9: https://rails.lighthouseapp.com/projects/8994/tickets/3144-undefined-method-for-string-ror-234 . Took months to get a fix for that.
The problem is a perform_delivery_smtp method from ActionMailer::Base used in rails 2.3.4 and 2.3.5. You can always try to monkey-patch-it like that:
class ApplicationMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email from "Site Notifications<notifications#example.com>"
subject "Welcome!"
sent_on Time.now
...
end
def perform_delivery_smtp(mail)
destinations = mail.destinations
mail.ready_to_send
sender = mail['return-path'] || mail.from
smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
smtp_settings[:authentication]) do |smtp|
smtp.sendmail(mail.encoded, sender, destinations)
end
end
end

Resources