Libreria di queries
Libreria di queriesInviare un'email agli iscritti per notificare la pubblicazione di un nuovo post

Inviare un'email agli iscritti per notificare la pubblicazione di un nuovo post

Questa query invia un'email a tutti gli utenti, notificando loro la creazione di un nuovo post sul sito.

Include la possibilità di selezionare gli utenti che si sono iscritti a una lista di distribuzione, tuttavia questa parte della query è commentata. (Decommenta se necessario.) Gli utenti iscritti sono quelli con il meta email_list con valore new_posts.

Questa query richiede che l'endpoint abbia le Mutation Annidate abilitate.

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
        }
      }
    }
  }
}