ActionMailer - limit on number of recipients? - ruby-on-rails

I've got Capistrano set up to send emails after deploying my RoR (2.3.8) application. I have a config/cap_mailer.rb file that basically looks like:
ActionMailer::Base.smtp_settings = {
:address => my,
:port => exchange,
:domain => server,
:authentication => settings,
:user_name => are,
:password => here
}
class CapMailer < ActionMailer::Base
def deploy_notification(cap_vars)
recipients cap_vars[:notify_emails]
from 'deploy#my.org'
subject "New app!"
body "Deployed application...blah blah blah"
end
end
Then, in my deploy.rb file, I have the following:
require 'config/cap_mailer.rb'
...
desc "Email recipients of deployment"
task :notify do
puts " * Sending notification email"
set :notify_emails, ["test1#my.org", "test2#my.org", etc.]
CapMailer.deliver_deploy_notification(self)
end
Now this all works fine and dandy......until I put more than 7 email addresses in the :notify_emails array. Up to 7 works fine, but when I put 8 or more (all valid addresses), the email gets screwed up a little bit (still goes through to the first 7, at least). Looking at the email header, it shows that it is cutting off the 8th (and 9th, 10th, ...) address from the 'To:' and putting it in the message body.
HEADER:
thread-index: AcyaZxlga08L9p35QYKJ22aiGG2zeA==
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By ...
Received: from exchange.my.org ([ip address]) by ...; Thu, 3 Nov 2011 14:28:08 -0600
Date: Thu, 3 Nov 2011 14:28:08 -0600
From: deploy#my.org
To: test1#my.org,
test2#my.org,
test3#my.org,
test4#my.org,
test5#my.org,
test6#my.org,
test7#my.org
HEADER:
BODY:
test8#my.org
Message-Id:
<4eb2f95816341_135ff800c21ac130#my_box.local.tmail>
Subject: New app!
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Return-Path: deploy#my.org
X-OriginalArrivalTime: 03 Nov 2011 20:28:08.0494 (UTC)
FILETIME=[19601CE0:01CC9A67]
Deployed application...blah blah blah
BODY:
In addition, the subject does not appear on the email, even though the "error text" in the message body shows the correct subject.
Does anyone have any idea why this is happening? What is it about having 8 or more recipients that breaks it? I've Googled around and can't find anything about ActionMailer having a limit on the number of recipients (even if there were, that's a small limit). Is there something I'm missing? Any help is appreciated! I really need to be able to send to 8 or more recipients.
UPDATE: Setting the recipients directly with an array of 8 or more addresses still breaks things, so this clearly seems like a problem with ActionMailer and not Capistrano.
recipients ["test1#my.org", "test2#my.org", "test3#my.org", "test4#my.org",
"test5#my.org", "test6#my.org", "test7#my.org", "test8#my.org", "test9#my.org"]

The 7th email address is not followed by a comma, which could be the problem. Try passing a string to recipients, like cap_vars[:notify_emails].join(','), with no newlines.

Try passing a string to recipients without commas (there have been reports of commas as a problem), like:
cap_vars[:notify_emails].join(' ')

With all respect some simple things that you might overlook in debugging difficult errors:
If its mail server related install it on another production server? If you not sure it is the mail server acting up looking for another error may be of no help.
Try upgrade rails / ruby ? Perhaps its a bug in the code

Related

Rails 4/5 Sending Dynamic ActionMailer::Base.mail email With Attachment labeled Noname

I've taken a look at similar posts that mostly deal with sending an attachment by creating a view and controller, such as:
PDF attachment in email is called 'Noname'
but I've got a process that generates files in the background dynamically and need to attach it to a recipient list using ActionMailer::Base.mail. Below is the code:
def send_email(connection)
email = ActionMailer::Base.mail(to: connection['to'], from: connection['from'], subject: 'Sample File', body: "<p>Hello,</p><p>Your data is ready</p>", content_type: 'multipart/mixed')
email.cc = connection['cc'] if connection['cc'].present?
email.bcc = connection['bcc'] if connection['bcc'].present?
#files.each do |file|
report_file_name = "#{#start_time.strftime('%Y%M%dT%I%m%s')}_#{file[0]}.xlsx"
file_location = "#{Rails.root}/tmp/#{report_file_name}"
email.attachments[report_file_name] = File.open(file_location, 'rb'){|f| f.read}
end
email.deliver if email
end
I can see in the logs that it's sending with the content but assume it's sending as Noname because it can't find the view. Any way to get this to work successfully?
Below is the sample output:
Sent mail to sample#sample.com (383.9ms) Date:
Thu, 13 Oct 2016 08:47:30 -0400 From: Sample To:
Recipient Message-ID:
<57ff326270f15_421f1173954919e2#ulinux.mail> Subject: Sample File
Mime-Version: 1.0 Content-Type: multipart/mixed; charset=UTF-8
Content-Transfer-Encoding: 7bit
-- Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;
filename=20161012T08101476259208_Data.xlsx
Content-Transfer-Encoding: base64 Content-Disposition: attachment;
filename=20161012T08101476259208_Data.xlsx Content-ID:
<57ff326270f15_421f1173954919e2#ulinux.mail>
UEsDBBQAAAAIAO.. ... ...ADUFQAAAAA=
Update - I noticed if I use email.content_type = 'text/plain' - the attachment comes through successfully. For me, this works, though I'd appreciate later being able to style my emails with HTML
I presume this works because it prevents Rails from its usual gleaning/autointerpreting process. I'd certainly like to see a multipart/mixed or html compatible version work here though.
Update 2 This only fixed the issue artificially in the rails_email_preview gem, which renders the emails to a new tab in development. In production, this simply and understandably prints the details and the presumably base64-encoded file, so question remains open.
I have meet this problem too, after some investigation, it seems in Rails 4, you can't call attachments method after calling mail method, otherwise the content_type of the mail message object won't have boundary information so that the attachments part can't be parsed correctly in the received email.
I think digging into the actionmailer source code and you should be able to find a solution, either by override the default mail method or set the correct boundary info manually.
But for quick resolving this problem, I thought out a not elegant work around by using meta programming: define a delegation class which inherits ActionMailer::Base.
class AnyMailer < ActionMailer::Base
# a delegation mailer class used to eval dynamic mail action
end
Then eval this class with defining an arbitrary method to perform the email sending.
def send_email(connection, files)
AnyMailer.class_eval do
def any_mailer(connection, files)
files.each do |file|
report_file_name = :foo
file_location = :bar
attachments[report_file_name] = File.open(file_location, 'rb'){|f| f.read}
end
mail(to: connection['to'], from: connection['from'], subject: 'Sample File', body: "<p>Hello,</p><p>Your data is ready</p>")
end
end
AnyMailer.any_mailer(connection, files).deliver_now
end
Attention, you don't need to specify the content_type as 'multipart/mixed', ActionMailer will handle it correctly. I tried to specify it explicitly but get messed up email content instead.
This has been driving me insane.
Make sure you have a well formed .html template and a .text template if you are using mailer views.
Minimal errors in either of them will render the entire email as a noname attachment.
You might don't have mailer.text.erb file along with mailer.html.erb file.
Add it and your mail will be multipart.

Rails 4 ActionMailer not sending emails in development mode

First, let me specify that I am aware that ActionMailer does NOT send emails by default in development mode.
Also, I took a look at similar questions, and none of them was relevant to my situation.
So, here we go:
I am following this Site Point tutorial by Ilya Bodrov-Krukowski, from March 26, 2015 in order to install and setup Devise on my Rails 4 app.
Because we activated Devise's confirmable module, I need to send emails in development to allow users who signup to activate their account and login.
Whenever I create a new user (through http://localhost:3000/users/sign_up), I do get the following alert:
A message with a confirmation link has been sent to your email
address. Please follow the link to activate your account.
However, I never actually receive the email with the confirmation link.
——————————
UPDATE
In my development.log file, I can see that the email was "sent":
Devise::Mailer#confirmation_instructions: processed outbound mail in 172.1ms
Sent mail to hello#mydomain.com (54.4ms)
Date: Tue, 25 Aug 2015 12:15:15 -0700
From: contact#anotherdomain.com
Reply-To: contact#anotherdomain.com
To: hello#mydomain.com
Message-ID: <55dcbec3a1ef8_33b93fcffc4988f493685#XXXXXX.local.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome hello#mydomain.com!</p>
<p>You can confirm your account email through the link below:</p>
<p>Confirm my account</p>
[1m[35m (0.8ms)[0m commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 560ms (ActiveRecord: 1.7ms)
And, when I copy and paste the link from the log into my browser, then I get the following alert:
Signed in successfully.
So, it seems the only thing that is not working is the actual sending / receiving of the email.
——————————
As recommended in the tutorial, I followed Rails documentation regarding ActionMailer configuration and here is what I have in my config/environments/development.rb file:
config.action_mailer.delivery_method = :sendmail
# Defaults to:
# config.action_mailer.sendmail_settings = {
# location: '/usr/sbin/sendmail',
# arguments: '-i -t'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'hello#mydomain.com'}
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Obviously, I am missing something, but I cannot figure out what (I am very, very junior with Rails).
Any idea?
Mail is not sent in development by default. To enable this you have to set config.action_mailer.perform_deliveries = true in development.rb file which I see you already have.
You may also have to enable config.action_mailer.raise_delivery_errors = true to raise delivery errors. That way, you will be able to see if there are any errors in the email delivery process.
Also, double check your default from if that has a valid email.
Also try switching the delivery method to sendmail:
config.action_mailer.delivery_method = :sendmail
This should solve your issue.
You need a mail server for Action Mailer to send emails.
such as
MailGun
which has a free account and a gem which makes set up easy.
mailgun_rails

Newlines resolved as =0A in Sendgrid X-SMTPAPI header

I am using Sendgrid to send email to a mailing list, using the X-SMTPAPI header to specify the multiple recipients. From the Sendgrid documentation "Headers must be wrapped to keep the line length under 72."
I am using the ActionMailer to send emails, and setting the X-SMTPAPI header using the headers method. To keep lines less than 72 characters, I have tried replacing each comma with a comma+newline+space. For example,
headers["X-SMTPAPI"] = {
:to => ['user1#example.com','user2#example.com','user3#example.com','user4#example.com','user5#example.com','user6#example.com']
}.to_json.gsub(',',",\n ")
Instead of getting newlines in my header, I am getting the following (from the log file)
X-SMTPAPI: {"to":["user1#example.com",=0A "user2#example.com",=0A "user3#example.com",=0A "user4#example.com",=0A "user5#example.com",=0A "user6#example.com"]}
Note that the \n characters are being replaced with =0A. This sequence is rejected as invalid by the Sendgrid server.
Any ideas what I can do to get the proper newlines into the header?
Edit:
I tried adding a "puts headers" to see what is being set in the headers. Then is what I found
Date: Sat, 13 Apr 2013 18:21:36 -0400
Message-ID: <5169da701cd26_5343fe1776afc50749b4#saunders.mail>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {"to":["user1#example.com",=0A "user2#example.com",=0A
"user3#example.com",=0A "user4#example.com",=0A "user5#example.com",=0A
"user6#example.com"]}
Note the newlines I am adding are still showing up as "=0A". But something appears to be adding wrapping on its own. Is this wrapping automatic, and sufficient to keep my header line length from exceeding the requirements?
ActionMailer actually will handle folding and encoding the lines for you if you give it the proper spacing to do so. You should use JSON.generate to give it the spacing:
Ex.
headers["X-SMTPAPI"] = JSON.generate({
:category => "welcome_email",
:to => ['user1#example.com','user2#example.com','user3#example.com','user4#example.com','user5#example.com','user6#example.com']
}, :indent => ' ')
Which would result in:
X-SMTPAPI: { "category":"welcome_email", "to":[ "user1#example.com",
"user2#example.com", "user3#example.com", "user4#example.com",
"user5#example.com", "user6#example.com"]}
As you can see, when ActionMailer encounters whitespace, it will wrap things for you - no need for the usual \r\n.
It seems like characters in headers have to be encoded according to the rules of RFC 2047 [14].
Accodingly to ASCII table %0A states for \n

Sending 1 attachement, recipients reports 2 attachements (ATT0001.c added) - Rails 3, ActionMailer

To exchange data with another system we send the data as an email attachment to a dedicated address. The email is generated using ActionMailer v3.2.12.
The problem is that when the email arrives at its destination, a redundant attachment named ATT00001.c is a part of the email, in addition to the attachment we created. This causes issues with the import routine at the other end.
A big part of the problem is that we know almost nothing about how the email is being handled at the destination . We also dont know what type of email server is in use and dont have access to check what the email actually looks like when it arrives. We can send it to one of our own addresses and it looks fine there.
I know this is not a lot to go on, but perhaps one of you guys have seen these ATT00001-attachments being added to machine generated emails before.
config.action_mailer.smtp_settings
address: smtp.<mailprovider>.com
port: 587
domain: ourdomain.com
authentication: login
user_name: <removed>
password: <removed>
enable_starttls_auto: false
Update:
We've been able to obtain a copy of the problematic email and it shows the email body rendered after the attachment as an attachment of its own.
We've tried setting ActionMailer's parts_order to make sure the attachment is generated after the email body, it did not help.
Update2:
Sending to my gmail account and showing original raw data I get this.
SENT MAIL
in receipt response from recipient to the correct attachment (the autocreated one creates an error log entry)
(...) cut: to from and through email header information
Mime-Version: 1.0
Content-Type: multipart/mixed;
charset=UTF-8
Content-Transfer-Encoding: 7bit
--
Date: Thu, 28 Feb 2013 12:15:23 +0100
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="thefile.mscons"
Content-ID: <512f3c4b6e875_a8f756dcc642fe#bjorns_arch.mail>
VU5BOisuPyAnVU5CK1VOT0M6Mys3MDgwMDAzNDExNzE2OjE0OlRJTUVSKzcw
... many more lines like this ...
ODAwMDUwNTEyMTc6MTQ6VElNRVIrMTMwMjI4OjEyMTUrUE9XRVNUMTMwMjI4
----
This is with body nil in actionmailer
Next is a RESPONSE from the recipient system, sent to my gmail. It's a receipt on the correct attachment (the extra attachement generates an error, flushing their system)
RECEIVED MAIL
(..) unintersting header stuff with addresses
Content-Disposition: attachment;
filename="afilename.txt"
Content-Transfer-Encoding: base64
Content-Type: Application/EDIFACT; charset="iso-8859-1"
Mime-Version: 1.0
Date: Sat, 16 Feb 2013 11:07:10 +0100
From: ediel#example.com
To: ***#gmail.com
Subject: thesubject
Message-ID: <511f5a53.850a700a.2fa0.2a0eSMTPIN_ADDED_BROKEN#mx.google.com>
X-TM-AS-Product-Ver: IMSS-7.0.0.6298-6.8.0.1017-19380.002
X-TM-AS-User-Approved-Sender: Yes
X-Greylist: Sender is SPF-compliant, not delayed by
milter-greylist-4.0 (isp-app27-vm.isp.example.com [213.239.116.46]);
Sat, 16 Feb 2013 11:07:11 +0100 (CET)
X-ExampleIKT-MailScanner-Information: Please contact the ISP for more information
X-ExampleIKT-MailScanner-ID: r1GA7BqD021150
X-ExampleIKT-MailScanner: Found to be clean
X-ExampleIKT-MailScanner-From: ediel#example.com
X-Spam-Status: No
VU5BOisuPyAnVU5CK1VOT0M6Mys3MDgwMDA1MDUxMjE3OjE0OlRJTUVSKzcwODAwMDM0MTE3MTY6
.. more..
pUSU1FUisxJ1VOVCszKzEnVU5aKzErMjAxMzAyMDAyNDg1Nzcn
something suspicious with the Content-Type? Is a new (empty) attachment generated from the stuff prior to -- in the sent email?
I believe this is to do with inline attachments and Exchange server. Some clients, Apple Mail in particular allow you to add inline attachments, that is, a MIME attachment sandwiched in between text/body parts of an email. Exchange server expects that all attachments appear after any text portion of a mail.
Everything after the attachment in your mail gets treated as an attachment, so the body gets stuffed into a file and named as you reported it to be named. Seeing as you're using ActionMailer, see this answer and possible this answer, which explains that you need to switch the order of the lines of code, and possibly play with some other settings.
Our problem is solved, though unfortunately I cant say what caused the redundant attachment. We worked around it by sending a non-multipart email that contained only the attachment. This solution obviously wont work for people who need to send a multipart email.
Sending non-multipart email with an attachment in Rails is not straight forward. You cant use the attachment helper method and a blank body, you need to put the attachment content in the email body and manually specify the disposition.
class MailMan < ActionMailer::Base
def test
attachment_content = "my attachment"
disposition = "attachment; filename=\"test.txt\""
mail(body: attachment_content, content_disposition: disposition)
end
end

Ruby on rails Devise Send Mail

I've been following this tutorial on how to setup Devise. I'm able to make a new User&Email and hit submit. It says "User succesfully created" and in the server log I can see the email that was just sent with subject, email adress to user, name of user etc and it says "Completed 302 Found in 434ms". I don't know what the problem is though because I don't receive an email, although the user gets saved to the database. I'm using gmail to send email.
This is the server log (Changed email addresses to --- for privacy):
Sent mail to ------#-------.com (389ms)
Date: Fri, 12 Aug 2011 09:11:45 -0500
From: ------#-----.com
To: -------#-------.com
Message-ID: <4e4534a175b12_b3e----6b6f998510d8#ubuntu.mail>
Subject: Registered
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Thank you for registering!
Redirected to http://127.0.0.1:3000/users/4
Completed 302 Found in 434ms
Anyone know how it could be solved or recognize the problem?
Have you checked your config/environment/development.rb file? Make sure that config.action_mailer.perform_deliveries is set to true. See below:
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :sendmail #:smtp
Otherwise, it's likely actually being sent and your email account is dropping the message silently. Turn up the first option above (…raise_delivery_errors = true) to see.

Resources