BlogDeveloperPHP Frameworks In The Wild: A Closer Look at the PHP Ecosystem

SparkPost and PHP Frameworks: Scratching the Surface

PHP is one of the older languages used on the Web, created in the mid-nineties. Some environments are still stuck running PHP 3 while others are running the latest version. There’s been a lot of development around amazing PHP frameworks and tools in the last few years, and most of them come with easy ways to send mail. We recently broke down how we simplified our PHP library, but going with a native solution can still be easier sometimes. Here’s a quick breakdown of how to use SparkPost to send mail with some of the more popular PHP frameworks in the wild.

Plain ol’ PHP

Sometimes you just need to fire off a quick API call. All you need is a little cURL magic to do what you need. Check out this snippet for shooting off SparkPost requests.

🐘 + 🎶 = ❤️

Here at SparkPost we created a composer package to simplify using our API in all modern PHP environments. It’s quick to install and provides some “sugar” to smooth out some of the more confusing parts of the API. To keep our library as flexible as possible, we use PHP HTTP to let you bring your own request library. To use it we can require
guzzlehttp/guzzle, the
php-http/guzzle6-adapter, and the
sparkpost/sparkpost library. Then we are good to go with the code below.

World of WordPress

Our very own Raju wrote a SparkPost plugin to use SparkPost as your email provider. It exposes key features like using templates, sending via SMTP or the API, and allows you to hook in at a ton of points to modify the email and settings on the fly. Plus, it’s easy to get started 😎

PHPMailer

PHPMailer is probably the most popular way to send mail in PHP! It’s got a great API that makes it easy to plug any SMTP provider. Check out this simple example to get started with SparkPost quickly.

$mail->Body='Congratulations!! You just sent an email from PHPMailer!';

if(!$mail->send()){

echo"Message could not be sent\n";

echo"Mailer Error: ".$mail->ErrorInfo."\n";

}else{

echo"Message has been sent\n";

}

Laravel

Laravel is a whole beast unto itself so I’ll just hit the main points. You can follow the official Laravel docs to get set up with the SparkPost mail driver. Once you’ve done that you can generate and send your mailables. Matt Stauffer did a solid write up on using SparkPost with Laravel which is worth checking out.

CodeIgniter

CodeIgniter provides a simple API to send mail. You can add the SMTP configuration in code or through config/email.php. Check out the example below to fire off a quick email.

We use cookies to optimize your experience, analyze traffic, and personalize content. To learn more, please visit our Cookie Policy. By using our site without disabling cookies, you consent to our use of them.