brown paper with silver round pendant

Tạo mật khẩu SMTP trong Amazon SES

Vào một ngày đẹp trời thiết lập gởi mail trong wordpress thì hỡi ôi làm mãi không gởi email đi được. Mãi mới xác định được là cách tạo xác thực trước đây cho SMTP bằng mật khẩu của AWS đã thay bằng Access key ID/AWS secret access key.

Với cặpAccess key ID/AWS secret access key thì sẽ được sử dụng cho Amazon SES API, nhưng các plugin dành cho wordpress thì lại không hỗ trợ, nếu có hỗ trợ thì phải sử dụng bản PRO mới có.

Vì vậy muốn sử dụng gởi email qua smtp bằng user/password thì phải “tạo mật khẩu smtp từAWS secret access key

Bước 1: Tạo tập tin smtp_credentials_generate.py

Cứ copy rồi lưu lại thành tập tin là được, không cần chỉnh sửa gì thêm nhé.

#!/usr/bin/env python3

import hmac
import hashlib
import base64
import argparse

SMTP_REGIONS = [
    'us-east-2',       # US East (Ohio)
    'us-east-1',       # US East (N. Virginia)
    'us-west-2',       # US West (Oregon)
    'ap-south-1',      # Asia Pacific (Mumbai)
    'ap-northeast-2',  # Asia Pacific (Seoul)
    'ap-southeast-1',  # Asia Pacific (Singapore)
    'ap-southeast-2',  # Asia Pacific (Sydney)
    'ap-northeast-1',  # Asia Pacific (Tokyo)
    'ca-central-1',    # Canada (Central)
    'eu-central-1',    # Europe (Frankfurt)
    'eu-west-1',       # Europe (Ireland)
    'eu-west-2',       # Europe (London)
    'sa-east-1',       # South America (Sao Paulo)
    'us-gov-west-1',   # AWS GovCloud (US)
]

# These values are required to calculate the signature. Do not change them.
DATE = "11111111"
SERVICE = "ses"
MESSAGE = "SendRawEmail"
TERMINAL = "aws4_request"
VERSION = 0x04


def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()


def calculate_key(secret_access_key, region):
    if region not in SMTP_REGIONS:
        raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.")

    signature = sign(("AWS4" + secret_access_key).encode('utf-8'), DATE)
    signature = sign(signature, region)
    signature = sign(signature, SERVICE)
    signature = sign(signature, TERMINAL)
    signature = sign(signature, MESSAGE)
    signature_and_version = bytes([VERSION]) + signature
    smtp_password = base64.b64encode(signature_and_version)
    return smtp_password.decode('utf-8')


def main():
    parser = argparse.ArgumentParser(
        description='Convert a Secret Access Key for an IAM user to an SMTP password.')
    parser.add_argument(
        'secret', help='The Secret Access Key to convert.')
    parser.add_argument(
        'region',
        help='The AWS Region where the SMTP password will be used.',
        choices=SMTP_REGIONS)
    args = parser.parse_args()
    print(calculate_key(args.secret, args.region))


if __name__ == '__main__':
    main()

Chạy lệnh tạo mật khẩu

python path/to/smtp_credentials_generate.py wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY us-east-1

Sau khi chạy lệnh trên, chúng ta sẽ được 1 dãy ký tự mới, đó chính là mật khẩu STMP. Sử dụngAccess key ID và mật khẩu được tạo ra ở trên để cài đặt trong các plugin gởi email bằng SMTP. Bên dưới là phần thiết lập cho plugin WP MAIL SMTP:

Tham khảo thêm các bài viết về Amazon SES:

https://docs.aws.amazon.com/ses/latest/dg/smtp-credentials.html

https://cuongthach.com/email-marketing/huong-dan-su-dung-amazon-ses-smtp-de-gui-email-tu-wordpress

https://vticloud.io/amazon-ses-la-gi-huong-dan-tong-hop-ve-dich-vu-amazon-ses/

Comments


Posted

in

, , ,

by

Comments

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *