Unsubscribing a user from all emails via an API call?

AI-generated summary

In a support discussion titled ‘Unsubscribing a user from all emails via an API call?’, jezra seeks to automatically unsubscribe users from Discourse emails when they opt out of site-generated emails via SSO. Stephen questions whether users might want to opt out of only one type of email, suggesting a preference management page instead. jezra confirms that policy requires full unsubscribe from Discourse emails upon site email opt-out. Later, jezra shares that they successfully implemented this by reverse-engineering the API using the reverse engineering guide, and provides a Ruby payload example targeting user email settings via a PUT request to https://DISCOURSEHOST/u/USERNAME.json. Thottempudi reports that their implementation did not reflect changes in the user’s email preferences UI. RGJ notes that passing api_key and api_username via query parameters is deprecated, recommending use of Api-Key and Api-Username headers instead, per current API documentation. The thread highlights both implementation success and common pitfalls in automating Discourse email settings via API.

The site I maintain uses Discourse via SSO. When a user of my site unsubscribes from site generated emails, I would also like to unsubscribe the user from Discourse emails. It should be possible to determine what is required by looking at the API calls that are made when user email settings are changed via the UI, but I was wondering if there is an easier way?

Will it always be the case that they don’t want to see site emails and community emails?

If the possibility exists that they will want to opt out of one and not both why not simply link to a page where they can manage their email preferences for discourse?

Yes. Management has told me to unsubscribe users from discourse email when the user unsubs from site emails.

For anyone interested in a follow up:
My need was met by following the reverse engineering guide. It was a fairly straight forward process to collect the contents of the PUT payload.

in Ruby, the payload I ended up with is:
payload = {mailing_list_mode: false, mailing_list_mode_frequency: 1, email_digests: false, email_in_reply_to: false, email_messages_level: 2, email_level: 2, email_previous_replies: 2 }

the ‘mailing_list_mode_frequency’ can probably be excluded since mailing_list_mode is false.
The payload is then PUT to https://DISCOURSEHOST/u/USERNAME.json?api_key=DISCOURSE_SYSTEM_API_KEY&api_username=system

I have implemented same logic , but values are not reflected in website User email preferences.

Note that the method of passing api_key and api_username via query parameters is now deprecated, you should use Api-Key and Api-Username headers nowadays.