在 Heroku PHP 應用程式中使用 Aspose.Email Cloud 發送電子郵件

有關如何設定 Heroku PHP 應用程式以及如何在 PHP 應用程式中使用 Aspose.Email Cloud 發送電子郵件的教學課程。

本部落格指導您如何部署到 Heroku(PHP 應用程式)。本文可協助您了解 Aspose.Email Cloud,以及如何使用它透過 Heroku PHP App 發送電子郵件。本文假設您已經設定了免費的 Heroku 帳戶,並且在本地安裝了 PHP 和 Composer。讓我們開始吧!

設定 Heroku

首先需要安裝 Heroku 命令列介面 (CLI)。 Heroku CLI 用於管理和執行各種可擴充性任務。您可以使用它來配置附加元件,查看應用程式日誌,並在本機上運行您的應用程式。如果您使用的是 macOS,您可以使用 Homebrew 來安裝它,或者可以訪問官方 Heroku。

brew install heroku/brew/heroku

安裝完成後,您可以執行以下命令來驗證 Heroku 以供本機使用。

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

此命令將開啟您的瀏覽器至 Heroku 登入頁面進行身份驗證。這是 Heroku 和 git 命令正常工作所必需的

設定 Aspose.Email Cloud

Aspose.Email Cloud 是一個雲端 SDK,用於傳送、接收、附加、標記和轉換雲端電子郵件,並支援在雲端中建立用於電子郵件存檔的資料夾結構。這是一個易於使用且快速的 API,無需安裝附加軟體。該 API 支援多種程式語言,例如 C#、Java、PHP、Python、Ruby 和 Typescript。若要設定 Aspose.Email Cloud,請按照以下說明進行操作。

登入 Aspose.Cloud 儀表板並點擊左側欄中的「應用程式」標籤。現在向下捲動,點擊「建立新應用程式」按鈕,如下圖所示。

Aspose.Email Cloud 設定郵件發送帳戶

現在透過選擇您的預設儲存來建立新的應用程序,如下圖所示。您也可以按照如何設定第三方雲端儲存指南配置第三方儲存來上傳您的資料檔案。

Aspose.Email Cloud 設定郵件發送帳戶

現在,向下捲動至客戶端憑證部分以複製客戶端 ID 和客戶端金鑰,如下圖所示。

Aspose.Email Cloud 設定郵件發送帳戶

使用 Aspose.Email Cloud 發送電子郵件

假設您已經安裝了 PHP 和 Composer。 Heroku 使用 Composer 進行 PHP 專案中的依賴管理,並且 composer.json 檔案向 Heroku 表明您的應用程式是用 PHP 編寫的。請為您的應用程式建立一個目錄。

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

在 index.php 檔案中加入以下程式碼

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)));

部署至 Heroku(PHP 應用程式)

完成所有變更並準備部署到 Heroku(PHP 應用程式)後,您可以使用以下命令將變更推送到 Heroku。

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

這將在 Heroku 上建立一個 git 儲存庫,並且您推送到該儲存庫的任何內容都會部署到 Heroku(PHP 應用程式)。

$ git push heroku main

現在您可以使用 heroku open 命令開啟您的應用程式。

結論

在本文中,我們了解了 Heroku 平台以及如何設定 Heroku PHP 應用程式。我們還探索了 Aspose.Email Cloud,並用於設定 SMTP 電子郵件用戶端以發送電子郵件。 Aspose.Email Cloud 不僅用於發送電子郵件。相反,它是一個雲端 SDK,用於發送、接收、附加、標記和轉換雲端電子郵件,並支援在雲端中建立用於電子郵件存檔的資料夾結構。這是一個易於使用且快速的 API,無需安裝附加軟體。該 API 支援多種程式語言,例如 C#、Java、PHP、Python、Ruby 和 Typescript。希望本文對您有幫助。

探索