Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
Mailgun API
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mark
Mailgun API
Commits
4583897b
Commit
4583897b
authored
Jul 11, 2024
by
mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加脚本
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
126 additions
and
0 deletions
+126
-0
api.js
api.js
+126
-0
No files found.
api.js
0 → 100644
View file @
4583897b
/**
* @fileoverview 使用 API 批量管理 Mailgun 发件域
* @author Mark
* @version 1.0.0
* @date 创建日期 (2024-05-24)
* @lastModified 最后修改日期 (2024-05-24)
*/
const
username
=
'
%USERNAME%
'
const
password
=
'
%PASSWORD%
'
function
getAuth
()
{
return
`Basic
${
btoa
(
`
${
username
}
:
${
password
}
`
)
}
`
}
function
getDomainPath
(
url
,
domain
)
{
return
url
.
replace
(
'
{domain}
'
,
domain
)
}
function
request
(
method
,
path
,
formData
)
{
let
headers
=
new
Headers
();
headers
.
append
(
'
Authorization
'
,
getAuth
())
return
fetch
(
`https://api.mailgun.net/
${
path
}
`
,
{
method
:
method
,
headers
,
body
:
formData
,
}).
then
(
r
=>
r
.
json
())
.
then
(
data
=>
{
console
.
log
(
data
)
return
data
})
}
async
function
updateDomainTracking
(
domain
,
trackingType
,
activeValue
)
{
const
formData
=
new
FormData
()
formData
.
append
(
'
active
'
,
activeValue
)
await
request
(
'
PUT
'
,
getDomainPath
(
`v3/domains/{domain}/tracking/
${
trackingType
}
`
,
domain
),
formData
)
}
async
function
createWebhooks
(
domain
,
id
,
...
urls
)
{
const
form
=
new
FormData
()
form
.
append
(
'
id
'
,
id
)
urls
.
forEach
(
url
=>
form
.
append
(
'
url
'
,
url
))
const
data
=
await
request
(
'
POST
'
,
getDomainPath
(
'
v3/domains/{domain}/webhooks
'
,
domain
),
form
)
console
.
log
(
data
)
}
function
initiateX509Certificate
(
domain
)
{
return
request
(
'
POST
'
,
getDomainPath
(
`v2/x509/{domain}`
,
domain
))
}
function
getX509CertificateStatus
(
domain
)
{
return
request
(
'
GET
'
,
getDomainPath
(
`v2/x509/{domain}/status`
,
domain
))
}
function
updateDomain
(
domain
,
data
)
{
var
form
=
new
FormData
();
for
(
var
key
in
data
)
{
form
.
append
(
key
,
data
[
key
]);
}
return
request
(
'
PUT
'
,
getDomainPath
(
`v4/domains/{domain}`
,
domain
),
form
)
}
async
function
getDomains
()
{
const
data
=
await
request
(
'
GET
'
,
'
v4/domains
'
)
}
async
function
getDomainDetails
(
domain
)
{
return
await
request
(
'
GET
'
,
getDomainPath
(
'
v4/domains/{domain}
'
,
domain
))
}
async
function
activeHttpsTracking
(
webPrefix
,
domain
)
{
const
x509Domain
=
`
${
webPrefix
}
.
${
domain
}
`
initiateX509Certificate
(
x509Domain
)
await
delay
(
2000
)
getX509CertificateStatus
(
x509Domain
)
updateDomain
(
domain
,
{
web_scheme
:
'
https
'
})
}
function
delay
(
ms
)
{
return
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
));
}
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////// 以下是执行批量管理发送域的示例 //////
const
mailgunWebhookUrl
=
[
'
https://test.parcelpanel.com/mailgun/openAndClick
'
,
'
https://prod.parcelpanel.com/mailgun/openAndClick
'
,
]
const
domainList
=
[
'
x1.parcelpanel.net
'
,
'
x2.parcelpanel.com
'
,
]
domainList
.
forEach
(
async
domain
=>
{
await
updateDomain
(
domain
,
{
web_scheme
:
'
https
'
})
})
domainList
.
forEach
(
async
domain
=>
{
// 开启 https tracking
const
{
web_prefix
,
web_scheme
}
=
(
await
getDomainDetails
(
domain
)).
domain
if
(
web_scheme
!==
'
https
'
)
{
await
activeHttpsTracking
(
web_prefix
,
domain
)
}
await
updateDomainTracking
(
domain
,
'
open
'
,
'
true
'
)
await
updateDomainTracking
(
domain
,
'
click
'
,
'
true
'
)
await
createWebhooks
(
domain
,
'
clicked
'
,
...
mailgunWebhookUrl
)
await
createWebhooks
(
domain
,
'
opened
'
,
...
mailgunWebhookUrl
)
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment