Send emails using utopia-php/messaging and MailSlurp
Ilhan Ates

Ilhan Ates / October 02, 2023

2 min read––– views

If you've ever used a web application, chances are quite high that you've received an email from it.

If you're building a web application, chances are you might also want to send an email from it.

Well, using MailSlurp and utopia-php/messaging, you can do that with just a couple lines!

Getting Started

1. Getting the MailSlurp API key

First we will go ahead and sign up for a free personal MailSlurp account.

  • Go to mailslurp.com and click on Sign Up.

  • Sign up using your email or one of the OAuth providers. image

  • Go to settings and copy your API key. image

  • Optionally setup a domain under Inboxes > Domains and create an inbox under the Inbox tab. We will skip for the simplicity of this tutorial.

Note that unfortunately with a free plan, you can't send emails to big providers as stated here, so you might also want to upgrade your account.

2. Using utopia-php/messaging

Now that we got the API key, you will need to install utopia-php/messaging in your PHP project, following their README:

composer require utopia-php/messaging
<?php
 
use \Utopia\Messaging\Messages\Email;
use \Utopia\Messaging\Adapters\Email\MailSlurp;
 
$message = new Email(
    to: ['[email protected]'],
    subject: 'Hello World',
    content: '<h1>Hello World</h1>',
    from: '[email protected]'
);
 
$messaging = new MailSlurp('YOUR_API_KEY');
$messaging->send($message);

And simple as that, the email should be sent!