Unsubscribing a user from all emails via an API call?

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?

1 Like

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?

3 Likes

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

1 Like

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

7 Likes

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.

1 Like