Adding bcc malfunctioning - Rails - ruby-on-rails

I just came across a problem, spent hours but couldn't found root cause.
Mail sending works fine if I don't add bcc option but when I add bcc,
def test_email
mail :to => 'recipient#xyz.com',
:subject => 'Sample test',
:bcc => ['bcc#xyz.com']
end
It sends email to none and adds recipient#xyz.com as bcc.
Here is console output,
Mailer.test_email.deliver
#<Mail::Message:63045380, Multipart: true, Headers: <Date: Mon, 02 Jun 2014 13:03:02 +0000>, <From: hostmail#sample.dk>, <To: >, <Cc: >, <Bcc: ["recipient#xyz.com"]>, <Message-ID: <538c760686568_45b9a1001872969#DANVA-test-NAGIOS-monitor.mail>>, <Subject: Sample test>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_538c76063914_45b9a1001872662"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <X-Auto-Response-Suppress: OOF>, <Auto-Submitted: auto-generated>>
I am using Rails 3.2.8 and ruby 1.9.3p545.
I ideas to fix this prblem?

Related

Attempt to send an email via Rails ActionMailer results in a failed call to `normalize_name` inside `lookup_context.rb`

I have a Mailer class which inherits from ApplicationMailer, which in turn inherits from ActionMailer::Base. Ruby version is ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15].
The Mailer class looks like the following:
class PurchaseOrderStatusMailer < ApplicationMailer
CONTACTS = {
JC: ['me#example.com'],
RM: ['me#example.com']
}
def daily_report_email(facility)
#facility = facility
ingredient_items = LineItem.ingredient.by_facility(facility)
#delivered_count = ingredient_items.by_date([7.days.ago, 7.days.from_now]).delivered.count
#partial_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).partial.count
#late_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).late.count
#expected_count = ingredient_items.by_date([Date.today, 7.days.from_now]).expected.count
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}")
end
end
ApplicationMailer looks like the following:
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'notify#example.com'
def facility_email(facility)
emails = Rails.application.config_for(:emails)
(emails[facility] + emails["DEFAULT"]).flatten
end
end
The view is located at app/views/purchase_order_status_mailer/daily_report_email.html.erb.
When I open my Rails console and type PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver, I see the following error:
NoMethodError: undefined method `empty?' for nil:NilClass
from /Users/me/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionview-5.0.4/lib/action_view/lookup_context.rb:215:in `normalize_name'
I tried passing a format block to the call to the mail helper with the same call, like so:
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}") do |format|
format.text { render plain: "Hey!" }
end
The above produced the following response, which appears to represent a successful email send:
Rendering text template
Rendered text template (0.0ms)
Sent mail to me#example.com (8.8ms)
Date: Mon, 25 Sep 2017 12:55:11 -0400
From: notify#example.com
To: me#example.com
Message-ID: <59c934efec401_1115b3fd69cc3f840917be#me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hey!
=> #<Mail::Message:70191029273300, Multipart: false, Headers: <Date: Mon, 25 Sep 2017 12:55:11 -0400>, <From: notify#example.com>, <To: ["me#example.com"]>, <Message-ID: <59c934efec401_1115b3fd69cc3f840917be#me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
I didn't actually receive an email, and I assume that means I don't have SMTP set up on my local machine, but the above response is encouraging. There was no stack trace apart from the error I posted above, so I tried digging into the Rails source code, and I saw that normalize_name inside lookup_context.rb gets called from within the args_for_lookup protected method, which in turn gets called by the ViewPaths module's find_all method. But beyond that it was hard to trace the call stack, as I couldn't find who the caller of find_all is.
My question is: what is wrong with the first call to mail?
EDIT 1: I also tried format.html { render html: "<h1>Hello Mikel!</h1>".html_safe } instead of the format.text option, as per the example here, and I got a similar success message.
I then tried adding a byebug statement inside normalize_name, to try and identify what the values of the parameters were on the successful email sends, but it looks like this method isn't getting called when a block is passed. That makes me suspect even more strongly that the problem is somehow related to my views. But I can't confirm that yet.
The issue is here
PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver
remove the new i.e
PurchaseOrderStatusMailer.daily_report_email('JC').deliver
Because of the wrong object of its not able to deduce the template path/file
I was able to generate a success response (including my mailer's rendered template file) by passing template_path and template_name options to the original call to the mail helper, like so:
mail(
to: CONTACTS[facility.to_sym],
subject: "#{facility} Daily Receipt Status - #{Date.today}",
template_path: 'purchase_order_status_mailer',
template_name: 'daily_report_email')
This generated the following success response:
Rendering purchase_order_status_mailer/daily_report_email.text.erb
Rendered purchase_order_status_mailer/daily_report_email.text.erb (0.3ms)
Sent mail to me#example.com (12.1ms)
Date: Mon, 25 Sep 2017 15:18:25 -0400
From: notify#example.com
To: me#example.com
Message-ID: <59c9568121cec_11e943fee8503f82823542#me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59c9568120033_11e943fee8503f8282341e
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Please find the receipt status for JC as of 09-25-2017:
=================================================================================================
...<lots more content, including ERB rendered into HTML>...
=> #<Mail::Message:70293683934580, Multipart: true, Headers: <Date: Mon, 25 Sep 2017 15:18:25 -0400>, <From: notify#example.com>, <To: ["me#example.com"]>, <Message-ID: <59c9568121cec_11e943fee8503f82823542#me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
I'm a little confused about why I had to add these options, as the documentation doesn't make it clear that these are required and my templates and folders appear to be named correctly, but it worked and I have deadlines so I'm moving on. :-)

amazon ses ActionMailer ruby on rails debug

I followed the recommendation here from Sujoy Gupta to use smtp to send emails on rails. I get no error on the console but the emails never seem to have reached ses: The statistics there show no email sent/received and I never got them in my inbox either.
I added this to my config/environments/development.rb/test.rb/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "email-smtp.us-west-2.amazonaws.com",
:port => 587,
:user_name => "...", # My SMTP user here.
:password => "...", # My SMTP password here.
:authentication => :login,
:enable_starttls_auto => true
}
and app/mailers/UserMailer.rb:
class UserMailer < ApplicationMailer
default from: "user#hotmail.com"
def test(email)
mail(to: email, subject: 'ses test')
end
end
Here is what printed in the console:
UserMailer.test("user#hotmail.com").deliver_now
Rendered user_mailer/test.html.erb within layouts/mailer (0.1ms)
UserMailer#test: processed outbound mail in 16.2ms
Sent mail to user#hotmail.com (769.1ms)
Date: Wed, 11 Nov 2015 10:26:33 -0800
From: user#hotmail.com
To: user#hotmail.com
Message-ID: <56438859b7ee2_a6503fe78cc601fc19958#10ddb1e504ea.ant.amazon.com.mail>
Subject: ses test
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
A quick brown fox jumped over the lazy dog.
</body>
</html>
</body>
</html>
=> #<Mail::Message:70263775568080, Multipart: false, Headers: <Date: Wed, 11 Nov 2015 10:26:33 -0800>, <From: user#hotmail.com>, <To: user#hotmail.com>, <Message-ID: <56438859b7ee2_a6503fe78cc601fc19958#10ddb1e504ea.ant.amazon.com.mail>>, <Subject: ses test>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>>
Following the java documentation here, I was able to send an email successfully using the same host and smtp username/password.
Any idea what I might have wrong? Where can I find more logs to dig more into it?
Thanks!
Turns out I just needed to close and reopen the rails console. For some reason reloading it is not enough.

Rspec Mail object does not respond to include?

I'm trying to learn testing in rails with the help of this screencast. But I get stuck with this step
expect(last_email).to include(user.email)
The error message is
expected #<Mail::Message:47172420, Multipart: true, Headers: <Date: Sun, 08 Feb 2015 09:51:56 +0530>, <From: from#example.com>, <To: person1#example.com>, <Message-ID: <54d6e464682e_1450a1b32815670#User-PC.mail>>, <Subject: Password Reset>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_54d6e4642e9e_1450a1b328155a"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>> to include "person1#example.com", but it does not respond to `include?`
The mail object does not respond to 'include?'. Could this be due to a change in the rspec syntax or is it something wrong with the code. Thanks for the answers in advance.
Looks like you're not calling to on last_email:
expect(last_email.to).to include(user.email)

Rails 3.0.17 sending inline attachments instead of regular ones

So, we have just upgraded our project to Rails 3.0.17 (please don't ask why this particular version, just happened so) on top of Ruby 1.8.7 enterprise edition.
I have methods to generate CSV data and send it as a file via email.
# contact_mailer.rb
def send_payments_report
attachments["report.csv"] = {
:content => Payment.generate_csv_report,
:mime_type => "text/csv"
}
mail(
:from => "from#example.com",
:subject => "Payments report",
:to => "to#example.com"
)
end
Here's what it returns:
=> #<Mail::Message:2231891440, Multipart: true, Headers: <Date: Wed, 19 Dec 2012 11:48:05 +0200>, <From: from#example.com>, <To: to#example.com>, <Message-ID: <50d18d555dcdc_2269838ab93812784#artem.local.mail>>, <Subject: Payments report>, <Mime-Version: 1.0>, <Content-Type: multipart/mixed; >, <Content-Transfer-Encoding: 7bit>>
The email itself is displayed as plain text without attachments:
--
Date: Wed, 19 Dec 2012 11:48:05 +0200
Mime-Version: 1.0
Content-Type: text/csv;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=report.csv
Content-ID: <50d18d555dcdc_2269838ab93812784#artem.local.mail>
"customer id","reference nr","invoice id","invoice type","invoice sum","sum paid","payment date"
"1035","010294","7113","Fine","250.00","100.00","2012-11-15"
"1035","010294","6132","Fine","250.00","250.00","2012-11-15"
"1035","010294","5241","Fine","850.00","650.00","2012-11-15"
--
I tried attaching existing file, but it displays it the same way. It worked perfectly on Rails 2.3.9. Can anyone help on that?
I seem to have solved this problem.
Here how I call the mail method:
mail(
:from => "from#example.com",
:subject => "Payments report",
:to => "to#example.com"
) do |format|
format.text {render :text => ""}
end

Rails 3 ActionMailer - no recipent address

i have a problem with my prod actionmailer. I'm hosted on site5 sharing cloud. The problem is i get this error message.
A message that you sent using the -t command line option contained no
addresses that were not also on the command line, and were therefore
suppressed. This left no recipient addresses, and so no delivery could
be attempted.
------ This is a copy of your message, including all the headers. ------
Date: Sat, 17 Sep 2011 21:06:42 +0000
From: noreply#mydomain.com
To: myemail#mydomain.com
Message-ID: <4e750be27b3ef_4a9c159402ee91b039763#themailserver.com>
Subject: test
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
i added default
default :recipient => 'noreply#mydomain.com'
but the mail return me the same error.
my ActionMailer controller
class Emailtousers < ActionMailer::Base
default :from => "noreply#mydomain.com"
default :recipient => "noreplay#mydomain.com"
def plan_notification(resource)
#resource = resource
puts resource
mail(:to => "myemail#mydomain.com", :subject => 'test')
end
end
What i missed?
i followed this to help me http://edgeguides.rubyonrails.org/action_mailer_basics.html
I got this problem using the :sendmail delivery method with exim. Adding this to config/application.rb fixed it for me:
config.action_mailer.sendmail_settings = { :arguments => "-i" }
I found the solution at https://github.com/mikel/mail/issues/70#issuecomment-2639987

Resources