Email not delivering with error "Cannot read property 'toJSON' of null"

I just ran into an error when trying to send out a newsletter: “Cannot read property ‘toJSON’ of null”.

My Ghost install is self-hosted on Digital Ocean and emails are sent out via Mailgun. I’ve sent 8 weekly emails out so far successfully, but now I received this error:

When tapping on retry, I get this:

What’s my next step to debug this? It’s impossible that they were “too many recipients” as it’s 12. Thanks for your ideas.

Hey @s2imon :wave: It would be very useful to find out where the toJSON error is coming from internally.

Are you able to search the logs on your instance for this error? You can locate the logs at ./content/logs/ folder relative to the root of your Ghost instance. To make the search quick you can run something like grep "toJSON" -RI ./content/logs and that would point you to exact entry with more details. Alternative approach is to run ghost log -f and try reproduce the error, it should than appear in the output of your terminal.

2 Likes

Hi @naz - wonderful of you to answer this fast. I went with

The following is my output, unfortunately so far I’m not able to make much sense of it. Only that supposedly there were too many recipients. Do you know where I’m having a problem?

[2020-03-30 19:30:58] INFO "PUT /ghost/api/v3/admin/emails/5e80fab589c33736e3ce1c28/retry/" 200 28ms
[2020-03-30 19:30:58] WARN

NAME: InternalServerError
MESSAGE: The server has encountered an error.

level: normal

"The email service was unable to send an email batch."
InternalServerError: The server has encountered an error.
    at new GhostError (/var/www/ghost/versions/3.9.0/core/server/lib/common/errors.js:10:26)
    at mailgunInstance.messages.send (/var/www/ghost/versions/3.9.0/core/server/services/bulk-email/index.js:103:45)
    at Request.callback (/var/www/ghost/versions/3.9.0/node_modules/async/dist/async.js:4617:26)
    at IncomingMessage.res.on (/var/www/ghost/versions/3.9.0/node_modules/mailgun-js/lib/request.js:331:19)
    at IncomingMessage.emit (events.js:203:15)
    at IncomingMessage.EventEmitter.emit (domain.js:448:20)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Error: Too Many Recipients
    at IncomingMessage.res.on (/var/www/ghost/versions/3.9.0/node_modules/mailgun-js/lib/request.js:327:17)
    at IncomingMessage.emit (events.js:203:15)
    at IncomingMessage.EventEmitter.emit (domain.js:448:20)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

[2020-03-30 19:30:59] INFO "GET /ghost/api/v3/admin/emails/5e80fab589c33736e3ce1c28/" 200 15ms

I’ve also reached out to see whether Mailgun has any idea where I’m wrong, I can’t find anything wrong on that side, but still waiting. Thank you!

Hi @s2imon, what kind of subscription do you have at mailgun? Also can you give a ballpark of how many members you have on the site? Also, did you do any modifications to the core? I’d expect for this error to happen you’d go over a 1000 emails/batch limit :thinking:

1 Like

Thanks again for the fast answer, was waiting for Mailgun to get back to me, they’re saying it’s not related to my account. I’m on their promotional lowest tier (which they’re about to erase).

Anyway, I have around 15 subscribers currently, so it can’t be that. Within the last month (since it last worked like a charm) I made

  • some changes to my theme recently, it can’t be theme level the disruption, can it?
  • and renewed the https manually

That’s it, no modifications to the core whatsoever. Thanks for the support, greatly apprectiated as it’s a little frustrating right now.

Hm. None of the factors you listed above seem to have effect on the email functionality. Are you able to send out any of the newsletter emails for newer posts? What’s the exact environment for your Ghost instance?

I will try again with the next one this weekend. It worked until this issue, but I’m guessing that it will hit the same problem.

It’s everything default as suggested in the guides “Ubuntu Ghost on 18.04” on Digital Ocean. Did you mean some specific details? Will happily get them for you.

Just had another email from Mailgun saying that my account had no restrictions at the moment.

P.s. just tried to update Ghost to see whether that would reset it, but no luck.

Don’t know if that helps, but the transactional emails, also via Mailgun, are going out (the test email sent to myself went out also).

1 Like

We didn’t see any similar error happening to other instances. Would be good if you could inform when this ever happens again. For now I’d put this issue on hold if nothing breaks and there’s no clear way to replicate it :slight_smile:

hey @naz thanks for following up. Unfortunately, now that I tried again, it gave back exactly the same errors.

Mailgun says it can’t be on their side, posting here in order to give a complete overview:

Im the escalation tech for Mailgun support. I was looking over this ticket and I dont belive that the probation status was the cause of the issue. In order to troubleshoot, we will need additional information.

  • How are you sending, API or SMTP?
  • If sending via API, are you capturing the respones from Mailgun?
  • Are you getting any error messages?

We are not familiar with ghost.org but I checked our logs for the last 7 days, and Im not showing any requests are being submited to us. My guess is that there is an issue with the configuration of the sending application. Unfortunatley, we cant troubleshoot third-party services or sending applications but we can try and point you in the right direction.

What can I do next, any ideas to debug?

Thanks so much, being quite restless to get it back up.

Hey @s2imon. Lets narrow this error down to a specific place in code and see what kind of data is causing this error in your specific case.

From the logs you provided it all comes down to this line throwing the error - Ghost/index.js at 3.9.0 · TryGhost/Ghost · GitHub .

Lets can put additional logging in your instance’s code to figure out what Ghost is trying to send out. Add following code on line 97:

common.logging.info('Recipients count: ', toAddresses.length);
common.logging.info('Recipient variables: ', Object.keys(recipientVariables).length);

This would give us a clue on the amount of emails that are going out. It’s best if you could run this code on your local or experimental instance with the same error to avoid problems on production.

You can also try and log other variables that are passed to mailgunInstance.messages().send() function to see if there’s anything out of normal with the post you are trying to send emails to.

The best way to tackle this problem is having a reproducible steps on a clean Ghost instance which you can set up using ghost install local on your computer. Otherwise it’s taking too much time to guess what is going on here :/

Hey Naz, thanks for the suggestions, will try asap and get back to you on the result!

Naz, I went back to it this week after I needed to let it simmer for a bit to let the frustration wear off. Turns out currently it works, without me having done anything except for some simple theme work. So unfortunately that leaves me (us) without the learning I was hoping for, I’m keeping my fingers crossed that it’ll not return.
@naz thanks a lot for the support.

1 Like

No worries :slight_smile: Hopefully this weird bug was just a glitch in a matrix!

Hi!

We have been facing the same errors with a list of 16 subscribers. As per your suggestion i’ve added the following logging statements at the suggested place

logging.warn("recipients count: " + recipients.length);
logging.warn('Recipient variables: ', Object.keys(recipientVariables).length);
logging.warn('bulkemailconfig: ', bulkEmailConfig);

The output I get is

So seems like the bulkemailconfig isn’t getting set properly… Any suggestions as to what next we can do?

this went away on its own after some time. coincidentally this was after our request to mailgun to remove the probationary status from our account was accepted. Could the two be related?