mailslurp/ mailslurp-client-php
MailSlurp 是一个API,可以用于从动态分配的电子邮件地址发送和接收电子邮件。它专为开发人员和QA团队设计,用于测试应用程序、处理传入的电子邮件、发送模板化通知、附件等。## 资源 - [主页](https://www.mailslurp.com) - 获取一个
Requires
- php: ^7.4 || ^8.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- phpunit/phpunit: ^8.0 || ^9.0
- dev-master
- 15.19.22
- 15.19.21
- 15.19.20
- 15.19.19
- 15.19.18
- 15.19.17
- 15.19.16
- 15.19.14
- 15.19.13
- 15.19.12
- 15.19.11
- 15.19.10
- 15.19.9
- 15.19.8
- 15.19.7
- 15.19.6
- 15.19.5
- 15.19.4
- 15.19.3
- 15.19.2
- 15.19.1
- 15.19.0
- 15.18.5
- 15.18.4
- 15.18.3
- 15.18.2
- 15.18.1
- 15.18.0
- 15.17.41
- 15.17.40
- 15.17.39
- 15.17.38
- 15.17.37
- 15.17.36
- 15.17.34
- 15.17.33
- 15.17.32
- 15.17.31
- 15.17.30
- 15.17.29
- 15.17.28
- 15.17.27
- 15.17.26
- 15.17.25
- 15.17.24
- 15.17.23
- 15.17.22
- 15.17.21
- 15.17.17
- 15.17.16
- 15.17.15
- 15.17.14
- 15.17.13
- 15.17.12
- 15.17.11
- 15.17.10
- 15.17.9
- 15.17.8
- 15.17.7
- 15.17.6
- 15.17.5
- 15.17.4
- 15.17.3
- 15.17.2
- 15.17.1
- 15.17.0
- 15.16.22
- 15.16.21
- 15.16.20
- 15.16.19
- 15.16.18
- 15.16.16
- 15.16.15
- 15.16.14
- 15.16.13
- 15.16.12
- 15.16.11
- 15.16.10
- 15.16.9
- 15.16.8
- 15.16.7
- 15.16.6
- 15.16.5
- 15.16.4
- 15.16.3
- 15.16.2
- 15.16.1
- 15.16.0
- 15.15.5
- 15.14.0
- 15.13.32
- 15.13.31
- 15.13.30
- 15.13.29
- 15.13.28
- 15.13.27
- 15.13.26
- 15.13.25
- 15.13.24
- 15.13.23
- 15.13.22
- 15.13.21
- 15.13.20
- 15.13.19
- 15.13.18
- 15.13.17
- 15.13.16
- 15.13.15
- 15.13.14
- 15.13.13
- 15.13.11
- 15.13.10
- 15.13.9
- 15.13.8
- 15.13.7
- 15.13.6
- 15.13.5
- 15.13.4
- 15.13.3
- 15.13.2
- 15.13.1
- 15.13.0
- 15.12.17
- 15.12.16
- 15.12.15
- 15.12.14
- 15.12.13
- 15.12.12
- 15.12.11
- 15.12.3
- 15.12.2
- 15.12.1
- 15.12.0
- 15.11.1
- 15.11.0
- 15.9.0
- 15.8.1
- 15.8.0
- 15.7.17
- 15.7.16
- 15.7.15
- 15.7.14
- 15.7.13
- 15.7.12
- 15.7.10
- 15.7.8
- 15.7.7
- 15.7.5
- 15.7.4
- 15.7.1
- 15.7.0
- 15.6.0
- 15.5.6
- 15.5.5
- 15.5.4
- 15.5.3
- 15.5.2
- 15.5.1
- 15.5.0
- 15.4.2
- 15.4.1
- 15.4.0
- 15.3.1
- 15.3.0
- 15.2.8
- 15.2.7
- 15.2.6
- 15.2.5
- 15.2.4
- 15.2.2
- 15.2.1
- 15.2.0
- 15.1.4
- 15.1.3
- 15.1.2
- 15.1.1
- 15.1.0
- 15.0.8
- 15.0.7
- 15.0.6
- 15.0.5
- 15.0.4
- 15.0.3
- 15.0.2
- 15.0.1
- 15.0.0
- 14.0.6
- 14.0.5
- 14.0.3
- 14.0.2
- 14.0.1
- 14.0.0
- 13.1.0
- 13.0.1
- 13.0.0
- 12.8.4
- 12.8.3
- 12.8.2
- 12.8.1
- 12.8.0
- 12.7.1
- 12.7.0
- 12.6.4
- 12.6.3
- 12.6.1
- 12.6.0
- 12.5.0
- 12.4.8
- 12.4.7
- 12.4.6
- 12.4.5
- 12.4.4
- 12.4.2
- 12.4.1
- 12.4.0
- 12.3.333
- 12.3.2
- 12.3.1
- 12.3.0
- 12.2.2
- 12.2.1
- 12.2.0
- 12.1.3333
- 12.1.32
- 12.1.31
- 12.1.30
- 12.1.29
- 12.1.28
- 12.1.27
- 12.1.26
- 12.1.25
- 12.1.24
- 12.1.23
- 12.1.22
- 12.1.1
- 12.1.0
- 12.0.0
- 11.16666.0
- 11.15.0
- 11.14.0
- 11.13.0
- 11.12.0
- 11.11.1
- 11.11.0
- 11.10.3
- 11.10.2
- 11.10.1
- 11.10.0
- 11.9.7
- 11.9.6
- 11.9.4
- 11.9.3
- 11.9.2
- 11.9.1
- 11.9.0
- 11.8.13
- 11.8.12
- 11.8.11
- 11.8.10
- 11.8.9
- 11.8.8
- 11.8.6
- 11.8.5
- 11.8.4
- 11.8.3
- 11.8.2
- 11.8.1
- 11.8.0
- 11.7.509
- 11.7.508
- 11.7.506
- 11.7.505
- 11.7.502
- 11.7.501
- 11.7.500
- 11.7.400
- 11.7.314
- 11.7.313
- 11.7.312
- 11.7.311
- 11.7.310
- 11.7.309
- 11.7.308
- 11.7.307
- 11.7.306
- 11.7.305
- 11.7.304
- 11.7.302
- 11.7.301
- 11.7.300
- 11.7.215
- 11.7.214
- 11.7.213
- 11.7.212
- 11.7.211
- 11.7.210
- 11.7.203
- 11.7.202
- 11.7.201
- 11.7.200
- 11.7.122
- 11.7.11
- 11.7.10
- 11.7.9
- 11.7.8
- 11.7.7
- 11.7.6
- 11.7.5
- 11.7.4
- 11.7.2
- 11.7.1
- 11.7.0
- 11.6.24
- 11.6.23
- 11.6.22
- 11.6.21
- 11.6.20
- 11.6.19
- 11.6.18
- 11.6.16
- 11.6.15
- 11.6.14
- 11.6.13
- 11.6.12
- 11.6.11
- 11.6.10
- 11.6.9
- 11.6.8
- 11.6.7
- 11.6.6
- 11.6.5
- 11.6.4
- 11.6.3
- 11.6.2
- 11.6.1
- 11.5.2333
- 11.5.22
- 11.5.21
- 11.5.20
- 11.5.12
- 11.5.11
- 11.5.10
- 11.5.8
- 11.5.7
- 11.5.6
- 11.5.5
- 11.5.4
- 11.5.3
- 11.5.2
- 11.5.1
- 11.5.0
- 11.4.25
- 11.4.24
- 11.4.23
- 11.4.22
- 11.4.0
- 11.3.0
- 11.2.0
- 11.1.1
- 11.1.0
- 11.0.1
- 11.0.0
- 9.0.0
- 8.7.1
- 8.7.0
- 8.6.0
- 8.5.5
- 8.5.4
- 8.5.3
- 8.5.2
- 8.5.1
- 8.5.0
- 8.4.3
- 8.4.2
- 8.4.1
- 8.4.0
- 8.3.1
- 8.3.0
- 8.2.17
- 8.2.16
- 8.2.15
- 8.2.14
- 8.2.12
- 8.2.11
- 8.2.10
- 8.2.9
- 8.2.8
- 8.2.7
- 8.2.6
- 8.2.5
- 8.2.4
- 8.2.2
- 8.2.1
- 8.2.0
- 8.1.1
- 8.1.0
- 8.0.23
- 8.0.22
- 8.0.21
- 8.0.20
- 8.0.19
- 8.0.18
- 8.0.17
- 8.0.16
- 8.0.15
- 8.0.14
- 8.0.13
- 8.0.12
- 8.0.11
- 8.0.10
- 8.0.9
- 8.0.8
- 8.0.6
- 8.0.5
- 8.0.4
- 8.0.3
- 8.0.2
- 8.0.1
- 7.2.6
- 7.0.10
- 7.0.9
- 7.0.8
- 7.0.7
- 7.0.6
- 7.0.5
- 7.0.3
- 7.0.2
- 5.2.0
- 5.1.0
- 4.9.0
- 4.8.2
- 4.8.1
- 4.8.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.0
This package is auto-updated.
Last update: 2024-09-03 06:37:49 UTC
README
使用PHP创建和管理电子邮件地址和电话号码。在代码和测试中发送和接收电子邮件、附件和短信。以下是一个快速示例
// configure mailslurp/mailslurp-client-php library $config = MailSlurp\Configuration::getDefaultConfiguration() ->setApiKey('x-api-key', getenv("API_KEY")); $inboxController = new MailSlurp\Apis\InboxControllerApi(null, $config); // create an inbox $inbox = $inboxController->createInboxWithDefaults(); // send an email $sendOptions = new MailSlurp\Models\SendEmailOptions(); $sendOptions->setTo([$inbox->getEmailAddress()]); $sendOptions->setSubject("Test"); $sendOptions->setBody("Hello"); $inboxController->sendEmail($inbox->getId(), $sendOptions); // receive the email $waitForController = new MailSlurp\Apis\WaitForControllerApi(null, $config); $email = $waitForController->waitForLatestEmail($inbox->getId(), 120000, true); $this->assertNotNull($email->getBody());
视频教程
快速链接
示例
开始使用
MailSlurp是一个电子邮件API,它允许您按需创建电子邮件地址,然后在代码和测试中发送和接收电子邮件。**不需要邮件服务器**。
本节描述了如何使用PHP客户端启动。要使用其他语言或REST API,请参阅开发者页面。
查看示例页面以获取代码示例和与常见框架一起使用。
查看方法文档,了解所有函数的列表。
获取API密钥
您需要一个免费的MailSlurp账户才能使用此服务。首先注册免费账户。
注册后,请登录仪表板。找到您的API密钥并复制代码。您将需要它来配置PHP中的MailSlurp客户端。
从MailSlurp仪表板复制您的API密钥。
PHP设置
客户端已在PHP 7上进行了测试,需要安装以下非默认PHP扩展
php-ext-curl php-xml php-mbstring
通常,这些扩展包含在大多数PHP安装中。在Linux上,也可以按如下方式安装
sudo apt-get install php-ext-curl php-mbstring php-xml
添加PHP库
有几种方法可以安装MailSlurp。
a) Composer依赖项
如果您使用composer包管理器,则可以运行
composer require mailslurp/mailslurp-client-php
或将它添加到您的composer.json
文件中
{ "repositories": [ { "type": "vcs", "url": "https://github.com/mailslurp/mailslurp-client-php.git" } ], "require": { "mailslurp/mailslurp-client-php": "*@dev" } }
然后使用composer自动加载约定包含库
<?php // assuming your file is in root directory and a vendor folder exists next to it require_once(__DIR__ . '/vendor/autoload.php');
b) 手动安装
或者,您可以直接下载PHP库并将其包含在项目的根目录中。
从MailSlurp仪表板下载MailSlurp PHP电子邮件库。然后包含文件以在代码中访问MailSlurp
<?php require_once('/path/to/MailSlurpClient/vendor/autoload.php');
创建API实例
要调用MailSlurp API,您必须创建要访问的控制器的一个实例。每个实例都必须配置API密钥。有关所有控制器和方法的列表,请参阅方法文档。
配置API密钥
private function getConfig() { // create a mailslurp configuration with API_KEY environment variable // get your own free API KEY at https://app.mailslurp.com/sign-up/ return MailSlurp\Configuration::getDefaultConfiguration() ->setApiKey('x-api-key', getenv("API_KEY")); }
创建控制器
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->getConfig());
基本用法
一个常见的用例是创建一个新的电子邮件地址,向其发送电子邮件,然后接收内容
创建电子邮件地址
要创建电子邮件地址,请使用收件箱控制器创建新的收件箱。收件箱具有真实电子邮件地址,可以发送和接收电子邮件以及附件。
$options = new \MailSlurp\Models\CreateInboxDto(); $options->setName("Test inbox"); $options->setPrefix("test"); $inbox = $inboxController->createInboxWithOptions($options);
发送电子邮件
以下是一个发送电子邮件的简单示例
$send_options = new MailSlurp\Models\SendEmailOptions(); $send_options->setTo([$inbox->getEmailAddress()]); $send_options->setSubject("Test"); $send_options->setBody("Confirmation code = abc123"); $inbox_controller->sendEmail($inbox_2->getId(), $send_options);
阅读电子邮件
您可以使用WaitFor控制器方法等待预期电子邮件到达收件箱
$wait_for_controller = new MailSlurp\Apis\WaitForControllerApi(null, $this->getConfig());
然后可以这样调用它
$timeout_ms = 30000; $unread_only = true; $email = $wait_for_controller->waitForLatestEmail($inbox->getId(), $timeout_ms, $unread_only);
获取电子邮件
您可以使用收件箱控制器从收件箱中获取电子邮件
您还可以以分页形式获取
获取电子邮件
使用电子邮件控制器直接通过ID获取电子邮件
s $emails = $inbox_controller->getEmails($inbox->getId());
SMTP访问(PHPMailer、mail函数等)
SMTP收件箱通过getImapSmtpAccess函数提供IMAP和SMTP访问凭据。使用这些凭据配置PHPMailer、PearMailer或其他SMTP发送客户端。首先创建一个SMTP_INBOX
$options = new \MailSlurp\Models\CreateInboxDto(); $options->setInboxType("SMTP_INBOX"); $inbox_smtp = $inboxController->createInboxWithOptions($options);
然后使用访问设置来配置PHPMailer
$access_details = $inboxController->getImapSmtpAccess($inbox_smtp->getId()); $mail = new PHPMailer\PHPMailer\PHPMailer(true); // set mail server settings using the inbox access details $mail->isSMTP(); $mail->Host = $access_details->getSecureSmtpServerHost(); $mail->SMTPAuth = true; $mail->Username = $access_details->getSecureSmtpUsername(); $mail->Password = $access_details->getSecureSmtpPassword(); $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = $access_details->getSecureSmtpServerPort();
列出收件箱
收件箱列表是分页的。
<?php public function test_canListInboxes(): void { // create an inbox controller with config $inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->config); $pageInboxes = $inboxController->getAllInboxes($favourite = null, $page = 0, $size = 20); // assert pagination properties $this->assertEquals(0, $pageInboxes->getNumber()); $this->assertEquals(20, $pageInboxes->getSize()); $this->assertGreaterThan(0, $pageInboxes->getTotalElements()); // access inboxes via content foreach ($pageInboxes->getContent() as $inbox) { $this->assertNotNull($inbox->getId()); } }
使用更多选项发送电子邮件
要发送电子邮件,首先创建一个收件箱。然后使用InboxController
上的sendEmail
方法,并传入发送者收件箱的ID和电子邮件选项。
<?php public function test_canSendEmail(): void { // create a new inbox $inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->config); $inbox = $inboxController->createInbox(); // send options $sendOptions = new MailSlurp\Models\SendEmailOptions(); $sendOptions->setTo([$inbox->getEmailAddress()]); $sendOptions->setSubject("Welcome"); $sendOptions->setIsHtml(true); // (you can use normal strings too) $sendOptions->setBody(<<<EOF <html> <h1>MailSlurp supports HTML</h1> </html> EOF); // send $inboxController->sendEmail($inbox->getId(), $sendOptions); }
附件
在PHP中使用附件非常简单。要发送附件,您必须首先上传每个文件并存储返回的附件ID。要发送,请使用这些ID与SendEmailOptions
一起使用。收到的电子邮件附件可以通过EmailControllerApi
下载。
<?php private function uploadAttachment(): array { // a path to some file you want to attach $pathToAttachment = $this->pathToAttachment; // read file contents $contents = file_get_contents($pathToAttachment); // encode the file contents to a base64 encoded string for uploading $base64Contents = base64_encode($contents); // extract file name from path $filename = basename($pathToAttachment); // get the mime type from the file name $contentType = mime_content_type($filename); // set upload options $uploadOptions = new MailSlurp\Models\UploadAttachmentOptions(); $uploadOptions->setFilename($filename); $uploadOptions->setContentType($contentType); $uploadOptions->setBase64Contents($base64Contents); // now upload using attachment controller $attachmentController = new MailSlurp\Apis\AttachmentControllerApi(null, $this->config); // returns [$attachmentId] return $attachmentController->uploadAttachment($uploadOptions); }
要发送附件,可以这样操作
<?php $attachmentIds = $this->uploadAttachment(); // send options with attachments $sendOptions = new MailSlurp\Models\SendEmailOptions(); $sendOptions->setTo([$inbox->getEmailAddress()]); $sendOptions->setSubject("See attached!"); $sendOptions->setAttachments($attachmentIds); $inboxController->sendEmail($inbox->getId(), $sendOptions);
接收电子邮件
您可以使用MailSlurp在PHP中接收电子邮件。首先创建一个收件箱,然后调用waitFor
方法。这些方法会等待新未读电子邮件到达并返回它。
<?php // get an email in the inbox $timeout_millis = 10000; // wait at most 10 seconds for new email $unread_only = true; // only count unread emails $email = $waitForController->waitForLatestEmail($inbox->getId(), $timeout_millis, $unread_only); // access email content and properties print_r($email->getBody()) print_r($email->getSubject())
更大示例
<?php // create inbox to send from $inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->config); $inbox1= $inboxController->createInbox(); $inbox2 = $inboxController->createInbox(); // send options $sendOptions = new MailSlurp\Models\SendEmailOptions(); $sendOptions->setTo([$inbox2->getEmailAddress()]); $sendOptions->setSubject("Here are your files"); $sendOptions->setBody("Hello <strong>JOE</strong>"); $sendOptions->setIsHtml(true); // send email from inbox 1 to inbox 2 using options above $inboxController->sendEmail($inbox1->getId(), $sendOptions); // wait for email to arrive in inbox 2 $waitForController = new MailSlurp\Apis\WaitForControllerApi(null, $this->config); $email = $waitForController->waitForLatestEmail($inbox_id=$inbox2->getId(), $timeout=30000); // access email contents $this->assertEquals($email->getInboxId(), $inbox2->getId()); $this->assertStringContainsString("Here are your files", $email->getSubject());
接收附件
测试示例
<?php $emailAttachmentId1 = $email->getAttachments()[0]; // download an attachment as base 64 encoded string $emailController = new MailSlurp\Apis\EmailControllerApi(null, $this->config); $downloadedAttachment = $emailController->downloadAttachmentBase64($emailAttachmentId1, $email->getId()); $this->assertGreaterThan(0, $downloadedAttachment->getSizeBytes()); $this->assertEquals("text/plain", $downloadedAttachment->getContentType()); // decode file contents $content = base64_decode($downloadedAttachment->getBase64FileContents()); $this->assertStringContainsString("Sample upload file", $content);
提取电子邮件内容
您可以使用正则表达式以如下方式解析电子邮件正文中的代码或语句
$matches = array(); preg_match('/.+code = (.+)/', $email->getBody(), $matches); $confirmation_code = $matches[1]; $this->assertEquals($confirmation_code, "abc123");