抄送电子邮件和多个 TO 电子邮件地址不起作用

CC email and multiple TO email addresses not working

我在 Python 中创建了一个函数,它将构建 html 格式的电子邮件并通过接受匿名电子邮件发送的 Exchange 服务器匿名发送它。

但是,抄送列表中的任何人都不会收到副本,只有收件人列表中的第一个人会收到副本。我试过 'comma' 和 'semicolon' 作为电子邮件分隔符,但都不起作用。奇怪的是,在收到的电子邮件中,可以看到所有电子邮件地址(并且格式正确),尽管事实上只有一个人(收件人列表中列在第一位的人)收到了一份副本。

这是我的代码

def send_email(sender, subject, sendAsDraft = True): # sendAsDraft not implemented

    global toEmail
    global ccEmail

    # create the email message
    htmlFile = open(saveFolder + '\' + htmlReportBaseName + '.html',encoding = 'utf-8')
    message = htmlFile.read().replace('\n', '')
    message = message.replace(chr(8217), "'") # converts a strange single quote into the standard one
    message = message.replace("'",''') # makes a single quote html friendly
    htmlFile.close()

    # get the email body text
    emailBody = open('emailBody.txt')
    bodyText = emailBody.read()
    emailBody.close()

    now = dt.datetime.today().replace(second = 0, microsecond = 0)
    timeStr = returnReadableDate(now) # returns the date as a string in the 'normal' reading format

    # insert the current date/time into the marker in the email body
    bodyText = bodyText.replace('xxxx', timeStr)

    # insert the email body text into the HTML
    message = message.replace('xxxx', bodyText)

    msg = MIMEMultipart('Report Email')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = '; '.join(toEmail) 
    msg['Cc'] = '; '.join(ccEmail) 
    msg.add_header('Content-Type','text/html')
    
    msg.set_payload(message)

    # complete the email send
    message = ''
    try:
        smtpObj = sl.SMTP('1.2.3.4:5') # obviously IP address anonymised
        smtpObj.sendmail(sender, '; '.join(toEmail), msg.as_string()) 
        message = 'Email successfully sent'        
    except:
        message = 'Email could not be sent due to an error'
    finally:
        # write the outcome to the Instance log
        global InstanceLog
        # write the log
        log(InstanceLog, message)

    # close object and exit function    
    smtpObj.quit()

有没有可能是邮件服务器的问题?未经身份验证的电子邮件只能发送给一个人?这在某种程度上看起来很奇怪,但在另一方面却很有意义。

谢谢大家

西米

查看 [=11th=],这应该是一个逗号分隔的字符串:

msg['To'] = ', '.join(toEmail)

当心。 send_message 可以使用消息的 headers 来构建其收件人列表,但是 sendmail 需要地址的 列表 或将字符串作为单个字符串处理收件人地址。

并且 headers 应该包含逗号而不是分号。

所以你应该:

...
msg['To'] = ' '.join(toEmail) 
msg['Cc'] = ','.join(ccEmail) 
...

及以后:

smtpObj.sendmail(sender, toEmail + ccEmail, msg.as_string())

或:

smtpObj.send_message(msg)