Şema eğitimi
Şema eğitimiDers 14: E-posta Gönderme Keyfi

Ders 14: E-posta Gönderme Keyfi

Bu tutorial dersi, Gato GraphQL'in e-posta göndermek için çeşitli yeteneklerini göstermektedir.

E-posta Gönderme

E-postaları, Email Sender eklentisi tarafından sağlanan _sendEmail mutation'ı aracılığıyla göndeririz.

  • E-posta, messageAs girdisinden hangi özelliğin kullanıldığına bağlı olarak "text" veya "HTML" içerik türüyle gönderilir
  • from girdisi isteğe bağlıdır; sağlanmazsa WordPress'te depolanan ayarlar kullanılır
  • _sendEmail, WordPress wp_mail fonksiyonunu çalıştırır, dolayısıyla WordPress'te e-posta göndermek için tanımlanmış yapılandırmayı (örneğin kullanılacak SMTP sağlayıcısı) kullanır
mutation {
  sendTextEmail: _sendEmail(
    input: {
      from: {
        email: "from@email.com"
        name: "Me myself"
      }
      replyTo: "replyTo@email.com"
 
      to: "target@email.com"
      cc: ["cc1@email.com", "cc2@email.com"]
      bcc: ["bcc1@email.com", "bcc2@email.com", "bcc3@email.com"]
      
      subject: "Email with text content"
      messageAs: {
        text: "Hello world!"
      }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
  
  sendHTMLEmail: _sendEmail(
    input: {
      to: "target@email.com"
      subject: "Email with HTML content"
      messageAs: {
        html: "<p>Hello world!</p>"
      }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}

Markdown Kullanarak E-posta Oluşturma

Helper Function Collection eklentisindeki _strConvertMarkdownToHTML alanı, Markdown'ı HTML'ye dönüştürür.

Bu alanı kullanarak e-postayı Markdown ile oluşturabiliriz:

query GetEmailData {
  emailMessage: _strConvertMarkdownToHTML(
    text: """
 
We have great news: **Version 1.0 of our plugin will be released soon!**
 
If you'd like to help us beta test it, please complete [this form](https://forms.gle/FpXNromWAsZYC1zB8).
 
_Please reply by 30th June 🙏_
 
Thanks!
 
    """
  )
    @export(as: "emailMessage")
}
 
mutation SendEmail @depends(on: "GetEmailData") {
  _sendEmail(
    input: {
      to: "target@email.com"
      subject: "Great news!"
      messageAs: {
        html: $emailMessage
      }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}

E-postaya Dinamik Veri Ekleme

PHP Functions via Schema tarafından sağlanan fonksiyon alanlarını kullanarak, yer tutucu içeren bir mesaj şablonu oluşturabilir ve bunları dinamik verilerle değiştirebiliriz:

query GetPostData($postID: ID!) {
  post(by: {id: $postID}) {
    title @export(as: "postTitle")
    excerpt @export(as: "postExcerpt")
    url @export(as: "postLink")
    author {
      name @export(as: "postAuthorName")
      url @export(as: "postAuthorLink")
    }
  }
}
 
query GetEmailData @depends(on: "GetPostData") {
  emailMessageTemplate: _strConvertMarkdownToHTML(
    text: """
 
There is a new post by [{$postAuthorName}]({$postAuthorLink}):
 
**{$postTitle}**: {$postExcerpt}
 
[Read online]({$postLink})
 
    """
  )
  emailMessage: _strReplaceMultiple(
    search: ["{$postAuthorName}", "{$postAuthorLink}", "{$postTitle}", "{$postExcerpt}", "{$postLink}"],
    replaceWith: [$postAuthorName, $postAuthorLink, $postTitle, $postExcerpt, $postLink],
    in: $__emailMessageTemplate
  )
    @export(as: "emailMessage")
  subject: _sprintf(string: "New post created by %s", values: [$postAuthorName])
    @export(as: "emailSubject")
}
 
mutation SendEmail @depends(on: "GetEmailData") {
  _sendEmail(
    input: {
      to: "target@email.com"
      subject: $emailSubject
      messageAs: {
        html: $emailMessage
      }
    }
  ) {
    status
  }
}

Yöneticiye Bildirim E-postası Gönderme

WordPress wp_options tablosundan yönetici kullanıcısının e-postasını alabilir ve bu değeri to alanına ekleyebiliriz:

query ExportData {
  adminEmail: optionValue(name: "admin_email")
    @export(as: "adminEmail")
}
 
mutation SendEmail @depends(on: "ExportData") {
  _sendEmail(
    input: {
      to: $adminEmail
      subject: "Admin notification"
      messageAs: {
        html: "There is a new post on the site, go check!"
      }
    }
  ) {
    status
  }
}

Alternatif olarak, Schema Configuration'da İç İçe Mutation'lar etkinleştirilmişse, yönetici e-postasını doğrudan mutation işleminde alabilir (ve Field to Input aracılığıyla mutation'a ekleyebiliriz):

mutation SendEmail {
  adminEmail: optionValue(name: "admin_email")
  _sendEmail(
    input: {
      to: $__adminEmail
      subject: "Admin notification"
      messageAs: {
        html: "There is a new post on the site, go check!"
      }
    }
  ) {
    status
  }
}

Kullanıcılara Kişiselleştirilmiş E-posta Gönderme

Bu GraphQL queries'in çalışması için, endpoint'e uygulanan Şema Yapılandırması'nın İç İçe Mutation'lar özelliğinin etkin olması gerekir

_sendEmail bir global alan (ya da daha kesin söylemek gerekirse, bir global mutation) olduğundan, User dahil GraphQL şemasındaki herhangi bir tür üzerinde çalıştırılabilir.

Bu queries, bir kullanıcı listesi alır, verilerini (ad, e-posta ve meta olarak depolanan kalan kredi sayısı) elde eder ve her birine kişiselleştirilmiş bir e-posta gönderir:

mutation {
  users {
    email
    displayName
    credits: metaValue(key: "credits")
    
    # If the user does not have meta entry "credits", use `0` credits
    hasNoCreditsEntry: _isNull(value: $__credits)
    remainingCredits: _if(condition: $__hasNoCreditsEntry, then: 0, else: $__credits)
 
    emailMessageTemplate: _strConvertMarkdownToHTML(
      text: """
 
Hello %s,
 
Your have **%s remaining credits** in your account.
 
Would you like to [buy more](%s)?
 
      """
    )
    emailMessage: _sprintf(
      string: $__emailMessageTemplate,
      values: [
        $__displayName,
        $__remainingCredits,
        "https://mysite.com/buy-credits"
      ]
    )
 
    _sendEmail(
      input: {
        to: $__email
        subject: "Remaining credits alert"
        messageAs: {
          html: $__emailMessage
        }
      }
    ) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
    }
  }
}