0

我有以下用于发送电子邮件的子例程,我正在尝试从变量“to”中删除用户“username1”,该变量是逗号分隔的电子邮件 ID,正在使用 to=to.strip('username1')似乎不起作用,知道如何从变量“to”中删除 username1@company.com

def email (body,subject,to=None):
    msg = MIMEText("%s" % body)
    msg["Content-Type"] = "text/html"
    msg["From"] = "serviceaccount@company.com"
    if to!=None:
        to=to.strip()
        to=to.strip('username1@company.com')
        msg["To"] = to
        print to
4

1 回答 1

0

用逗号将其拆分,然后用逗号减去排除的逗号将它们重新加入...

msg['To'] = ','.join(email for email in msg['To'].split(',') if email != 'username1@company.com')

您还可以将其概括为具有“请勿发送邮件”的列表,例如:

DO_NOT_MAIL = ['username1@companyname.com', 'username2@anothercompany.com']

def email(body, subject, to=None):
    msg = MIMEText("%s" % body)
    msg["Content-Type"] = "text/html"
    msg["From"] = "serviceaccount@company.com"
    msg["To"] = ', '.join(email for email in set(to).difference(DO_NOT_MAIL))
于 2013-10-16T22:29:01.873 回答