Posting via API does not send email notification

Hello,

In my setup:

  • mailgun is set up, posting manually works, publish send by email manually works (subscriber notifications are generated and sent)
  • posting via API works
  • but posting via API does not generate subscriber notifications

Am I missing some argument or some setting?

Thanks!

What version of Ghost are you using?
Can you include an example of how you’re communicating with the API? Which params you’re passing etc…

Yes, ghost v3, using python

url = 'https://<someblog>/ghost/api/v3/admin/posts/' ## edited

	body = {
		"posts": [{
		"title": "api test html table",
		"custom_excerpt": "excerpt - api test",
		"tags": ["api-post"],
		"authors": [<some@email>], ## edited
		"visibility": "members",
		#"status": "published", ## published publishes, but no email
		#"send_email_when_published": True, ## no effect?
		"mobiledoc": mdoc,
		}]
	}
	r = requests.post(url, json=body, headers=headers)

BTW I can create the same post as draft successfully with the API
Then publish the draft & send by email manually works and generates the emails

How can I automate this?

I can also publish the draft via API (GET, then PUT status=published), but still no emails…

OK. So there is a workaround, not sure this is documented

First post a draft, then publish the draft in a second transaction, and set argument send_email_when_published

...
	r = requests.post(url, json=body, headers=headers)

	body = json.loads(r.content)
	post_id = body['posts'][0]['id']
	
	time.sleep(1)
	
	url = 'https://<someblog>/ghost/api/v3/admin/posts/'+post_id+'/?send_email_when_published=all'
	body['posts'][0]['status'] = 'published'
	body['posts'][0]['send_email_when_published'] = 'all' ## i think this is ignored
	
	r = requests.put(url, json=body, headers=headers)

@os72 immediate publishing of a post via email needs to be done via a query param, POST .../posts/?send_email_when_published=true. Note that all API’s around the members beta feature are experimental and are subject to change without notice.

Thanks @Kevin,

I tried POST w the parameter again, it’s not working for me (ghost 3.37.1). For some reason it needs the 2 transactions. (Value true or all doesn’t seem to matter) I wonder if the code somewhere looks at the transition from draft to published to trigger this

Re API, yes I saw this one in progress ( email_recipient_filter): https://github.com/TryGhost/Ghost/pull/12343

When is this part of the API expected to stabilize?

:frowning: sorry for pointing you down the wrong path! The code was intended to work with POSTs but an omission meant that it only works with PUTs

For now I think you’ll have to stick with the draft creation then publish with the ?send_email_when_published query.

When is this part of the API expected to stabilize?

No concrete timeline at the moment as the members and bulk email functionality is moving from the early prototype/beta to first-class feature. Sometime in the new year I expect.

No worries, the important thing is there is a way. (I hope the API won’t break too often…)

Looks like 3.38.2 has broken this
What’s the new way to invoke this API?

I think you need to use the email_recipient_filter={none,paid,free,all} param

Yes, I can confirm that works

But I think there is a bug. On my test site, for the free option, it misses 1 user (both manual and API). On the manual publish preview it shows 3 free members, but then the email is sent only to 2

@os72 have you checked that all 3 of those free members are subscribed?

Yes. They were subscribed all along. The behavior changed with the 3.38.2 upgrade
BTW sending to all does include all

There is also an inconsistency issue because paid post with email_recipient_filter=all seems to deliver the post content (not just the title) to everyone, not just paid subscribers. I believe that wasn’t the case earlier

I’m not sure this is intended behavior

paid post with email_recipient_filter=all seems to deliver the post content (not just the title) to everyone, not just paid subscribers

This is the expected behaviour with the new email_recipient_filter option as the recipient list has been decoupled from post visibility.

Does your free member that’s missed have a cancelled or expired plan? You may be running into this issue.

No, it’s a “pure” free member…

Delivering the notification (title + excerpt) would be expected/desired. The question is, should the content be delivered too. Note that the same free user wouldn’t be allowed to see the same content on the site