通过 python 脚本将邮件传播到 Google 群组地址的问题

问题描述

我正在尝试更新此脚本以使用自定义 google 群组(我已向公众开放权限)而不是个人地址。对此进行测试时,我发现 Slack 在我们的一个频道中收到了消息,但电子邮件本身从未分发给该组,然后分发给每个单独的用户。我错过了什么?

#!/usr/bin/python
from   __future__ import print_function
__requires__ = [
    'boto3~=1.4','doapi==0.2.*','pytz','requests==2.*',]
import datetime
import json
from   operator   import attrgetter
import os
import sys
import boto3
from   doapi      import doapi
import pytz
import requests

dokey = os.environ['bamboo_do_test_token']

aws_creds = {
    'aws_access_key_id':     os.environ['bamboo_aws_access_key'],'aws_secret_access_key': os.environ['bamboo_aws_secret_password'],}

sendgrid_api_key = os.environ['bamboo_sendgrid_api_password']

from_address = 'example@example.net'

to_addresses = [
    'example.slack.com','genericgooglegroup@gmail.com'
]

Now = datetime.datetime.Now(pytz.utc)

def elapsed(ts):
    delta = Now - ts
    return '{}d {}h'.format(delta.days,delta.seconds // 3600)

def instance_table(instances,fields):
    html = '<table border="1" cellpadding="5" style="border-collapse:collapse">'
    html += '<tr>' + ''.join('<th>' + f + '</th>' for f,_ in fields) + '</tr>'
    tbody = ''
    for inst in instances:
        tbody += '<tr>' + ''.join('<td><tt>' + str(attr(inst)) + '</tt></td>'
                                  for _,attr in fields) + '</tr>'
    if tbody:
        return html + tbody + '</table>'
    else:
        return 'No instances currently provisioned'

msg = '<h2>DigitalOcean Droplets</h2>'
msg += instance_table(doapi(dokey).fetch_all_droplets(),[
    ("ID",attrgetter("id")),("Name",attrgetter("name")),("IP Address",attrgetter("ip_address")),("Created",lambda d: d.created_at.isoformat(' ')),("Age",lambda d: elapsed(d.created_at)),("Status",attrgetter("status")),("Image",lambda d: d.image.slug or d.image.id),("Size",attrgetter("size_slug")),("Region",attrgetter("region_slug")),])

msg += '<h2>AWS EC2 Instances</h2>'
client = boto3.client('ec2',region_name='test',**aws_creds)
msg += instance_table(
    (
        setattr(i,'region',region['RegionName']) or i
        for region in client.describe_regions()['Regions']
        for i in boto3.resource(
            'ec2',region_name=region['RegionName'],**aws_creds
        ).instances.all()
    ),[
        ("ID",attrgetter("public_ip_address")),("DNS Name",attrgetter("public_dns_name")),("Launched",lambda ec: ec.launch_time.isoformat(' ')),lambda ec: elapsed(ec.launch_time)),("State",lambda ec: ec.state["Name"]),lambda ec: ec.image and ec.image.name),attrgetter("region")),]
)

r = requests.post(
    'https://test',headers={
        "Authorization": "test" + sendgrid_api_key,"Content-Type": "application/json","Accept": "application/json",},data=json.dumps({
        "from": {"email": from_address},"subject": "ALERT: Test","content": [
            {"type": "text/html","value": msg},],"personalizations": [
            {"to": [{"email": addr} for addr in to_addresses]}
        ],"tracking_settings": {
            "click_tracking": {"enable": False},"open_tracking": {"enable": False},"subscription_tracking": {"enable": False},"ganalytics": {"enable": False},}),)

if not r.ok:
    if 400 <= r.status_code < 500:
        msg = '{0.status_code} Client Error: {0.reason} for url: {0.url}'
    elif 500 <= r.status_code < 600:
        msg = '{0.status_code} Server Error: {0.reason} for url: {0.url}'
    else:
        msg = '{0.status_code} UnkNown Error: {0.reason} for url: {0.url}'
    print(msg.format(r),file=sys.stderr)
    try:
        resp = r.json()
    except ValueError:
        print(r.text.encode('utf-8'),file=sys.stderr)
    else:
        print(json.dumps(resp,sort_keys=True,indent=4,separators=(',',': ')),file=sys.stderr)
    sys.exit(1)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)