# 免费自动发送 Discourse 邀请邮件的方法（适用于 WordPress 及其他平台）

**URL:** <https://meta.discourse.org/t/free-ways-to-automate-sending-discoures-invite-emails-with-wordpress-and-other-platform/122097>\
**Category:** WordPress\
**Created:** [2019年七月4日 13:08 UTC](https://meta.discourse.org/t/free-ways-to-automate-sending-discoures-invite-emails-with-wordpress-and-other-platform/122097 "2019-07-04T13:08:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phuong\_SuSu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phuong_susu/32/148042_2.png) [@Phuong\_SuSu](https://meta.discourse.org/u/Phuong_SuSu)\
**Post date:** [2019年七月4日 13:08 UTC](https://meta.discourse.org/t/free-ways-to-automate-sending-discoures-invite-emails-with-wordpress-and-other-platform/122097/1 "2019-07-04T13:08:57Z")

</div>

继续讨论来自 [使用 Zapier 自动发送 Discourse 邀请邮件](https://meta.discourse.org/t/automate-sending-discourse-invite-emails-with-zapier/121608)：

在实现目标的过程中，我发现了这个主题。这很棒，但 Zapier 毕竟不是免费的，☹ 所以我创建了这两个代码，希望它们对某人有用。请将它们放入您的主题 `functions.php` 文件中。

如果您也有类似的有用代码，请告诉我 😃 我想更多地了解 Discourse API，以及仅通过编码免费将它们与 WordPress 连接的方法 ^^！

**在用户提交评论后（待审核状态）发送 Discourse 邀请**

```php
function invite_forum_from_comment($comment_ID) {
    $comment = get_comment($comment_ID);
    $comment_email = get_comment_author_email($comment);
    $maybe_notify = ('0' == $comment->comment_approved);
    if ($maybe_notify) {
        // 邀请用户
        wp_remote_post('https://forum.yourdomain.com/invites', array(
            'method' => 'POST',
            'headers' => array(
                'Content-Type' => 'multipart/form-data',
                'Api-key' => 'yourapikey',
                'Api-Username' => 'yourusername'
            ),
            'body' => array(
                'email' => $comment_email,
                'custom_message' => '感谢您的评论，在等待审核期间，为什么不加入我们的论坛呢？！'
            )
        ));
    }
}
add_action('comment_post', 'invite_forum_from_comment');

```

**在用户完成 WooCommerce 订单后邀请其加入 Discourse**

```php
function invite_forum_from_order($order_id) {
    // 获取所有订单数据
    $order = new WC_Order($order_id);
    // 从订单中获取用户邮箱
    $order_email = $order->billing_email;
    // 邀请用户
    wp_remote_post('https://forum.yourdomain.com/invites', array(
        'method' => 'POST',
        'headers' => array(
            'Content-Type' => 'multipart/form-data',
            'Api-key' => 'yourapikey',
            'Api-Username' => 'yourusername'
        ),
        'body' => array(
            'email' => $order_email,
            'custom_message' => '感谢您的订单，何必浪费时间等待，直接加入我们的论坛吧！'
        )
    ));
}
// 将此新创建的函数添加到感谢页面
add_action('woocommerce_thankyou', 'invite_forum_from_order', 10, 1);

```

---

<div class="post-metadata">

**Author:** ![Phuong\_SuSu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phuong_susu/32/148042_2.png) [@Phuong\_SuSu](https://meta.discourse.org/u/Phuong_SuSu)\
**Post date:** [2019年七月5日 16:28 UTC](https://meta.discourse.org/t/free-ways-to-automate-sending-discoures-invite-emails-with-wordpress-and-other-platform/122097/2 "2019-07-05T16:28:21Z")

</div>

更新：此代码将仅邀请已批准的评论，以防止邀请垃圾邮件发送者。

```
function invite_forum_from_approved_comment($comment_ID) {
 $comment = get_comment( $comment_ID );
 $comment_email = get_comment_author_email($comment);
 $maybe_notify = ( '1' == $comment->comment_approved );
 if ( $maybe_notify ) {
  // 邀请用户
  wp_remote_post( 'https://forum.fususu.com/invites', array(
      'method' => 'POST',
      'headers' => array('Content-Type' => 'multipart/form-data', 
                         'Api-key' => 'xxxxxxxxxxxxxxxxxxxx',
                         'Api-Username' => 'xxxx'),
      'body' => array('email' => $comment_email, 
                     'custom_message' => '感谢您的评论。您的评论已获批准，您已获得加入我论坛的邀请。')
        )
  );  
 };
}
add_action('comment_unapproved_to_approved', 'invite_forum_from_approved_comment');

```

---

<div class="post-metadata">

**Author:** ![Phuong\_SuSu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phuong_susu/32/148042_2.png) [@Phuong\_SuSu](https://meta.discourse.org/u/Phuong_SuSu)\
**Post date:** [2019年七月6日 00:17 UTC](https://meta.discourse.org/t/free-ways-to-automate-sending-discoures-invite-emails-with-wordpress-and-other-platform/122097/3 "2019-07-06T00:17:30Z")

</div>

在邀请我的联系人加入 Mautic 系统的论坛途中，  
我发现 Mautic 已经具备一个非常出色的 webhook 功能，可以向 Discourse 发送 POST 请求 :D，^^！只需在活动中创建一个操作，然后就完成了！无需编写任何代码 😃

 ![14%20AM](https://global.discourse-cdn.com/meta/original/3X/6/a/6a099a1e8bcf01551b314c0c4d2800cd8892820a.png)  
 ![49%20AM](https://global.discourse-cdn.com/meta/original/3X/8/2/82169b7dec958ca1a8dbbd0aa936b9151a6188d7.png)  
为 Zapier 感到惋惜，它们确实不错，但我更喜欢无限制且免费的解决方案 😊
