Email Sending using Aspose.Email Cloud in Heroku PHP App

A tutorial on how to setup a Heroku PHP app, and how to use Aspose.Email Cloud for email sending in PHP application.

This blog guides you on how to deploy to Heroku (PHP App). The article helps you understand Aspose.Email Cloud, and how to use it for email sending using Heroku PHP App. The article assumes that you already have a free Heroku Account setup and PHP and Composer installed locally. Let’s get started!

Setup Heroku

To get started you first need to install the Heroku Command Line Interface (CLI). Heroku CLI is used to manage and perform various scalability tasks. You can use this to provision add-ons, view your application logs, and run your application locally. If you are using macOS, you can use Homebrew to install it or can visit the official Heroku.

brew install heroku/brew/heroku

Once the installation is done you can run the following command to authenticate Heroku to be used locally.

heroku login
heroku: Press any key to open up the browser to login or q to exit
 ›   Warning: If browser does not open, visit
 ›   https://cli-auth.heroku.com/auth/browser/\*\*\*
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com

This command opens your browser to the Heroku login page for authentication. This is required for both Heroku and git commands to work properly

Setup Aspose.Email Cloud

Aspose.Email Cloud is a Cloud SDK to send, receive, append, flag, and convert cloud emails & support to create a folder structure for email archiving in the cloud. This is easy to use and fast API, that doesn’t need to install the additional software. The API supports many programming languages, such as C#, Java, PHP, Python, Ruby & Typescript. To set up Aspose.Email Cloud follow the instructions as below.

Login to Aspose.Cloud Dashboard and click the Applications tab in the left sidebar. Now scroll down, click Create New Application button as shown in the image below.

Aspose.Email Cloud Setup Account for Email Sending

Now create your new application by selecting your default storage as shown in the image below. You can also configure third-party storage to upload your data files by following how to configure 3rd party cloud storage guide.

Aspose.Email Cloud Setup Account for Email Sending

Now, scroll down towards the Client Credentials section to copy Client Id and Client Secret as shown in the image below.

Aspose.Email Cloud Setup Account for Email Sending

Email Sending using Aspose.Email Cloud

Assuming you’ve already installed PHP and Composer. Heroku uses Composer for dependency management in PHP projects and the composer.json file indicates to Heroku that your application is written in PHP. Please create a directory for your application.

$ mkdir my-email-sending-app
$ cd my-email-sending-app
$ composer require aspose/aspose-email-cloud

add the following code in your index.php file

include_once(__DIR__.'/vendor/autoload.php'); 
use Aspose\Email\EmailCloud;
use Aspose\Email\Configuration;
use Aspose\Email\Model\EmailDto;
use Aspose\Email\Model\MailAddress;
use Aspose\Email\Model\MailMessageDto;
use Aspose\Email\Model\EmailClientAccount;
use Aspose\Email\Model\StorageFileLocation;
use Aspose\Email\Model\ClientAccountSaveRequest;
use Aspose\Email\Model\ClientMessageSendRequest;
use Aspose\Email\Model\EmailClientAccountPasswordCredentials;

// Setup App Credentials 
$configuration = new Configuration();
$configuration
	->setClientSecret("\*\*\*\*")
	->setClientId("\*\*\*\*");

// Setup the SDK
$api = new EmailCloud($configuration);


$credentials = new EmailClientAccountPasswordCredentials(
    "email", "password");

// Setup an email account for email sending
$sendAccountDto = new EmailClientAccount(
    "smtp@server", 465, "SSLAuto", "SMTP", $credentials);
$smtpAccount = "smtp.account";
$storageName = 'storagename';
$accountFolder = 'myfolder';
$smtpLocation = new StorageFileLocation(
    $storageName, $accountFolder, $smtpAccount);

$api->client()->account()->save(
    new ClientAccountSaveRequest($smtpLocation, $sendAccountDto));

$email = (new EmailDto())
    ->setFrom(new MailAddress("MyName", "my@email.com"))
    ->setTo(array(new MailAddress("Testing", "to@email.com")))
    ->setSubject("Testing Email from PHP")
    ->setBody("Some body from PHP");

$api->client()->message()->send(
    new ClientMessageSendRequest(
        $smtpLocation, new MailMessageDto($email)));

Deploy to Heroku (PHP App)

Once you are done with all your changes and ready to deploy to Heroku (PHP App), you can use the following commands to push your changes to Heroku.

$ heroku create
Creating sharp-rain-871... done, stack is heroku-18
http://sharp-rain-871.herokuapp.com/ | https://git.heroku.com/sharp-rain-871.git
Git remote heroku added

this will create a git repository on Heroku and anything you push to this repo will deploy to Heroku(PHP App).

$ git push heroku main

Now you can open your application using the heroku open command.

Conclusion

In this article, we learned about the Heroku platform and how we can set up a Heroku PHP App. We also explored the Aspose.Email Cloud, and used to set up an SMTP email client for email sending. Aspose.Email Cloud isn’t just for sending emails. Instead, it’s a Cloud SDK to send, receive, append, flag, and convert cloud emails & support to create a folder structure for email archiving in the cloud. This is easy to use and fast API, that doesn’t need to install the additional software. The API supports many programming languages, such as C#, Java, PHP, Python, Ruby & Typescript. I hope this article helped you.

Explore