Description

This function creates an MX record.

 For more information about MX record settings, read our Edit MX Entry documentation.

We strongly recommend that you use the following UAPI function instead of this function:

Examples 




https://hostname.example.com:2087/cpsess##########/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=addmx&domain="example.com"&exchange="newmx.example.com"&oldexchange="oldmail"&oldpreference="1"&alwaysaccept="auto"



$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
 
// Replace mail.example.com with an MX entry for newmx.example.com.
$add_new_mx = $cpanel->api2(
    'Email', 'addmx', 
    array(
        'alwaysaccept'          => 'auto', 
        'domain'                => 'example.com', 
        'exchange'              => 'newmx.example.com', 
        'oldexchange'           => 'mail', 
        'oldpreference'         => '5', 
        'preference'            => '15',
    ) 
);



my $cpliveapi = Cpanel::LiveAPI->new(); # Connect to cPanel - only do this once.
 
# Replace mail.example.com with an MX entry for newmx.example.com.
my $add_new_mx = $cpliveapi->api2(
    'Email', 'addmx',
    { 
        'alwaysaccept'          => 'auto', 
        'domain'                => 'example.com', 
        'exchange'              => 'newmx.example.com', 
        'oldexchange'           => 'mail', 
        'oldpreference'         => '5', 
        'preference'            => '15',
    }
);




cpapi2 --user=username Email addmx domain=example.com exchange=newmx.example.com oldexchange=mail oldpreference=5 preference=15 alwaysaccept=auto


For more information and additional output options, read our Guide to cPanel API 2 documentation or run the cpapi2 --help command.  




{
  "cpanelresult": {
    "apiversion": 2,
    "func": "addmx",
    "data": [
      {
        "statusmsg": "Added entry: example.com.\tIN\tMX\t0\tnewmx.\n\nBind reloading on server1 using rndc zone: [example.com]\n",
        "status": 1,
        "checkmx": {
          "warnings": [
          ],
          "issecondary": 0,
          "local": 0,
          "detected": "local",
          "remote": 0,
          "isprimary": 1,
          "changed": 0,
          "secondary": 0,
          "mxcheck": "auto"
        },
        "results": "Added entry: example.com.\tIN\tMX\t0\tnewmx.\n\nBind reloading on server1 using rndc zone: [example.com]\n"
      }
    ],
    "event": {
      "result": 1
    },
    "module": "Email"
  }
}



Parameters

ParameterTypeDescriptionPossible valuesExample
alwaysacceptstring

Whether the mail exchanger accepts mail for its domain.

This parameter defaults to auto.

 

  • auto — Allow cPanel to determine the appropriate role.
  • local — Always accept the domain's mail.
  • secondary — Accept mail until a higher priority mail server is available.
  • remote — Do not accept mail.
auto
domainstring

Required

The new mail exchanger's domain.

A valid domain on the cPanel account.

example.com
exchange, newmx, or exchangerstring

Required

The new mail exchanger's name.

A valid mail exchanger name.

newmx.example.com
oldexchange, oldmx, or oldexchangerstring

Required

The name of an existing mail exchanger, which the new exchanger replaces.

A valid mail exchanger on the cPanel account.mail
oldpreference or oldpriorityinteger

Required

The oldexchange exchanger's priority value.

If multiple MX entries match the oldexchange value, the system uses this parameter to find the correct entry.

A positive integer.5
preference or priorityinteger

Required

The new mail exchanger's priority value.

A positive integer.

It is common practice to set a priority value that is divisible by five. 


15

Returns

ReturnTypeDescriptionPossible valuesExample
statusmsgstringA message of success or a reason for failure.
  • A message of success that includes the mail exchanger, domain, and server name.
  • A string that describes an error.


Added entry: example.com.\tIN\tMX\t0\tnewmx.\n\nBind reloading on server1 using rndc zone: [example.com]\n


statusBooleanWhether the function succeeded.
  • 1 — The function succeeded.
  • 0 — The function failed.
1
checkmxhashA hash of the mail exchanger's data.This hash includes the warnings, issecondary, local, detected, remote, isprimary, changed, secondary, and mxcheck returns. 

warnings

string

Warning messages, if any exist.

The function returns this value in the checkmx hash.

One or more warning messages.


Auto Detect of MX configuration not possible due to non-resolving MX entries.  Defaulting to last known setting: local.


issecondary

Boolean

Whether the mail exchanger is a secondary exchanger.

The function returns this value in the checkmx hash.

  • 1 — Secondary.
  • 0 — Not secondary.

0

local

Boolean

Whether the mail exchanger is a local exchanger.

The function returns this value in the checkmx hash.

  • 1 — Local.
  • 0 — Not local.
0

detected

string

The mail exchanger type.

The function returns this value in the checkmx hash.

  • auto
  • local
  • secondary
  • remote
auto

remote

Boolean

Whether the mail exchanger is a remote exchanger.

The function returns this value in the checkmx hash.

  • 1 — Remote.
  • 0 — Not remote.
0

isprimary

Boolean

Whether the mail exchanger is the primary mail exchanger.

The function returns this value in the checkmx hash.

  • 1 — Primary.
  • 0 — Not primary.
0

changed

Boolean

Whether a change occurred during the function.

The function returns this value in the checkmx hash.

  • 1 — Change occurred.
  • 0 — No change.
1

secondary

Boolean

Whether the mail exchanger is a secondary exchanger.

The function returns this value in the checkmx hash.

  • 1 — Secondary.
  • 0 — Not secondary.

0

mxcheck

string

The mail exchanger type.

The function returns this value in the checkmx hash.

  • auto
  • local
  • secondary
  • remote
auto
resultsstringA message of success or a reason for failure.
  • A message of success that includes the mail exchanger, domain, and server name.
  • A string that describes an error.


Added entry: example.com.\tIN\tMX\t0\tnewmx.\n\nBind reloading on server1 using rndc zone: [example.com]\n


reasonstring

A reason for failure.

This function only returns a reason value if it failed. 


A string that describes the error.

This is an error message.
resultBoolean

Whether the function succeeded.

  • 1 — The function succeeded.
  • 0 — The function failed.
1