

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 透過 Postfix 整合 Amazon SES
<a name="postfix"></a>

Postfix 為普及度最高的 Sendmail 訊息傳輸代理程式 (MTA) 之替代程式。如需 Postfix 的資訊，請前往 [http://www.postfix.org](http://www.postfix.org)。此主題中的程序將搭配 Linux、macOS 或 Unix 操作。

**注意**  
Postfix 是第三方應用程式，並非由 Amazon Web Services 開發或支援。本節中的程序僅供參考，如有變更，恕不另行通知。

## 先決條件
<a name="send-email-postfix-prereqs"></a>

在完成本節的程序前，您必須先執行下列任務：
+ 若您的系統已安裝 Sendmail 應用程式，請先解除安裝。完成此步驟的程序因您使用的作業系統而異。
**重要**  
以下提到的 *sendmail* 是指 Postfix 命令 `sendmail`，不要與 Sendmail 應用程式混淆。
+ 安裝 Postfix。完成此步驟的程序因您使用的作業系統而異。
+ 安裝 SASL 身分驗證套件。完成此步驟的程序因您使用的作業系統而異。例如，如果您使用 RedHat 為基礎的系統，您應該安裝 `cyrus-sasl-plain` 套件。如果您使用 Debian 或 Ubuntu 為基礎的系統，您應該安裝 `libsasl2-modules` 套件。
+ 驗證用於傳送電子郵件的電子郵件地址或網域。如需詳細資訊，請參閱[建立電子郵件地址身分](creating-identities.md#verify-email-addresses-procedure)。
+ 如果您的帳戶仍在沙盒內，您只能將電子郵件傳送到已驗證的電子郵件地址。如需詳細資訊，請參閱「[請求生產存取權 （移出 Amazon SES 沙盒）](request-production-access.md)」。

## 設定 Postfix
<a name="send-email-postfix"></a>

完成以下步驟來設定您的郵件伺服器，以使用 Postfix 來透過 Amazon SES 傳送電子郵件。

**若要設定 Postfix**

1. 在命令列中輸入以下命令：

   ```
   sudo postconf -e "relayhost = [email-smtp.us-west-2.amazonaws.com]:587" \
   "smtp_sasl_auth_enable = yes" \
   "smtp_sasl_security_options = noanonymous" \
   "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" \
   "smtp_use_tls = yes" \
   "smtp_tls_security_level = secure" \
   "smtp_tls_note_starttls_offer = yes"
   ```
**注意**  
如果您在美國西部 （奧勒岡） 以外的 AWS 區域使用 Amazon SES，請將上述命令中的 *email-smtp.us-west-2.amazonaws.com* 取代為適當區域的 SMTP 端點。如需詳細資訊，請參閱[區域和 Amazon SES](regions.md)。

1. 在文字編輯器中開啟檔案 `/etc/postfix/master.cf`。搜尋下列項目：

   ```
   -o smtp_fallback_relay=
   ```

   如果發現此項目，請在程式碼的開頭放置 `#` (雜湊) 字元來註解它。儲存並關閉檔案。

   否則請移至下一個步驟 (如果此項目不存在)。

1. 在文字編輯器中開啟檔案 `/etc/postfix/sasl_passwd`。如果檔案不存在，請建立檔案。

1. 新增下行到 `/etc/postfix/sasl_passwd`：

   ```
   [email-smtp.us-west-2.amazonaws.com]:587 SMTPUSERNAME:SMTPPASSWORD
   ```
**注意**  
將 *SMTPUSERNAME* 和 *SMTPPASSWORD* 取代為您的 SMTP 登入憑證。您的 SMTP 登入憑證與您的 AWS 存取金鑰 ID 和私密存取金鑰不同。如需憑證的詳細資訊，請參閱 [取得 Amazon SES SMTP 憑證](smtp-credentials.md)。  
如果您在美國西部 （奧勒岡） 以外的 AWS 區域使用 Amazon SES，請將上述範例中的 *email-smtp.us-west-2.amazonaws.com* 取代為適當區域的 SMTP 端點。如需詳細資訊，請參閱[區域和 Amazon SES](regions.md)。

   儲存並關閉 `sasl_passwd`。

1. 在命令提示中，輸入下列命令來建立包含您 SMTP 登入資料的雜湊對應資料庫檔案：

   ```
   sudo postmap hash:/etc/postfix/sasl_passwd
   ```

1. (選用) 您在前面步驟中建立的 `/etc/postfix/sasl_passwd` 和 `/etc/postfix/sasl_passwd.db` 檔案不會加密。由於這些檔案包含您的 SMTP 登入資料，我們建議您修改檔案的所有權和權限，以限制存取。若要限制存取這些檔案：

   1. 在命令提示中，輸入下列命令來變更檔案的所有權：

      ```
      sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
      ```

   1. 在命令提示中，輸入下列命令來變更檔案的許可，只讓根使用者可以讀取或寫入它們：

      ```
      sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
      ```

1. 告訴 Postfix 可在哪裡找到憑證授權機構憑證 (需要驗證 Amazon SES 伺服器憑證)。在此步驟中使用的命令將根據您的作業系統而有不同。
   + 如果您使用 Amazon Linux、Red Hat Enterprise Linux 或相關的分佈類型，請輸入下列命令：

     ```
     sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt'
     ```
   + 如果您使用 Ubuntu 或相關的分佈，請輸入下列命令：

     ```
     sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt'
     ```
   + 如果您使用 macOS，您可以從您的系統金鑰鏈產生憑證。若要產生憑證，請在命令列輸入下列命令：

     ```
     sudo security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain | sudo tee /etc/ssl/certs/ca-bundle.crt > /dev/null
     ```

     產生憑證之後，請輸入下列命令：

     ```
     sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt'
     ```

1. 輸入以下命令啟動 Postfix 伺服器 (如果伺服器已在執行，則重新載入組態設定)：

   ```
   sudo postfix start; sudo postfix reload
   ```

1. 在命令列輸入下列行並在各行末按 Enter 鍵以傳送測試電子郵件。以您的寄件者電子郵件地址取代 *sender@example.com*。寄件者地址需經驗證才可搭配 Amazon SES 使用。以目標地址取代 *recipient@example.com*。如果您的帳戶仍在沙盒中，也必須驗證收件人地址。最後，訊息的最後一行必須包含單一句號 (.)，沒有其他內容。

   ```
   sendmail -f sender@example.com recipient@example.com
   From: Sender Name <sender@example.com>
   Subject: Amazon SES Test                
   This message was sent using Amazon SES.                
   .
   ```

1. 檢查與收件人地址相關的信箱。如未收到電子郵件，請檢查您的垃圾郵件資料夾。如果仍找不到電子郵件，請檢查用以傳送電子郵件的系統郵件日誌 (通常位於 `/var/log/maillog`) 以取得詳細資訊。

## 進階用法範例
<a name="send-email-postfix-advanced"></a>

此範例說明如何使用[組態集](using-configuration-sets.md)來傳送電子郵件，以及使用 MIME 分段編碼來傳送訊息的純文字和 HTML 版本，同時包含附件。它也包含一個[連結標籤](faqs-metrics.md#sending-metric-faqs-clicks-q5)，可用於分類點選事件。電子郵件的內容中於外部檔案中指定，因此您不需要在 Postfix 工作階段中手動輸入指令。

**若要使用 Postfix 傳送分段 MIME 電子郵件**

1. 在文字編輯器中，建立名為 `mime-email.txt` 的新檔案。

1. 在文字檔案中，貼上下列內容以適用於帳戶的值來取代紅字值：

   ```
   X-SES-CONFIGURATION-SET: ConfigSet
   From:Sender Name <sender@example.com>
   Subject:Amazon SES Test
   MIME-Version: 1.0
   Content-Type: multipart/mixed; boundary="YWVhZDFlY2QzMGQ2N2U0YTZmODU"
   
   --YWVhZDFlY2QzMGQ2N2U0YTZmODU
   Content-Type: multipart/alternative; boundary="3NjM0N2QwMTE4MWQ0ZTg2NTYxZQ"
   
   --3NjM0N2QwMTE4MWQ0ZTg2NTYxZQ
   Content-Type: text/plain; charset=UTF-8
   Content-Transfer-Encoding: quoted-printable
   
   Amazon SES Test
   
   This message was sent from Amazon SES using the SMTP interface.
   
   For more information, see:
   http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html
   
   --3NjM0N2QwMTE4MWQ0ZTg2NTYxZQ
   Content-Type: text/html; charset=UTF-8
   Content-Transfer-Encoding: quoted-printable
   
   <html>
     <head>
   </head>
     <body>
       <h1>Amazon SES Test</h1>
         <p>This message was sent from Amazon SES using the SMTP interface.</p>
         <p>For more information, see
         <a ses:tags="samplekey0:samplevalue0;samplekey1:samplevalue1;" 
         href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html">
         Using the Amazon SES SMTP Interface to Send Email</a> in the <em>Amazon SES
         Developer Guide</em>.</p>
     </body>
   </html>
   --3NjM0N2QwMTE4MWQ0ZTg2NTYxZQ--
   --YWVhZDFlY2QzMGQ2N2U0YTZmODU
   Content-Type: application/octet-stream
   MIME-Version: 1.0
   Content-Transfer-Encoding: base64
   Content-Disposition: attachment; filename="customers.txt"
   
   SUQsRmlyc3ROYW1lLExhc3ROYW1lLENvdW50cnkKMzQ4LEpvaG4sU3RpbGVzLENh
   bmFkYQo5MjM4OSxKaWUsTGl1LENoaW5hCjczNCxTaGlybGV5LFJvZHJpZ3VleixV
   bml0ZWQgU3RhdGVzCjI4OTMsQW5heWEsSXllbmdhcixJbmRpYQ==
   --YWVhZDFlY2QzMGQ2N2U0YTZmODU--
   ```

   儲存並關閉檔案。

1. 在命令列中輸入以下命令。以您的電子郵件地址取代 *sender@example.com*，以收件人的電子郵件地址取代 *recipient@example.com*。

   ```
   sendmail -f sender@example.com recipient@example.com < mime-email.txt
   ```

   如果命令執行成功，將不提供任何輸出便直接關閉。

1. 請檢查您的收件匣，注意是否有電子郵件。如未遞送訊息，請檢查系統的郵件日誌。