quantimailq-connector

用于与 MailQ REST API 通信的库

v0.9.3 2021-05-25 08:06 UTC

README

安装

Composer

composer require quanti/mailq-connector

在 Nette 中使用

将此部分添加到您的配置中。您应该为开发和生产环境使用不同的 URL 和 API 密钥。

用法

存在一个 MailQ 对象,它是 MailQ REST API 的外观。最常见的用例是每个客户在 MailQ 中只有一个公司。您需要使用公司 ID 实例化 MailQ 对象。因为也存在有多个公司的客户,所以有 MailQFactory,它可以创建特定公司的 MailQ。

$apiKey = "6e2211bf472a9478f03420fb5897e324c57d05fc27bc0e871083275e98eec344";
$apiUrl = "http://mailq-test.quanti.cz/api/v2";
$mailqFactory = new MailQFactory($apiUrl);
$companyId = 1;
$mailq = $mailqFactory->createMailQ($companyId, $apiKey);

使用 Docker 运行沙盒

沙盒是用于与 PHP MailQ 库玩耍的最小应用程序。

Windows

  • 运行 docker run --rm -it -v %cd%:/app composer install 以安装 PHP 依赖项
  • 运行 docker run --rm -it -v %cd%:/app php:cli php /app/sandbox/sandbox.php <API key> <company ID>

Unix

  • 运行 docker run --rm -it -v $(pwd):/app composer install 以安装 PHP 依赖项
  • 运行 docker run --rm -it -v $(pwd):/app php:cli php /app/sandbox/sandbox.php <API key> <company ID>

其中 API 密钥是用于身份验证的私有公司密钥,公司 ID 是用于标识公司的整数。

活动资源

获取所有活动

$campaigns = $mailq->getCampaigns();

获取单个活动

$campaignId = 1;
$campaigns = $mailq->getCampaign();

公司资源

获取公司

$company = $mailq->getCompany();

重新生成 API 密钥

小心使用!重新生成 API 密钥后,应用程序将抛出错误,因为您已经创建了连接

$apiKey = $mailq->regenerateApiKey();

日志消息资源

获取所有日志消息

$logMessagesEntity = $mailq->getLogMessages();

获取单个日志消息

$logMessageId = 1;
$logMessageEntity = $mailq->getLogMessage($logMessageId);

通讯资源

创建通讯

$data = [
    "name" => "Awesome newsletter",
    "campaign"=> "Spring 2016",
    "subject" => "Buy our new product",
    "senderEmail" => "newsletter@example.org",
    "sendAs" => "Awesome Company",
    "from" => "2018-07-01T00:00:00+00:00",
    "to" => "2018-07-02T00:00:00+00:00",
    "text"=> "TmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciw=",
    "automaticTime"=> false,
    "recipientsListId"=>1,
    "templateUrl" => "http://example.org/newsletter.html",
    "unsubscribeTemplateUrl" => "http://example.org/unsubscribe.html"
];
$newsletter = new \MailQ\Entities\v2\NewsletterEntity($data);
$mailq->createNewsletter($newsletter);
$newsletterId = $newsletter->getId();

更新通讯

$data = [
    "id" => 1,
    "name" => "Awesome newsletter",
    "campaign" => "Spring 2016",
    "subject" => "Buy our new product",
    "senderEmail" => "newsletter@example.org",
    "sendAs" => "Awesome Company",
    "from" => "2018-07-01T00:00:00+00:00",
    "to" => "2018-07-02T00:00:00+00:00",
    "text" => "TmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciw=",
    "automaticTime" => false,
    "recipientsListId" => 1,
    "templateUrl" => "http://example.org/newsletter.html",
    "ampTemplateUrl" => "http://example.org/ampNewsletter.html",
    "unsubscribeTemplateUrl" => "http://example.org/unsubscribe.html"
];
$newsletter = new \MailQ\Entities\v2\NewsletterEntity($data);
$mailq->updateNewsletter($newsletter);
$newsletterId = $newsletter->getId();

更新就绪状态下的通讯

$data = [
    "id" => 1,
    "name" => "Awesome newsletter",
    "campaign" => "Spring 2016",
    "subject" => "Buy our new product",
    "senderEmail" => "newsletter@example.org",
    "replyToEmail" : "newsletter@example.org",
    "sendAs" => "Awesome Company",
    "text" => "TmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciwgbmV3c2xldHRlciw=",
    "automaticTime" => false,
    "templateUrl" => "http://example.org/newsletter.html",
    "ampTemplateUrl" => "http://example.org/ampNewsletter.html",
    "unsubscribeTemplateUrl" => "http://example.org/unsubscribe.html"
];
$newsletter = new \MailQ\Entities\v2\NewsletterEntity($data);
$mailq->updateNewsletter($newsletter);
$newsletterId = $newsletter->getId();

获取所有通讯

$newsletters = $mailq->getNewsletters();

获取通讯

$newsletterId = 1;
$newsletter = $mailq->getNewsletter($newsletterId);

发送测试电子邮件

$newsletterId = 1;
$email = "test@example.org";
$mailq->sendTestEmail($email,$newsletterId);

开始准备通讯

$newsletterId = 1;
$mailq->startNewsletter($newsletterId);

停止准备通讯

$newsletterId = 1;
$mailq->stopNewsletter($newsletterId);

通知资源

创建通知

$data = [
    "name" => "First Notification",
    "code" => "N1",
    "subject" => "{{orderNumber}} order is ready",
    "sendAs" => "Awesome Company",
    "text" => "QWx0ZXJuYXRpdmUgYmFzZTY0IGVtYWlsIHRleHQ=",
    "template" => "QWx0ZXJuYXRpdmUgYmFzZTY0IGVtYWlsIHRleHQ=",
    "ampTemplate" => "QWx0ZXJuYXRpdmUgYmFzZTY0IGVtYWlsIHRleHQ=",
    "appliedSenderEmail" => "notification@example.org"
];
$notification = new \MailQ\Entities\v2\NotificationEntity($data);
$mailq->createNotification($notification);
$notificationId = $notification->getId();

更新通知

$data = [
    "id" => 1,
    "name" => "First Notification",
    "code" => "N1",
    "subject" => "{{orderNumber}} order is ready",
    "sendAs" => "Awesome Company",
    "text" => "QWx0ZXJuYXRpdmUgYmFzZTY0IGVtYWlsIHRleHQ=",
    "template" => "QWx0ZXJuYXRpdmUgYmFzZTY0IGVtYWlsIHRleHQ=",
    "ampTemplate" => "QWx0ZXJuYXRpdmUgYmFzZTY0IGVtYWlsIHRleHQ=",
    "appliedSenderEmail" => "notification@example.org"
];
$notification = new \MailQ\Entities\v2\NotificationEntity($data);
$mailq->updateNotification($notification);

获取所有通知

$notifications = $mailq->getNotifications();

删除通知

$notificationId = 1;
$mailq->deleteNotification($notificationId);

发送通知电子邮件

在数据部分包含所有将在通知中使用的值。关联数组的键是变量名,值是值。

$data = [
    "recipientEmail" => "recipient@example.org",
    "data" => [
        "key1" => "value1",
        "key2" => "value2"
    ],
    "attachments" => [
        [
            "displayName" =>  "Priloha 1",
            "link" => "http://example.org/test.txt",
            "source" => "dGVzdHM=",
            "mimeType" => "text/plain"
        ],
        [
            "displayName" => "Priloha 2",
            "link" => "http://example.org/image.png",
            "source" => "R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==",
            "mimeType" => "image/png"
        ]
    ]
];
$notificationId = 1;
$notificationData = new \MailQ\Entities\v2\NotificationDataEntity($data);
$mailq->sendNotificationEmail($notificationData,$notificationId);
$notificationDataId = $notificationData->getId();

获取通知电子邮件

$notificationId = 1;
$notificationDataId = 2;
$notificationData = $mailq->getNotificationData($notificationId,$notificationDataId);

获取所有通知电子邮件

$notificationId = 1;
$email = "recipiet@example.org";
$notificationsData = $mailq->getNotificationsData($notificationId,$email);

收件人列表资源

创建收件人列表

$mailq = $mailqFactory->createMailQ($companyId);
$data = [
    "name" => "All clients",
    "description" => "All clients of our awesome company",
    "variables" => ["salutation","gender"],
    "formVisible" => true
];
$recipientsList = new \MailQ\Entities\v2\RecipientsListEntity($data);
$mailq->createRecipientsList($recipientsList);
$recipientsListId = $recipientsList->getId();

删除收件人列表

$recipientsListId = 1;
$mailq->deleteRecipientsList($recipientListId);

获取收件人列表

$recipientsListId = 1;
$recipientsList = $mailq->getRecipientsList($recipientListId);

获取收件人列表

您可以为电子邮件指定电子邮件,API 返回仅包含此电子邮件的收件人列表

$email = "recipient@example.org";
$recipientsLists = $mailq->getRecipientsLists($email);

获取所有收件人

$recipientsListId = 1;
$recipients = $mailq->getRecipients($recipientsListId);

一次添加多个收件人

$data = [
	"recipients" => [
		[
			"email" => "recipient@example.org",
	        "data" => [
	            "key1" => "value1",
	            "key2" => "value2"
	        ]
		]
	]
];
$recipients = new \MailQ\Entities\v2\RecipientsEntity($data);
$recipientsListId = 1;
$validate = false;
$mailq->addRecipients($recipients,$recipientsListId,$validate);

更新或创建收件人

$data = [
	"email" => "recipient@example.org",
    "data" => [
        "key1" => "value1",
        "key2" => "value2"
    ]
];
$recipient = new \MailQ\Entities\v2\RecipientEntity($data);
$recipientsListId = 1;
$validate = false;
$mailq->updateRecipient($recipient,$recipientsListId,$validate);

获取所有收件人列表未订阅者

$recipientsListId = 1;
$unsubscribers = $mailq->getRecipientListUnsubscribers($recipientsListId);

添加收件人列表未订阅者

$recipientsListId = 1;
$email = "recipient@example.org";
$mailq->addRecipientListUnsubscriber($emails,$recipientsListId);

添加收件人列表未订阅者

$data = [
	"emails" => [
		[
			"email" => "recipient@example.org"
		],
		[
			"email" => "recipient2@example.org"
		]
	]
];
$recipientsListId = 1;
$emails = new \MailQ\Entities\v2\EmailAddressesEntity($data);
$mailq->addRecipientListUnsubscribers($emails,$recipientsListId);

删除收件人列表未订阅者

$recipientsListId = 1;
$email = "recipient@example.org";
$mailq->deleteRecipientListUnsubscriber($emails,$recipientsListId);

短信通知

创建单个短信通知

$data = [
	"name" => "SMS notification",
    "code" => "S1",
    "template" => "U01TIHRlc3Qge3t2YXJpYWJsZX19"
];
$smsNotification = new \MailQ\Entities\v2\SmsNotificationEntity($data);
$mailq->createSmsNotification($smsNotification);
$smsNotificationId = $smsNotification->getId();

更新单个短信通知

$data = [
	"name" => "SMS notification",
    "code" => "S1",
    "template" => "U01TIHRlc3Qge3t2YXJpYWJsZX19"
];
$smsNotification = new \MailQ\Entities\v2\SmsNotificationEntity($data);
$mailq->updateSmsNotification($smsNotification);

删除短信通知

$smsNotificationId = 1;
$mailq->deleteSmsNotification($smsNotification);

获取所有短信通知

$notifications = $mailq->getSmsNotifications();

获取单个短信通知

$smsNotificationId = 1;
$notifications = $mailq->getSmsNotification($smsNotificationId);

发送短信

$smsNotificationId = 1;
$data = [
	"toNumber" => "+420123456789",
    "data" => [
        "key1" => "value1",
        "key2" => "value2"
    ]
];
$sms = new \MailQ\Entities\v2\SmsEntity($data);
$mailq->sendSms($sms,$smsNotificationId);
$smsId = $sms->getId();

批量发送短信

$smsNotificationId = 1;
$data = [
	"batch" => [
        [    
            "id" => 1,
            "toNumber" => "+420123456789",
            "data" => [         
                "text" => "value1"   
            ]
        ],
        [     
            "id" => 2,
            "toNumber" => "+420123456789",
            "data" => [        
                "text" => "value2"   
            ]
        ]

    ]
];
$smsBatch = new \MailQ\Entities\v2\SmsBatchEntity($data);
$smsBatchResultEntity = $mailq->sendSmsBatch($smsBatch,$smsNotificationId);

发送者电子邮件

获取所有发送者电子邮件

$senderEmails = $mailq->getSenderEmails();

获取单个发送者电子邮件

$senderEmailId = 1;
$senderEmail = $mailq->getSenderEmail($senderEmailId);

用户

获取所有用户

$users = $mailq->getUsers();

获取单个用户

$userId = 1;
$user = $mailq->getUser($userId);