通过Gmail API发送AMP电子邮件的Python脚本,而不是在接收方端解密

rggaifut  于 2023-06-04  发布在  Python
关注(0)|答案(1)|浏览(533)

AMP电子邮件未在接收方端解密。
我在amp playground中发送了一封AMP电子邮件,并在amp validator和amp playground中验证了代码。代码在这两种情况下都通过了,我能够通过amp playground发送一封测试amp电子邮件。
所以我写了一个python脚本来使用API复制相同的内容。我在Google Cloud中设置了一个项目,并将凭据下载到本地系统中。凭证被存储在credentials.json文件中,并且为了捕获令牌,创建了名为token.json的空白文件。在运行脚本时,身份验证没有问题,邮件也被发送,但接收到的内容是加密的共振峰,并且在接收者端没有解密。

import os
import base64
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Scopes required for Gmail API access
SCOPES = ['https://www.googleapis.com/auth/gmail.send']

# Path to the credentials file downloaded from Google Cloud Console
CREDENTIALS_FILE = os.path.expanduser('path of credentials.json file')

# Path to the token file generated after authorization
TOKEN_FILE = os.path.expanduser('path of token.json file')

# Email content
amp_html = """
<!doctype html>
<html ⚡4email data-css-strict>
<head>
  <meta charset="utf-8">
  <style amp4email-boilerplate>body{visibility:hidden}</style>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
</head>
<body>Hello, <br> Please enter the details for referrals.<br>
  <form method="post" action-xhr="address of webhook">
    <label for="startup-name">Startup Name:</label>
    <input type="text" id="startup-name" name="startupName" required>
    <br>
    <label for="founder-name">Founder Name:</label>
    <input type="text" id="founder-name" name="founderName" required>
    <br>
    <input type="submit" value="Submit">
  </form>
</body>
</html>
"""

def create_amp_message(sender, receiver, subject, amp_html):
    message = MIMEMultipart("alternative")
    message["to"] = receiver
    message["from"] = sender
    message["subject"] = subject

    amp_part = MIMEText(amp_html, "html", "utf-8")
    amp_part.replace_header("Content-Type", "text/html; charset=utf-8")
    amp_part.replace_header("Content-Transfer-Encoding", "quoted-printable")
    amp_part.add_header("Content-Disposition", "inline")
    message.attach(amp_part)

    raw_message = base64.b64encode(message.as_bytes()).decode()
    return {"raw": raw_message}

def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
    creds = flow.run_local_server(port=8080)  
    creds_file_content = creds.to_json()

    with open(TOKEN_FILE, 'w') as token:
        token.write(creds_file_content)
    return build('gmail', 'v1', credentials=creds)

def send_email(service, user_id, message):
    try:
        message = service.users().messages().send(userId=user_id, body=message).execute()
        print('AMP email sent successfully! Message ID: %s' % message['id'])
    except Exception as e:
        print('An error occurred while sending the email: %s' % str(e))

def main():
    sender_email = "sender email id"
    receiver_email = "receiver email id"
    subject = "AMP Email using Python"

    amp_message = create_amp_message(sender_email, receiver_email, subject, amp_html)
    service = get_authenticated_service()
    send_email(service, 'me', amp_message)

if __name__ == '__main__':
    main()

我试着用amp验证器和amp-playground验证amp邮件正文,这两种情况下代码都通过了验证。我也能够发送一个测试电子邮件与相同的代码从ampPlayground。
我写了python脚本来从我的本地机器上复制相同的内容,这样我就可以把它交给google进行白名单。但是在接收器端接收的内容是加密形式的,并且它没有被解密。我试着把它发送到不同的邮件服务器,如Gmail和Yandex都出现了同样的问题。

gojuced7

gojuced71#

几个观察结果:
1.您在text/html MIME部件中包含AMP HTML。您应该在text/x-amp-html MIME部分中包含AMP HTML,并在text/html MIME部分中包含回退静态HTML电子邮件。您需要同时具有AMP MIME部分和回退MIME部分(HTML或纯文本)。
1.您指定了quoted-printable作为编码方案,但不清楚您的脚本是否使用quoted-printable正确编码了您的电子邮件(我不熟悉您使用的库)。
1.该脚本似乎正在向me发送电子邮件,这是Gmail API中向自己发送电子邮件的别名。如果您在Gmail中测试AMP电子邮件,则需要确保发件人不是您自己,因为不支持:
电子邮件的“发件人”和“收件人”标头字段必须包含不同的电子邮件地址。
https://developers.google.com/gmail/ampemail/testing-dynamic-email#delivery_requirements

相关问题