
Microsoft Word 的郵件合併是一項出色的功能,它使用戶能夠將信件轉換為讀起來像個人信件的文件。與準備單封信件來向許多人傳達一組訊息的過程相比,郵件合併可以節省時間和精力,產生帶有標籤或信封的群發郵件,並且您可以發送帶有定制詳細信息的統一訊息。郵件合併的其他好處包括
- 一旦設定了合併,我們就可以快速產生數千封信件。
- 校對一封信比校對數千封單獨的信件要容易得多。
- 可以向符合特定標準的人發送信件。
- 保存並重複使用標準信件。
- 個性化信件。
- 相同的資料來源並可重複使用–降低了錯誤的風險。
考慮到此功能的重要性,Aspose.Words Cloud 完美支援郵件合併功能。它還允許您根據收件人的姓名對信件進行個性化設定。資料來源可以是與文件相關的清單、電子表格或資料庫。在這篇文章中,我們將討論使用 PHP 語言向郵件合併或鬍鬚模板填入資料。
使用範本進行郵件合併
為了使用 MailMerge 功能,我們需要使用 Word 範本文件和資料檔案。這是 ExecuteMailMergeOnline 的兩個輸入參數
cRUL 指令
curl -X PUT "https://api.aspose.cloud/v4.0/words/MailMerge?withRegions=false&documentFileName=template" -H "accept: application/octet-stream" -H "Authorization: Bearer <JWT Token>" -H "Content-Type: multipart/form-data" -H "x-aspose-client: Containerize.Swagger" -d {"Template":{},"Data":{}}
請求 URL
https://api.aspose.cloud/v4.0/words/MailMerge?withRegions=false&documentFileName=template
PHP
<?php
include(dirname(__DIR__).'\CommonUtils.php');
use Aspose\Words\WordsApi;
use Aspose\Words\Model\Requests;
try {
// 建立 WordsApi 實例
$wordsApi = new WordsApi(CommonUtils::$AppSid, CommonUtils::$AppKey, CommonUtils::$ApiBaseUrl);
$fileName = "SampleMailMergeTemplate.docx";
$fileData = realpath(__DIR__ . '/../..').'\resources\\' . $fileName;
$destName = "Out_TestMailMergeWithImages.docx";
$data = realpath(__DIR__ . '/../..').'\resources\SampleMailMergeTemplateData.txt';
// 將文檔上傳至雲端存儲
$wordsApi->uploadFile(new Requests\UploadFileRequest(realpath(__DIR__ . '/../..').'\resources\\' . $fileName, "", CommonUtils::$MyStorage));
// 建立請求並執行 api 方法
$request = new Requests\ExecuteMailMergeOnlineRequest($fileData, $data, null, null, null);
$result = $wordsApi->executeMailMergeOnline($request);
print_r($result);
} catch (Exception $e) {
echo "Something went wrong: ", $e->getMessage(), "\n";
}
?>

圖 1:MailMerge 操作的預覽。
在郵件合併期間插入 HTML
除了簡單的內容之外,此 API 還提供在郵件合併欄位內插入 HTML 的功能。 ExecuteMailMerge API 可讓您在郵件合併欄位內插入 HTML。按照下面的範例,我們將在模板檔案中插入 HTML 表和圖像,並將生成的檔案儲存到雲端儲存。
cURL 指令
curl -X PUT "https://api.aspose.cloud/v4.0/words/template.doc/MailMerge?withRegions=false&mailMergeDataFile=TestExecuteMailMergeData%20%281%29.txt&useWholeParagraphAsRegion=true" -H "accept: application/json" -H "Authorization: Bearer <JWT Token>" -H "Content-Type: multipart/form-data" -H "x-aspose-client: Containerize.Swagger" -d {"data":""}
請求 URL
https://api.aspose.cloud/v4.0/words/template.doc/MailMerge?withRegions=false&mailMergeDataFile=TestExecuteMailMergeData%20%281%29.txt&useWholeParagraphAsRegion=true
PHP
<?php
include(dirname(__DIR__).'\CommonUtils.php');
use Aspose\Words\WordsApi;
use Aspose\Words\Model\Requests;
try {
// 建立 WordsApi 實例
$wordsApi = new WordsApi(CommonUtils::$AppSid, CommonUtils::$AppKey, CommonUtils::$ApiBaseUrl);
$fileName = "template.doc";
$destName = "Out_PostDocumentExecuteMailMergeWithHTMLData.docx";
$data = file_get_contents(realpath(__DIR__ . '/../..').'\resources\TestPostDocumentExecuteMailMerge.txt');
// 將文檔上傳至雲端存儲
$wordsApi->uploadFile(new Requests\UploadFileRequest(realpath(__DIR__ . '/../..').'\resources\\' . $fileName, "", CommonUtils::$MyStorage));
// 建立請求並執行 api 方法
$request = new Requests\ExecuteMailMergeRequest($fileName, $data, null, CommonUtils::$MyStorage, null, null, null, null, null, false, $destName);
$result = $wordsApi->executeMailMerge($request);
print_r($result);
} catch (Exception $e) {
echo "Something went wrong: ", $e->getMessage(), "\n";
}
?>

圖 2:- 插入 HTML 後的郵件合併。
結論
在本文中,我們討論如何使用 PHP Cloud SDK 執行郵件合併的詳細資訊。我們特別探討了使用範本執行郵件合併以及如何在郵件合併期間插入 HTML 相關的細節。請注意,Aspose.Words Cloud SDK for PHP 是在 MIT 許可下開發的,其完整的原始碼可透過 GitHub 下載。如果您在使用 SDK 時遇到任何問題,請隨時透過免費的產品支援論壇與我們聯絡。
相關文章
我們還建議您訪問以下連結以了解更多資訊: