messenger_os/messenger_os-php

此库允许通过PHP轻松发送通过MessengerOS的电子邮件。

V1.5 2023-11-20 08:17 UTC

This package is auto-updated.

Last update: 2024-09-20 10:14:02 UTC


README

MessengerOS Logo

此库允许您通过PHP快速轻松地使用MessengerOS API发送电子邮件、短信和网络推送通知。

目录

安装

先决条件

  • 兼容PHP版本7.4及以上。
  • 需要一个活动MessengerOS账户 - 我们每月提供3000封免费电子邮件,无需在您的电子邮件中添加我们的标志。需要更多?请查看我们的定价方案

设置环境变量

  1. 将示例env文件复制到新文件名为.env的文件中
cp .env.sample .env
  1. 编辑.env文件,包括存放在您的MessengerOS账户中的以下API密钥。
MESSENGER_OS_USER_KEY - from your account information
MESSENGER_OS_PROJECT_KEY - from the current project dashboard 
MESSENGER_OS_EMAIL_DELIVERY_PROVIDER_KEY - from the Delivery Channel information  
MESSENGER_OS_SMS_DELIVERY_PROVIDER_KEY - from the Delivery Channel information 
MESSENGER_OS_SEND_URL="https://inbound.messengeros.com/1.0/send"

安装包

composer require messenger_os/messenger_os-php

快速入门

根据您的安装方法,在以下示例中包含正确的行,放在每个示例的顶部

<?php
// Uncomment the next line if you're using a dependency loader (such as Composer) (recommended)
// require 'vendor/autoload.php';

// Uncomment the next line if you're not using a dependency loader (such as Composer),
// replacing <PATH TO> with the path to the messengeros-loader.php file
// require_once __DIR__ . '/../../messengeros-loader.php';

发送电子邮件的基本示例

以下是最小需要的代码来发送电子邮件。您可以在我们的示例类中找到更多示例这里

use MessengerOS\MessengerOS\Model\Email;
use MessengerOS\MessengerOS\Service\ApiService;

$apiService = new ApiService(
    getenv('MESSENGER_OS_USER_KEY'),
    getenv('MESSENGER_OS_PROJECT_KEY'),
    getenv('MESSENGER_OS_SEND_URL')
);

/* Build email recipients list */
$emailRecipients[] = (new Email\EmailRecipient())
    ->setEmail("recipient1@example.com");

$email = new Email\Email();
$email
    ->setFromName("John | My Company")
    ->setFromEmail("john@mycompany.com")
    ->setRecipients($emailRecipients)
    ->setSubject("Hi [first_name], this is my subject line from API")
    ->setPreviewLine("My preview line from API")
    ->setDeliveryProvider(getenv('MESSENGER_OS_EMAIL_DELIVERY_PROVIDER_KEY'))
    ->setReplyTo("no-reply@mycompany.com")
    ->setParams([
        ['first_name' => 'Thomas']
    ]);

// Send only Email notifications //
try {
    $response = $apiService->sendEmails($emails);
    print $response . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

关于

messengeros-php由MessengerOS维护和资助。