DEV Community

Ilhan Ates
Ilhan Ates

Posted on • Originally published at ilhan.me

Send emails using utopia-php/messaging and MailSlurp

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
Enter fullscreen mode Exit fullscreen mode
<?php

use \Utopia\Messaging\Messages\Email;
use \Utopia\Messaging\Adapters\Email\MailSlurp;

$message = new Email(
    to: ['you@email.com'],
    subject: 'Hello World',
    content: '<h1>Hello World</h1>',
    from: 'your-inbox-identifier@mailslurp.com'
);

$messaging = new MailSlurp('YOUR_API_KEY');
$messaging->send($message);
Enter fullscreen mode Exit fullscreen mode

And simple as that, the email should be sent!

Top comments (0)