maileroo/maileroo-php-sdk

使用这个库,您可以轻松地将Maileroo强大的电子邮件API集成到您的PHP项目中。

dev-main 2024-08-28 08:55 UTC

This package is not auto-updated.

Last update: 2024-09-26 07:35:23 UTC


README

欢迎使用Maileroo的官方PHP SDK,一个强大且灵活的电子邮件发送API。此SDK允许您轻松地将Maileroo的电子邮件发送功能集成到您的PHP应用程序中。

为什么使用它?

此库可用于...

  • 通过单个API调用发送基本电子邮件
  • 发送具有动态内容和个性化的模板电子邮件
  • 向多个收件人发送电子邮件,附加文件等
  • 管理您的联系人,包括添加、更新、列出和删除联系人

要求

  • PHP 7.4或更高版本
  • PHP的cURL扩展

安装

通过运行以下命令使用Composer安装SDK

composer require maileroo/maileroo-php-sdk

用法

1. 发送基本电子邮件

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient("YOUR_API_KEY");

    $basic_email_response = $client->setFrom('Maileroo', 'no.reply@mail.maileroo.com')
        ->setTo('John Doe', 'john.doe@maileroo.com')
        ->setCc('Jane Doe', 'jane.doe@maileroo.com') // Optional
        ->setBcc('Jim Doe', 'jim.doe@maileroo.com') // Optional
        ->setReplyTo('Administrator', 'admin@maileroo.com') // Optional
        ->setReferenceId($client->generateReferenceId()) // Optional
        ->setTags(['tag1' => 'value1', 'tag2' => 'value2']) // Optional
        ->setTracking(true) // Optional
        ->setSubject('Hello World')
        ->setHtml('<p>Hello World</p>')
        ->setPlain('Hello World') // Optional
        ->addAttachment('path/to/file', 'file_name', 'file_type') // Optional
        ->addInlineAttachment('path/to/file', 'file_name', 'file_type') // Optional
        ->sendBasicEmail();

    echo "Basic Email Sent Successfully.\n";

} catch (Exception $e) {
    echo "An error occurred while sending the basic email: " . $e->getMessage() . "\n";
}

2. 发送模板电子邮件

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient(""); // Use Domain Sender Key

    $client->setFrom('Maileroo', 'no.reply@mail.maileroo.com')
        ->setTo('John Doe', 'john.doe@maileroo.com')
        ->setCc('Jane Doe', 'jane.doe@maileroo.com') // Optional
        ->setBcc('Jim Doe', 'jim.doe@maileroo.com') // Optional
        ->setReplyTo('Administrator', 'admin@maileroo.com') // Optional
        ->setReferenceId($client->generateReferenceId()) // Optional
        ->setTags(['tag1' => 'value1', 'tag2' => 'value2']) // Optional
        ->setTracking(true) // Optional
        ->setSubject('Hello World')
        ->setTemplateId(1)
        ->setTemplateData(['name' => 'John Doe'])
        ->addAttachment('path/to/file', 'file_name', 'file_type') // Optional
        ->addInlineAttachment('path/to/file', 'file_name', 'file_type') // Optional
        ->sendTemplateEmail();

    echo "Template Email Sent Successfully.\n";

} catch (Exception $e) {
    echo "An error occurred while sending the template email: " . $e->getMessage() . "\n";
}

3. 创建联系人

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient(""); // Use Organization API Key

    $list_id = '123';

    $contact = [
        'subscriber_name' => 'John Doe',
        'subscriber_email' => 'john.doe@example.com',
        'subscriber_timezone' => 'America/New_York',
        'subscriber_tags' => 'tag1,tag2',
        'subscriber_status' => 'UNCONFIRMED'
    ];

    $client->createContact($list_id, $contact);

    echo "Contact Created Successfully.\n";

} catch (Exception $e) {
    echo "An error occurred while creating the contact: " . $e->getMessage() . "\n";
}

4. 获取联系人

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient(""); // Use Organization API Key

    $list_id = '123';

    $retrieved_contact = $client->getContact($list_id, 'new.contact@example.com');

    print_r($retrieved_contact);

} catch (Exception $e) {
    echo "An error occurred while retrieving the contact: " . $e->getMessage() . "\n";
}

5. 更新联系人

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient(""); // Use Organization API Key

    $list_id = '123';

    $updated_contact = [
        'subscriber_name' => 'John Doe',
        'subscriber_timezone' => 'Australia/Sydney',
    ];

    $client->updateContact($list_id, 'john.doe@example.com', $updated_contact);

    echo "Contact Updated Successfully.\n";

} catch (Exception $e) {
    echo "An error occurred while updating the contact: " . $e->getMessage() . "\n";
}

6. 删除联系人

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient(""); // Use Organization API Key

    $list_id = '123';

    $client->deleteContact($list_id, 'john.doe@example.com');

    echo "Contact Deleted Successfully.\n";

} catch (Exception $e) {
    echo "An error occurred while deleting the contact: " . $e->getMessage() . "\n";
}

7. 列出联系人

<?php

use Maileroo\MailerooClient;

try {

    $client = new MailerooClient(""); // Use Organization API Key

    $list_id = '123';
    $query = '';
    $page = 1;

    $contacts = $client->listContacts($list_id, $query, $page);

    print_r($contacts);

} catch (Exception $e) {
    echo "An error occurred while listing the contacts: " . $e->getMessage() . "\n";
}

进一步阅读

有关如何使用Maileroo API的更多信息,请参阅Maileroo API文档

许可

本软件根据MIT许可证发布。有关更多信息,请参阅LICENSE文件。