Email Sending using Aspose.Email Cloud in Heroku Python App

Heroku is a platform as a service (PaaS). This article helps you set up Heroku Python app, and how to use Aspose.Email Cloud for email sending.

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It supports several programming languages. This blog guides you on how to deploy to Heroku (Python App). The article helps you understand Aspose.Email Cloud, and how to use it for email sending using Heroku Python App. The article assumes that you already have a free Heroku Account setup and Python and pip 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 Python and Pip. You now have a functioning git repository that contains a simple application, a runtime.txt specifying which Python version will be used, and a requirements.txt, which is used by Python’s dependency manager, Pip.

$ mkdir my-email-sending-app
$ cd my-email-sending-app
$ pip install aspose-email-cloud

add the following code in your python file

from AsposeEmailCloudSdk import api #EmailApi class is here
from AsposeEmailCloudSdk import models #REST API models are here

// Setup App Credentials 
client_secret = 'Your Client secret'
client_id = 'Your Client id'

email_cloud = api.EmailCloud(client_secret, client_id)

credentials = models.EmailClientAccountPasswordCredentials(
    'example@gmail.com', 'password')

// Setup an email account for email sending
send_account_dto = models.EmailClientAccount(
    'smtp.gmail.com', 465, 'SSLAuto', 'SMTP', credentials)
smtp_account = 'smtp.account'
smtp_location = models.StorageFileLocation(
    storage_name, account_folder, smtp_account)

api.client.account.save(
    models.ClientAccountSaveRequest(
        smtp_location, send_account_dto))

email_document = models.EmailDto(
    _from=models.MailAddress('From Name', 'example@gmail.com'),
    to=[models.MailAddress('To Name', 'to@aspose.com')],
    subject='Some subject',
    body='Some body')

api.client.message.send(
    models.ClientMessageSendRequest(
        smtp_location, models.MailMessageDto(email_document)))

Deploy to Heroku (Python App)

Once you are done with all your changes and ready to deploy to Heroku (Python 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(Pythoon 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 as a service (PaaS) and how it enables developers to build, run, and operate applications entirely in the cloud. We also learned how to set up a Heroku Python 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