SFMC Send Email By REST API

因为工作原因,项目需要使用Salesforce中Marketing Cloud发送邮件的功能。
仅有管理员提供的一个账号。
我们仅仅使用管理员提供的邮件模板来发送邮件
以下记录在使用过程中的一些心得。

REST API官方文档: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/routes.htm
REST API社区文档: https://api.mcexperts.ninja/
1.Get Token
文档链接: https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/access-token-s2s.htm
Request: 
POST https://${YOUR_SUBDOMAIN}.auth.marketingcloudapis.com/v2/token
{
"grant_type": "client_credentials",
"client_id": "7a9j47upktedde30uedl822p",
"client_secret": "1955278925675241571",
"scope": "email_read email_write email_send",
"account_id": "12345"
}

Response:
{
    "access_token": "eyJhbGciOiJIUz********************************FliR3lsM1plc2ZjWmdBYXN2SGptT2ciLCJjbGllbnRfaWQiOiJ5a3l4Zjducjh2eXZyMGwwaGUxY3NoMWQiLCJlaWQiOjExMDAwNjQ3NSwic3RhY2tfa2V5IjoiUzExIiwicGxhdGZvcm1fdmVyc2lvbiI6MiwiY2xpZW50X3R5cGUiOiJTZXJ2ZXJUb1NlcnZlciJ9.lNw24VjMDixo5uIj8ChN0VPUw7FglRJ9POXsRlkHJWc.0NwdydHyjyOMh4kzT3g2Cm5b1sFZvIdZWqu2auO4AWhfLGIqL38taoJkkbeQx6mfcZpWRRvp0BRUxG4lPCRA1IUh0yMWDNrvLmw-9CrPyJMCF_hJYbxMdVxN1WsQBy1eJL0xb_OGiGW_Zhevq9zqhfzbIyUr9nLJ1F7GyTZv3yY-4M9snVi",
    "token_type": "Bearer",
    "expires_in": 1079,
    "scope": "offline documents_and_images_read documents_and_images_write saved_content_read saved_content_write automations_execute automations_read automations_write journeys_execute journeys_read journeys_write email_read email_send email_write push_read push_send push_write sms_read sms_send sms_write social_post social_publish social_read social_write web_publish web_read web_write audiences_read audiences_write list_and_subscribers_read list_and_subscribers_write data_extensions_read data_extensions_write file_locations_read file_locations_write tracking_events_read calendar_read calendar_write campaign_read campaign_write accounts_read accounts_write users_read users_write webhooks_read webhooks_write workflows_write approvals_write tags_write approvals_read tags_read workflows_read ott_chat_messaging_read ott_chat_messaging_send ott_channels_read ott_channels_write marketing_cloud_connect_read marketing_cloud_connect_write marketing_cloud_connect_send event_notification_callback_create event_notification_callback_read event_notification_callback_update event_notification_callback_delete event_notification_subscription_create event_notification_subscription_read event_notification_subscription_update event_notification_subscription_delete tracking_events_write key_manage_view key_manage_rotate key_manage_revoke journeys_aspr journeys_delete",
    "soap_instance_url": "https://***************************.soap.marketingcloudapis.com/",
    "rest_instance_url": "https://***************************.rest.marketingcloudapis.com/"
}
Note:
1. account_id 很多时候就决定了你的权限范围
2. rest_instance_url就是后面我们URL中的路径前缀
2.Create Send Definition
//发送邮件时需要使用
文档链接: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/createSendDefinition.htm
POST https://${YOUR_BASE_REST_URL}.rest.marketingcloudapis.com/messaging/v1/email/definitions/
Headers:
Content-Type: application/json
Authorization: Bearer YOUR_ACCESS_TOKEN
{
  "definitionKey": "2FA_order_accounts",	//Required, Unique, 自定义
  "status": "Active",		//激活状态才能使用
  "name": "My Unique Definition Name",		//Required, Unique, 自定义
  "description": "Created via REST",
  "classification": "Default Transactional",
  "content": {
    "customerKey": "bebf8650-137a-494c-8727-cdeb32534961"	//Required, 邮件模板externalKey
  },
  "subscriptions": {
    "list": "test-list",	//Required, 订户列表externalKey,决定发送邮件中的收件人基本信息存在哪个列表
    "dataExtension": "test-de",	//Required, 数据拓展externalKey,决定发送邮件中的收件人拓展信息存在哪个数据拓展
    "autoAddSubscriber": true,	//自动添加到订户列表中
    "updateSubscriber": true	//自动更新到订户列表中
  },
  "options": {
    "trackLinks": true,
    "cc": ["cc_address@example.com"],
    "bcc": ["bcc_address@example.com"]
    "createJourney": true
  }
}
3. 发送邮件
文档链接: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/sendMessageMultipleRecipients.htm
POST  https://${YOUR_BASE_REST_URL}.rest.marketingcloudapis.com/messaging/v1/email/messages
Headers:
Content-Type: application/json
Authorization: Bearer YOUR_ACCESS_TOKEN

{
  "definitionKey": "2FA_order_accounts",	//Required, 上文中自定义的definitionKey
  "recipients": [
    {
      "contactKey": "recipient1",	//Required, 订户的唯一标识
      "to": "recipient1@example.com",
      "messageKey": "nFL4ULgheUeaGbPIMzJJSw",	//用于记录这封邮件的发送状态的唯一标识
      "attributes": {
        "RequestAttribute_1": "value_1",
        "RequestAttribute_2": "value_2",
        "Attribute1": "This is one for recipient1",
        "Attribute2": "This is two for recipient1"
      }
    },
    {
      "contactKey": "recipient2",
      "to": "recipient2@example.com",
      "messageKey": "GV1LhQ6NFkqFUAE1IsoQ9Q"
      "attributes": {
        "UserAttribute_3": "value_3",
        "UserAttribute_3": "value_4",
      }
    }
  ],
  "attributes": {
    "UserAttribute_a": "value_a",
    "UserAttribute_b": "value_b"
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值