PHP code example of mailslurp / mailslurp-client-php
1. Go to this page and download the library: Download mailslurp/mailslurp-client-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mailslurp / mailslurp-client-php example snippets
// 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());
// assuming your file is in root directory and a vendor folder exists next to it
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);
s
$emails = $inbox_controller->getEmails($inbox->getId());
$options = new \MailSlurp\Models\CreateInboxDto();
$options->setInboxType("SMTP_INBOX");
$inbox_smtp = $inboxController->createInboxWithOptions($options);
$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();
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());
}
}
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);
}
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);
}
$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);
// 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())
// 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());
$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);