API:Email

From MediaWiki.org
Jump to: navigation, search
Tools clipart.png This page is part of the MediaWiki API documentation.
Language: English  • Deutsch
MediaWiki API

Quick overview:

v · d · e
MediaWiki version: = 1.14

Token[edit | edit source]

To send an email, an email token is required. This token is equal to the edit token and the same for all recipients, but changes at every login. An email token can be obtained as follows: Obtaining an email token

Sending email to users[edit | edit source]

You can send email to users who have a confirmed email address with action=emailuser. Sending email is subject to rate limits.

Parameters[edit | edit source]

  • target: User to send email to
  • subject: The subject of the message
  • text: The message
  • token: The token obtained in the previous request. Take care to encode the + as %2B
  • ccme: If set, a copy of the email will be sent to you

Examples[edit | edit source]

Note: In this example, all parameters are passed in a GET request just for the sake of simplicity. However, action=emailuser requires POST requests; GET requests will cause an error.

Sending an email to User:Catrope

Possible errors[edit | edit source]

In addition to the usual stuff:

  • code: cantsend
    • info: You're not logged in or you don't have a confirmed email address, so you can't send email
  • code: blockedfrommail
    • info: You have been blocked from sending email
  • code: usermaildisabled
    • info: User email has been disabled
  • code: notarget
    • info: You have not specified a valid target for this action
  • code: noemail
    • info: The user has not specified a valid email address, or has chosen not to receive email from other users

Checking emailable status[edit | edit source]

Before trying to send an email, it is recommended to check if the user is emailable first. To do this, you can execute a list query on the user (or several users at once). Here is an example using Ajax:

$.ajax({
  url: wgScriptPath + '/api.php?',
  data: 'action=query&list=users&ususers='+encodeURIComponent(wgTitle)+'&usprop=emailable&format=json',
  dataType: 'json',
  success: function( data ) {
    if ( typeof data.query.users[0]['emailable'] !== 'undefined' ) {
      emailable = true;
    } else {
      emailable = false;
    }
  }
});

If you are testing from a client-side script, it is also possible to simply check for the existence of the t-emailuser list item:

emailable = $('#t-emailuser').length ? true : false;