0
Rails 4.2.1
Ruby 2.1.5

I am trying to use ActionMailer to send an email to a number of recipients, but I want the recipients to be all in the :Bcc field.

In my conttollers/communications_controller.rb, I have:

  def create
    @communication = Communication.new(communication_params)
    @message_elements = construct_message_body(communication_params)
    @communication.message = @message_elements.to_json
    @communication.originator_id = current_user.id
    users_email_groups = users_emails
    @communication.recipients = users_email_groups.to_json
    respond_to do |format|
      if @communication.save
        users_email_groups.each do |recipients|
          logger.info("Recipients - start:")
          logger.info(recipients)
          MyMailer.communication_to_all_users(subject, recipients, message_elements).deliver_later
        end
        format.html { redirect_to root_path, notice: 'Email was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

In my models/my_mailer.rb, I have:

  def communication_to_all_users(subject, recipients, message_elements)
    logger.info("Module: #{__FILE__} - Line: #{__LINE__}")
    @message_elements = message_elements
    mail(:subject => subject,
         :to => Rails.application.secrets.email_provider_username,
         :bcc => recipients)
  end

When the code is run, I get the message:

Incomplete response received from application

And in the log file, I have:

Processing by CommunicationsController#create as HTML
  Parameters: {"utf8"=>"✓", "communication"=>{"subject"=>"Test Message - 2018-04-05 04:58", "paragraph_one"=>"Para 1 test", "paragraph_two"=>"", "paragraph_three"=>"", "paragraph_four"=>"", "paragraph_five"=>"", "signature"=>""}, "commit"=>"Send Message"}
    SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1[0m
    SELECT `users`.`id`, `users`.`email` FROM `users`
    INSERT INTO `communications` (`subject`, `message`, `originator_id`, `recipients`, `created_at`, `updated_at`) VALUES ('Test Message - 2018-04-05 04:58', '[\"Para 1 test\"]', 1, '[[\"adam@xxxx.com\",\"eve@xxxx.org\"]]', '2018-04-09 00:10:34', '2018-04-09 00:10:34')
Recipients - start:
["adam@xxxx.com","eve@xxxx.org"]
Completed 500 Internal Server Error in 19ms (ActiveRecord: 3.2ms)

Based on the log file, it looks like the method communication_to_all_users is never called, and up until that point, it's all fine.

Any ideas?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • while testing, try using just `deliver` instead of `deliver_later`. You can change it back once you're sure it's all working properly. – Taryn East Apr 09 '18 at 02:46
  • I already tried deliver – EastsideDev Apr 09 '18 at 02:48
  • Good, it wasn't a suggestion for fixing the problem, but a suggestion for giving us better output while we're debugging your problem. Did it give the same error/output in the console? Can you show us more of the stacktrace (that usually is printed in the logfiles after the main error message)? – Taryn East Apr 09 '18 at 02:50
  • Have you also looked at this: https://stackoverflow.com/questions/29241053/incomplete-response-received-from-application-from-nginx-passenger/29456249 – Taryn East Apr 09 '18 at 02:52
  • @TarynEast The log was the same in both cases, and I posted everything in the log file. – EastsideDev Apr 09 '18 at 05:02
  • Also, no Phusion Passenger is not the issue. The app is running fine, with the exception of this error. As clear from the logger.info statements I placed inside the methods, that the ActionMailer method does not get executed at all – EastsideDev Apr 09 '18 at 05:03
  • Does `Rails.application.secrets.email_provider_username` return something meaningful when you check it in the console? – Taryn East Apr 09 '18 at 05:14
  • yes, it returns what it's supposed to. That's not the problem – EastsideDev Apr 09 '18 at 05:15
  • Ok we need the stacktrace from the error. So you need to fix logging to figure out how to get it to produce the stacktrace. This means debug-level, or running it in the console and catching the error and displaying the stacktrace yourself. Find the way. – Taryn East Apr 10 '18 at 00:08
  • When bug-chasing, don't assume *anything* is as you think it is. Always re-test the obvious things, even if you think they're working. Even if they are so obviously simple they couldn't possibly be the problem... Nobody is immune to the small errors. Every assumption must be bash-tested. So instead of "yes it's working as expected" or "no it's not the issue" run it again, and tell me what the output is. I'm not being a PITA I'm being rigorous :) – Taryn East Apr 10 '18 at 00:08
  • Running as expected, means it produced the email address stored in the secrets file. Not sure what you mean by find the way – EastsideDev Apr 10 '18 at 00:14
  • I meant I can't dictate how to get to the stacktrace... but there are some options to try and you'll need to find the one that works... :) if it's not automatically coming up in the console, then trying upping the debug-level for your log. try tailing your logfile instead of just looking in the console output. If that fails then try stepping through your code with pry, or run the code in your rails console (`rails c` instead of `rails s` and literally running it with a begin/rescue and catch the exception and manually print out the stacktrace :) – Taryn East Apr 10 '18 at 05:02

0 Answers0