Thư viện Queries
Thư viện QueriesGửi email cho người đăng ký để thông báo về bài viết mới

Gửi email cho người đăng ký để thông báo về bài viết mới

Query này gửi email đến tất cả người dùng, thông báo về việc tạo một bài viết mới trên trang web.

Query bao gồm khả năng chọn những người dùng đã đăng ký danh sách email, tuy nhiên phần này của query đã được chú thích. (Vui lòng bỏ chú thích nếu cần.) Những người dùng đã đăng ký là những người có meta email_list với giá trị new_posts.

Query này yêu cầu endpoint phải bật Nested Mutations.

query GetPostAndExportData($postId: ID!) {
  post(by: { id: $postId }) {
    content @export(as: "postContent")
    title @export(as: "postTitle")
    url @export(as: "postURL")
  }
 
  hasPost: _notNull(value: $__post)
    @export(as: "doSendEmail")
}
 
query GetEmailData
  @depends(on: "GetPostAndExportData")
  @include(if: $doSendEmail)
{ 
  siteName: optionValue(name: "blogname")
    @export(as: "siteName")
 
  emailSubject: _sprintf(
    string: "There is a new post: \"%s\"",
    values: [$postTitle]
  )
    @export(as: "emailSubject")
}
 
mutation SendEmailToUsersAboutNewPost
  @depends(on: "GetEmailData")
  @include(if: $doSendEmail)
{
  users
  # # Retrieve only users subscribed to an email list (uncomment if needed)
  # (
  #   filter: {
  #     metaQuery: {
  #       key: "email_list",
  #       compareBy: {
  #         arrayValue: {
  #           value: "new_posts",
  #           operator: IN
  #         }
  #       }
  #     }
  #   }
  # )
  {
    displayName
    email
 
    emailMessageTemplate: _strConvertMarkdownToHTML(
      text: """
 
Hi {$userDisplayName},
 
There is a new post on the **{$siteName}** website:
 
[**{$postTitle}**]({$postURL})
 
{$postContent}
  
      """
    )
      @remove
    emailMessage: _strReplaceMultiple(
      search: ["{$userDisplayName}", "{$siteName}", "{$postTitle}", "{$postContent}", "{$postURL}"],
      replaceWith: [$__displayName, $siteName, $postTitle, $postContent, $postURL],
      in: $__emailMessageTemplate
    )
      @remove
 
    _sendEmail(
      input: {
        to: $__email
        subject: $emailSubject
        messageAs: {
          html: $__emailMessage
        }
      }
    ) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
    }
  }
}