Parts not parsed with ActionMailer

45 views
Skip to first unread message

Aeris Imirhil

unread,
Jan 3, 2021, 7:33:49 AM1/3/21
to Ruby's Mail Discussion Group
Hello here,

I got a trouble with mail in a rails 6.0 application (actionmailer 6.0.3.4, ruby 2.7.2, mail 2.7.1).

My rspec test is as follow:

```
some_action_supposed_to_send_an_email
expect(ActionMailer::Base.deliveries).to_not be_empty
mail = ActionMailer::Base.deliveries.pop
expect(mail.subject).to eq 'something'
# binding.pry
expect(mail.text_part.body.to_s).to eq 'some content'
```

When I run this test, I catch a nil value on `mail.text_part` and the test fails…

Debugging with the `binding.pry` uncommented, I observed the mail structure is correct but `body.parts` is not filled correctly:

```
> mail
=> #<Mail::Message:18760, Multipart: false, Headers: <Date: Fri, 01 Jan 2021 21:51:06 +0100>, <From: monit...@example.org>, <Reply-To: nor...@example.org>, <To: moni...@example.org>, <Message-ID: <5fef8b3a43bd8...@endor.mail>>, <Subject: Service is DOWN: [ping] test>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
> mail.text_part
=> nil
> mail.body.parts
=> []
```

If I force something I don't identify very well at the moment with a `mail.part` call, `body.parts` is now correctly filled and `mail.text_part` returns correct content:

```
> mail.part
=> [#<Mail::Part:18760, Multipart: false, Headers: <Content-Type: text/plain;>>,
 #<Mail::Part:18780, Multipart: false, Headers: >]
> mail.body.parts
=> [#<Mail::Part:18760, Multipart: false, Headers: <Content-Type: text/plain;>>,
 #<Mail::Part:18780, Multipart: false, Headers: >]
> mail.text_part
=> #<Mail::Part:18760, Multipart: false, Headers: <Content-Type: text/plain;>>
=>
```

And so adding a dangling `mail.part` just before expectations allows the test to pass.
And it's not satisfying at all :D
What's the trouble in my case and why mail seems not completely parsed during rspec?

Regards,

Mikel Lindsaar

unread,
Jan 3, 2021, 7:38:51 AM1/3/21
to mail...@googlegroups.com
It's been a while since I've looked at that code, but I've got a feeling it's because your email is not a multi part email.

When you are inspecting the mail message, it's not a mime email, it's just a text/plain email.  Text plain emails don't have html parts or text parts, they just have a plain body.

```
expect(mail.body.to_s).to eq 'some content'
```

Would work the way you expect.

If you define a HTML and a Plain Text partial, then you would be sending a mime email and you could then use text and html body parts.

Best

Mikel

--

---
You received this message because you are subscribed to the Google Groups "Ruby's Mail Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mail-ruby+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mail-ruby/73de30e3-732b-47f0-8523-084fc2f00785n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages