railsware/mailtrap-php

Mailtrap SDK 提供所有 API 函数的方法。

2.0.1 2024-08-15 14:34 UTC

This package is auto-updated.

Last update: 2024-09-15 14:47:51 UTC


README

GitHub Actions GitHub Actions

PHP version support Latest Version on Packagist Total Downloads

安装

您可以通过composer安装此软件包。

Mailtrap API 客户端与 Guzzle、React、Zend、Symfony HTTP 或其他任何发送 HTTP 消息的库不是硬耦合。相反,它使用PSR-18 客户端抽象。

这将为您提供选择想要使用的HTTP 客户端的灵活性。

如果您想快速开始,应运行以下命令之一(取决于您想使用哪个 HTTP 客户端)

# With symfony http client (recommend)
composer require railsware/mailtrap-php symfony/http-client nyholm/psr7

# Or with guzzle http client
composer require railsware/mailtrap-php guzzlehttp/guzzle php-http/guzzle7-adapter

用法

您应在应用程序中使用 Composer 自动加载器来自动加载依赖项。

以下是使用 SDK 发送消息的方法

<?php

use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapClient;
use Mailtrap\Mime\MailtrapEmail;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\UnstructuredHeader;

require __DIR__ . '/vendor/autoload.php';

// Mailtrap SENDING client (real) for transactional emails
$mailtrap = MailtrapClient::initSendingEmails(
    apiKey: getenv('MAILTRAP_API_KEY') # your API token from here https://mailtrap.io/api-tokens
);

$email = (new MailtrapEmail())
    ->from(new Address('example@your-domain-here.com', 'Mailtrap Test'))
    ->replyTo(new Address('reply@your-domain-here.com'))
    ->to(new Address('email@example.com', 'Jon'))
    ->priority(Email::PRIORITY_HIGH)
    ->cc('mailtrapqa@example.com')
    ->addCc('staging@example.com')
    ->bcc('mailtrapdev@example.com')
    ->subject('Best practices of building HTML emails')
    ->text('Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap’s Guide on How to Build HTML Email is live on our blog')
    ->html(
        '<html>
        <body>
        <p><br>Hey</br>
        Learn the best practices of building HTML emails and play with ready-to-go templates.</p>
        <p><a href="https://mailtrap.io/blog/build-html-email/">Mailtrap’s Guide on How to Build HTML Email</a> is live on our blog</p>
        <img src="cid:logo">
        </body>
    </html>'
    )
    ->embed(fopen('https://mailtrap.io/wp-content/uploads/2021/04/mailtrap-new-logo.svg', 'r'), 'logo', 'image/svg+xml')
    ->category('Integration Test')
    ->customVariables([
        'user_id' => '45982',
        'batch_id' => 'PSJ-12'
    ])
;

// Custom email headers (optional)
$email->getHeaders()
    ->addTextHeader('X-Message-Source', 'domain.com')
    ->add(new UnstructuredHeader('X-Mailer', 'Mailtrap PHP Client')) // the same as addTextHeader
;

try {
    $response = $mailtrap->send($email);

    var_dump(ResponseHelper::toArray($response)); // body (array)
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}


// OR -> Mailtrap BULK SENDING client (real)
try {
    $mailtrapBulkSending = MailtrapClient::initSendingEmails(
        apiKey: getenv('MAILTRAP_API_KEY'), # your API token from here https://mailtrap.io/api-tokens
        isBulk: true # Bulk sending (@see https://help.mailtrap.io/article/113-sending-streams)
    );

    $response = $mailtrapBulkSending->send($email);

    var_dump(ResponseHelper::toArray($response)); // body (array)
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// OR -> Mailtrap Testing client (sandbox)
try {
    $mailtrapTesting = MailtrapClient::initSendingEmails(
        apiKey: getenv('MAILTRAP_API_KEY'), # your API token from here https://mailtrap.io/api-tokens
        isSandbox: true, # Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
        inboxId: getenv('MAILTRAP_INBOX_ID') # required param for sandbox sending
    );

    $response = $mailtrapTesting->send($email);

    var_dump(ResponseHelper::toArray($response)); // body (array)
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

所有用法示例

您可以在这里找到更多示例。

框架集成

如果您正在使用框架,您可能会考虑这些 composer 软件包,以简化框架集成。

贡献

欢迎在GitHub上提交错误报告和拉取请求。该项目旨在成为一个安全、欢迎的协作空间,贡献者应遵守行为准则

许可证

该软件包根据MIT 许可证条款提供开源。

行为准则

与 Mailtrap 项目代码库、问题跟踪器、聊天室和邮件列表互动的所有人应遵守行为准则